2017-04-18 18 views
16

Tôi sẽ sử dụng tìm kiếm đàn hồi cho ruby ​​của tôi trên dự án ray. Tôi nhận được lỗi này khi tôi tìm kiếm một số từ .. mà Nó được sử dụng trong bài viết của tôi quá nhiềuNoMethodError (phương thức undefined `highlight 'cho # <Elasticsearch :: Model :: Response :: Result>

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

tôi đã nhận này trong việc sản xuất bản ghi đây là những gì tất cả mọi thứ mà tôi đã làm: trong điều khiển:

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

đây là mô hình article.rb của tôi

#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 

và tôi cũng sử dụng 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 
+0

Vì bạn đã gắn thẻ 2.x và 5.x, bạn đang sử dụng phiên bản ES chính xác nào? Ngoài ra vui lòng hiển thị dấu vết ngăn xếp đầy đủ. – Val

+0

tôi nghĩ rằng đó là 2.x vì tôi chạy lệnh curl -XGET 'localhost: 9200' { "tên": "Dark Beast", "cluster_name": "elasticsearch", "cluster_uuid": "s9SER6_tR5ezUoM83r6ETg" , "phiên bản": { "số": "2.4.1", "build_hash": "c67dc32e24162035d18d6fe1e952c4cbcbe79d16", "build_timestamp": "2016-09-27T18: 57: 55Z", "build_snapshot": false , "lucene_version": "5.5.2" }, "khẩu hiệu": "Bạn biết, để tìm kiếm" – Sandro

+0

và nhận kết quả này – Sandro

Trả lời

4

lẽ là một quan sát ngớ ngẩn, nhưng bạn đã cố gắng để thay đổi giá trị

: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 
+0

Tôi đã làm điều đó nhưng không hoạt động nữa. – Sandro

+0

Anymore? làm sao? Bạn có thể hiển thị lỗi theo dõi ngăn xếp đầy đủ cho tôi không? –

+0

bạn cần gì chính xác – Sandro

Các vấn đề liên quan