如何按照Elasticsearch中的孩子数量对父母进行排序 [英] How to sort parents by number of children in Elasticsearch
问题描述
我的索引中有 parent
/ child
相关的文档,并希望得到按照号码排序的父母列表的孩子。有没有办法呢?我正在使用 Elasticsearch
1.5.1
I have parent
/child
-related documents in my index and want to get list of parents sorted by number of children. Is it any way to do it? I'm using Elasticsearch
1.5.1
现在我可以很容易地获得与父查询一起的子文档数结果使用 inner_hits
功能,但似乎没有办法从脚本或搜索/访问 inner_hits。{child_type_name} .hits.total
得分功能。任何想法?
Right now I can easily get number of children documents together with parent query results by using inner_hits
feature, but it seems no way to access inner_hits.{child_type_name}.hits.total
value from the script or search/score function. Any ideas?
推荐答案
嗯,我发现回答自己,终于。感谢@doctorcal在 #flasticsearch 上的提示IRC
Well, I found answer myself, finally. Thanks to hints from @doctorcal on #elasticsearch IRC
正如我在问题中提到的,我们可以使用 Elasticsearch 1.5中的inner-hits.htmlrel =nofollow> inner_hits 。
As I mentioned in the question, we can get list of children together with each parent using inner_hits in Elasticsearch
1.5.
能够按照他们的孩子的数量对父母进行排序,我们需要使用一个小技巧 - 把孩子的数量放入父母的分数(默认情况下用于排序)。为此,我们只是使用的分数模式 has_child 查询:
To be able to sort parents by number of their children we need to use a small trick - put number of children into the parent's score (which is used to sort by default). For that, we just use the score mode sum
for has_child query:
{
"query": {
"has_child": {
"type": "comment",
"score_mode": "sum",
"query": {
"match_all": {}
},
"inner_hits": {}
}
}
}
注意:此查询有一个限制 - 似乎您不能保留关于初始分数(查询的相关性分数)的信息,因为我们替换他们有孩子的数量。
NOTE: this query has a limitation - it seems you can't keep information about initial scores (relevance scores for the query), since we replace them with number of children.
这篇关于如何按照Elasticsearch中的孩子数量对父母进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!