如何在 Elasticsearch 中按 children 的数量对 parents 进行排序
How to sort parents by number of children in Elasticsearch
我的索引中有 parent
/child
相关文档,我想获得按 children 的数量排序的 parent 的列表。有什么办法吗?我正在使用 Elasticsearch
1.5.1
现在我可以通过使用 inner_hits
功能轻松获得 children 个文档以及 parent 个查询结果,但似乎无法访问 inner_hits.{child_type_name}.hits.total
值来自脚本或 search/score 函数。有什么想法吗?
好吧,我终于找到了答案。感谢@doctorcal 在 #elasticsearch IRC
上的提示
正如我在问题中提到的,我们可以使用 Elasticsearch
1.5 中的 inner_hits 获得 children 的列表以及每个 parent。
为了能够按 children 的数量对 parent 进行排序,我们需要使用一个小技巧 - 将 children 的数量放入 parent 中分数(默认用于排序)。为此,我们只使用得分模式 sum
for has_child query:
{
"query": {
"has_child": {
"type": "comment",
"score_mode": "sum",
"query": {
"match_all": {}
},
"inner_hits": {}
}
}
}
注意:此查询有一个限制 - 您似乎无法保留有关初始分数(查询的相关分数)的信息,因为我们将它们替换为 children.
的数量
我的索引中有 parent
/child
相关文档,我想获得按 children 的数量排序的 parent 的列表。有什么办法吗?我正在使用 Elasticsearch
1.5.1
现在我可以通过使用 inner_hits
功能轻松获得 children 个文档以及 parent 个查询结果,但似乎无法访问 inner_hits.{child_type_name}.hits.total
值来自脚本或 search/score 函数。有什么想法吗?
好吧,我终于找到了答案。感谢@doctorcal 在 #elasticsearch IRC
上的提示正如我在问题中提到的,我们可以使用 Elasticsearch
1.5 中的 inner_hits 获得 children 的列表以及每个 parent。
为了能够按 children 的数量对 parent 进行排序,我们需要使用一个小技巧 - 将 children 的数量放入 parent 中分数(默认用于排序)。为此,我们只使用得分模式 sum
for has_child query:
{
"query": {
"has_child": {
"type": "comment",
"score_mode": "sum",
"query": {
"match_all": {}
},
"inner_hits": {}
}
}
}
注意:此查询有一个限制 - 您似乎无法保留有关初始分数(查询的相关分数)的信息,因为我们将它们替换为 children.
的数量