elasticsearch - 如何获取相同键中具有最高 _score 的文档?
问题描述
我正在研究 Elasticsearch。如何获取相同键中具有最高 _score 的文档?我应该使用或阅读哪个 API?
示例文档和查询
{ "foreignId": 1, "body": "One is a green apple, however another one is red."}
{ "foreignId": 1, "body": "apple apple apple apple" } // has a higher score than the one right above
{ "foreignId": 2, "body": "One is a green apple, however another one is red."}
{ "foreignId": 2, "body": "apple apple apple" } // has a higher score than the one right above
{"query": { "match_phrase": { "body": "apple"}}}
预期的
{ "foreignId": 1, "body": "apple apple apple apple", _score: <the highest where foreignId=1> }
{ "foreignId": 2, "body": "apple apple apple", _score: <the highest where foreignId=2> }
解决方案
您可以使用字段折叠并按 _score 排序
{
"query": {
"match_phrase": {
"body": "apple"
}
},
"collapse": {
"field": "foreignId"
},
"sort": ["_score"]
}
推荐阅读
- amazon-web-services - Terraform AWS,未创建实例
- angular - 服务工作者获取事件未触发(路径问题)
- python-3.x - python中的写入模式不创建新文件
- c++ - 我如何从 std::vector 转换
到 std::vector ? - sql-server - 可以禁用为唯一约束自动创建唯一索引吗?
- java - 如何在 Spring Java 的后端服务中使用 Google Web oauth2
- oracle - 获取 PL/SQL 集合中特定元素的键或索引?
- jquery - Laravel Pusher 私人频道不适用于 Jquery
- html - 如何使手机上的标志出现在左侧,但在大屏幕上保持在中央?
- python - 如果键已包含在字典中,如何以智能方式将新的键值对添加到字典中?