2017-04-18 7 views
16

के लिए अपरिभाषित विधि 'हाइलाइट' मैं रेल परियोजना पर अपने रूबी के लिए लोचदार खोज का उपयोग करने जा रहा हूं। मुझे यह त्रुटि मिलती है जब मैं कुछ शब्द खोजता हूं ।। नियंत्रक में:NoMethodError (# <Elasticsearch :: मॉडल :: प्रतिक्रिया :: परिणाम>

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

यह मेरा article.rb मॉडल है यह मेरे लेख बहुत ज्यादा में प्रयोग किया जाता है कि

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

मैं लॉग उत्पादन में यह मिल गया यह है कि क्या सब कुछ है कि मैंने किया है

#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 

और यह भी मैं 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

चूंकि आपने 2.x और 5.x टैग किया है, तो आप किस ईएस का सटीक संस्करण उपयोग कर रहे हैं? कृपया पूरा स्टैक ट्रेस दिखाएं। – Val

+0

मुझे लगता है कि यह 2.x है क्योंकि मैं इस आदेश को कर्ल -XGET 'स्थानीय होस्ट: 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

+0

और इस परिणाम को प्राप्त करें – Sandro

उत्तर

4

हो सकता है कि एक मूर्खतापूर्ण अवलोकन है इस्तेमाल किया है, लेकिन आप मूल्य

:_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

मैंने ऐसा किया लेकिन अब और काम नहीं करता है। – Sandro

+0

अनमोर? कैसे? क्या आप कृपया मुझे पूरा स्टैक ट्रेस त्रुटि दिखा सकते हैं? –

+0

आपको वास्तव में क्या चाहिए – Sandro

2

मैं करने के लिए खेतों स्विचन की कोशिश करेंगे:

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

बस देखने के लिए कि अगर त्रुटि से छुटकारा मिलता है। निम्नलिखित देखें: https://github.com/elastic/elasticsearch-rails/issues/446