首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >NoMethodError (用于#<Elasticsearch::Model::Response::Result>的未定义方法‘`highlight’

NoMethodError (用于#<Elasticsearch::Model::Response::Result>的未定义方法‘`highlight’
EN

Stack Overflow用户
提问于 2017-04-18 14:06:00
回答 2查看 1K关注 0票数 16

我将对我的ruby on rails项目使用elastic search。当我搜索一些在我的文章中使用太多的单词时,我得到了这个错误。

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
EN

回答 2

Stack Overflow用户

发布于 2017-05-03 01:41:10

也许是一个愚蠢的观察,但是你有没有尝试改变这个值

:highlight:_highlight

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
票数 4
EN

Stack Overflow用户

发布于 2017-05-14 05:12:37

我会尝试将字段切换为:

fields: {
  :"*" => {}
}

只是为了看看这是否能消除错误。请参阅以下内容:https://github.com/elastic/elasticsearch-rails/issues/446

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43464752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档