gpt4 book ai didi

ruby-on-rails - Tire gem:如何访问Elasticsearch的 'highlight'属性?

转载 作者:行者123 更新时间:2023-12-03 00:44:11 24 4
gpt4 key购买 nike

我有一些在Elasticsearch(通过Tire gem)中建立索引的Rails模型。我可以索引新文档并查询现有索引。

我似乎无法做的是从Rails应用程序中获取附加到记录的高亮。但是,当我直接通过highlight与Elasticsearch进行交互时,我可以看到jsont返回了curl

当我尝试访问记录的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返回到Tire ...
$ 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
...

最佳答案

使用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

关于ruby-on-rails - Tire gem:如何访问Elasticsearch的 'highlight'属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12379194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com