0

Elasticsearchで(Tire gemを介して)インデックス付けされたRailsモデルがいくつかあります。新しいドキュメントにインデックスを付け、既存のインデックスをクエリできます。

私ができないように見えるのは、Railsアプリ内からレコードに添付されたハイライトを取得することです。ただし、を介してElasticsearchを直接操作すると、jsonに返されることがわかります。highlightcurl

highlightレコードのプロパティにアクセスしようとすると、次のようになります。undefined method 'highlight' for #<Report:0x007fe8afa54700>

# app/views/reports/index.html.haml
%h1 Listing reports
...
  - @reports.results.each do |report|
    %tr
      %td= report.title
      %td= raw report.highlight.attachment.first.to_s

しかし、使用すると、タイヤに戻されるcurlことがわかります...highlight

$ curl -X GET "http://localhost:9200/testapp_development_reports/report/_search?load=true&pretty=true" -d '{query":{"query_string":{"query":"contains","default_operator":"AND"}},"highlight":{"fields":{"attachment":{}}}}'
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.111475274,
    "hits" : [ {
      "_index" : "testapp_development_reports",
      "_type" : "report",
      "_id" : "1",
      "_score" : 0.111475274, "_source" : {"id":1,"title":"Sample Number One",...,"attachment":"JVBERi0xMJ1Ci... ...UlRU9GCg==\n"},
      "highlight" : {
        "attachment" : [ "\nThis <em>contains</em> one\n\nodd\n\n\n" ]
      }
    }, {
      "_index" : "testapp_development_reports",
      "_type" : "report",
      "_id" : "2",
      "_score" : 0.111475274, "_source" : {"id":2,"title":"Number two",...,"attachment":"JVBERi0xLKM3OA... ...olJVPRgo=\n"},
      "highlight" : {
        "attachment" : [ "\nThis <em>contains</em> two\n\neven\n\n\n" ]
      }
    } ]
  }
}

モデルの検索方法:

  ...
  def self.search(params)
    tire.search(load: true) do
      query { string params[:query], default_operator: "AND" } if params[:query].present?
      highlight :attachment
    end
  end
  ...
4

2 に答える 2

3

オプションを使用している場合、メソッドhighlightにアクセスできませんload: true。これは、Tireの将来のバージョンで修正される必要があります。

編集:each_with_hitメソッドを使用して、返されたelasticsearch値に今すぐアクセスできます

例えば:

results = Article.search 'One', :load => true
  results.each_with_hit do |result, hit|
  puts "#{result.title} (score: #{hit['_score']})"
end
于 2012-09-12T08:46:31.730 に答える
0

この投稿で私の答えを見つけることができます Elasticsearch/Luceneハイライト

私の方法は私にとってはうまく機能し、あなたもそれを機能させることができればと思います。

于 2012-11-15T12:58:35.753 に答える