2012-09-11 31 views
0

我有一些在Elasticsearch(通过轮胎宝石)索引的Rails模型。我可以索引新文档并查询现有索引。轮胎宝石:如何访问Elasticsearch的'突出'属性?

我似乎无法做到的是在我的Rails应用程序中获取连接到记录的突出显示。我可以但是,当我通过curl直接与Elasticsearch进行交互时,请参阅在json中返回highlight

当我尝试访问我的纪录的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 
    ... 

回答

3

方法当您使用load: true选项时,highlight无法访问。这应该在未来的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 
+0

你知道什么时候以及如何修复吗(直接访问'@response ['hits'] ['hits']')?因为我没有发现有关它的任何信息。 –

0

您可以直接在这个帖子找到我的答案 Elasticsearch/Lucene highlight

我的方法工作正常对我而言,希望你也能得到它的工作。

相关问题