Rubyプロジェクトでrubyの検索に弾性検索を使用します。それは、あまりにも多くの私の記事に使われているNoMethodError(未定義のメソッドのハイライト表示)#<Elasticsearch :: Model :: Response :: Result>
NoMethodError (undefined method `highlight' for #<Elasticsearch::Model::Response::Result:0x007f062ed26708>)
私は、ログの生産にこれを得た、これは私がやったすべてのものです:。。これは私のarticle.rbモデルです
# POST /search/article
def search
render json: Article.search(params[:query]), each_serializer: ElasticsearchResultsSerializer
end
:コントローラで
#default_scope { order('created_at DESC') }
scope :visible, -> { where(enabled: true) }
after_commit on: [:create] do
self.keywords = self.keywords.each {|str| str.force_encoding("UTF-8")}
__elasticsearch__.index_document if self.enabled?
end
after_commit on: [:update] do
self.keywords = self.keywords.each {|str| str.force_encoding("UTF-8")}
__elasticsearch__.update_document if self.enabled?
end
after_commit on: [:destroy] do
__elasticsearch__.delete_document
end
settings index: { number_of_shards: 1, number_of_replicas: 0 }
mappings dynamic: 'false' do
indexes :content, type: "string", index_options: 'offsets'
indexes :title, type: "string"
indexes :description, type: "string"
indexes :category, type: "string"
indexes :created_at, type: "date"
indexes :keywords, type: "string"
end
def self.search(query)
__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
fields: ['title^10', 'content^5', 'description^2', 'keywords', 'category']
}
},
highlight: {
pre_tags: ['<em>'],
post_tags: ['</em>'],
fields: { title: {}, content: {} }
}
}
)
end
def as_indexed_json(options={})
as_json(
only: [:content, :title, :id, :category, :keywords, :description]
)
end
とも私はシリアライザ
class ElasticsearchResultsSerializer < ActiveModel::Serializer
attributes :_id, :highlight, :_score, :_source
def _source
@article = object._index.singularize.capitalize.constantize.find(object._id)
@serializer = "#{object._index.singularize.capitalize}Serializer".constantize
@serializer.new(@article)
end
end
あなたは2.xと5.xというタグを付けているので、どのESの正確なバージョンを使用していますか?また、完全なスタックトレースを表示してください。私はカール-XGETこのコマンドを実行するため – Val
私はそれは2.xのだと思う 'localhostを:9200' { "名前": "ダーク・ビースト"、 "CLUSTER_NAME": "elasticsearch"、 "cluster_uuid": "s9SER6_tR5ezUoM83r6ETg" 、 "バージョン":{ "番号": "2.4.1"、 "build_hash": "c67dc32e24162035d18d6fe1e952c4cbcbe79d16"、 "build_timestamp": "2016-09-27T18:57:55Z"、 "build_snapshot":偽、 "lucene_version": "5.5.2" }、 "タグライン": "あなたが知っているのは、検索のためのもの" – Sandro
この結果を得る – Sandro