2017-04-18 13 views
16

Rubyプロジェクトでrubyの検索に弾性検索を使用します。それは、あまりにも多くの私の記事に使われているNoMethodError(未定義のメソッドのハイライト表示)#<Elasticsearch :: Model :: Response :: Result>

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

私は、ログの生産にこれを得た、これは私がやったすべてのものです:。。これは私のarticle.rbモデルです

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

:コントローラで

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

あなたは2.xと5.xというタグを付けているので、どのESの正確なバージョンを使用していますか?また、完全なスタックトレースを表示してください。私はカール-XGETこのコマンドを実行するため – Val

+0

私はそれは2.xのだと思う '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" }、 "タグライン": "あなたが知っているのは、検索のためのもの" – 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

関連する問題