我打算在rails項目中使用彈性搜索,當我搜索一些單詞時出現這個錯誤。,它在我的文章中過多使用的NoMethodError(undefined method'highlight'for#<Elasticsearch :: Model :: Response :: Result>
NoMethodError (undefined method `highlight' for #<Elasticsearch::Model::Response::Result:0x007f062ed26708>)
我在日誌生產得到了這個,這是我做了什麼,一切:在控制器 :
# POST /search/article
def search
render json: Article.search(params[:query]), each_serializer: ElasticsearchResultsSerializer
end
這是我article.rb模型
#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的確切版本?還請顯示完整的堆棧跟蹤。 – Val
我認爲這是2.x的,因爲我運行此命令捲曲-XGET的 '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」 }, 「tagline」:「你知道,用於搜索」 – Sandro
並獲得此結果 – Sandro