我有一些在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
...
你知道什麼時候以及如何修復嗎(直接訪問'@response ['hits'] ['hits']')?因爲我沒有發現有關它的任何信息。 –