NoMethodError (undefined method `highlight' for #<Elasticsearch::Model::Response::Result>
Asked Answered
F

2

16

I'm going to use elastic search for my ruby on rails project. I get this error when I search some of the word that It's used in my article too much.

NoMethodError (undefined method `highlight' for #<Elasticsearch::Model::Response::Result:0x007f062ed26708>)

i got this in the log production. this is what everything that i did: in controller:

      # POST /search/article
      def search
        render json: Article.search(params[:query]), each_serializer: ElasticsearchResultsSerializer
      end

this is my article.rb model

  #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

and also i used serializer

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
Friedrick answered 18/4, 2017 at 6:6 Comment(6)
Since you tagged 2.x and 5.x, which exact version of ES are you using? Also please show the full stack trace.Crist
i think it is 2.x because i run this command curl -XGET 'localhost:9200' { "name" : "Dark Beast", "cluster_name" : "elasticsearch", "cluster_uuid" : "s9SER6_tR5ezUoM83r6ETg", "version" : { "number" : "2.4.1", "build_hash" : "c67dc32e24162035d18d6fe1e952c4cbcbe79d16", "build_timestamp" : "2016-09-27T18:57:55Z", "build_snapshot" : false, "lucene_version" : "5.5.2" }, "tagline" : "You Know, for Search"Friedrick
and get this resultFriedrick
Have you tried flushing the cache? (elastic.co/guide/en/elasticsearch/reference/current/…)Thapsus
@TomLord I did my best to do the cache on the shared link, but unfortunately i couldn't do it, can u give me a tutorial or give me a clue,Friedrick
@AndroidDev what happens if you remove the :highlight attribute in your serializer (since it's used nowhere)?Crist
P
4

Maybe is a silly observation, but did you tried to change the value

:highlight to :_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
Particiaparticipant answered 2/5, 2017 at 17:41 Comment(2)
Anymore? how? Can you show the full stack trace error to me please?Particiaparticipant
what do u need exactlyFriedrick
K
2

I would try switching fields to:

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

Just to see if that gets rid of the error. See the following: https://github.com/elastic/elasticsearch-rails/issues/446

Kerf answered 13/5, 2017 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.