通过子聚集结果值对术语聚集桶进行排序 [英] Ordering term aggregation buckets by sub-aggregration result values
问题描述
关于此捕获显示的查询,我有两个问题:
I have two questions about the query seen on this capture:
-
如何按
sum_category
字段中的> value ?
我在查询中再次使用respsize
,但是如下所示,它是不正确的。
How do I order by
value
in thesum_category
field in the results? I userespsize
again in the query but it's not correct as you can see below.
即使我仅进行了汇总,为什么所有文档都附带结果?我的意思是,如果我在SQL中进行 group by
查询,它将仅检索分组的数据,但是Elasticsearch检索所有文档,就像我进行常规搜索查询一样。我怎么跳过它们?
Even if I make only an aggregration, why do all the documents come with the result? I mean, if I make a group by
query in SQL it retrieves only grouped data, but Elasticsearch retrieves all documents as if I made a normal search query. How do I skip them?
推荐答案
尝试一下:
{
"query" : {
"match_all" : {}
},
"size" : 0,
"aggs" : {
"categories" : {
"terms" : {
"field" : "category",
"size" : 999999,
"order" : {
"sum_category" : "desc"
}
},
"aggs" : {
"sum_category" : {
"sum" : {
"field" : "respsize"
}
}
}
}
}
}
1)。有关您的排序
的操作,请参见(2)中的注释。至于按sum_category的值对类别进行排序,请参见 order
部分。似乎存在与该 https://github.com/elastic/elasticsearch相关的古老且封闭的问题/ issues / 4643 ,但在Elasticsearch v1.5.2版本中对我来说效果很好。
1). See the note in (2) for what your sort
is doing. As for ordering the categories by the value of sum_category, see the order
portion. There appears to be an old and closed issue related to that https://github.com/elastic/elasticsearch/issues/4643 but it worked fine for me with v1.5.2 of Elasticsearch.
2)。尽管您没有该match_all查询,但我认为这可能是您得到的结果。因此,您指定的排序实际上已应用于这些结果。为了不让它们回来,我只提供了 size:0
部分。
2). Although you do not have that match_all query, I think that's probably what you are getting results for. And so the sort your specified is actually getting applied to those results. To not get these back, I just have size: 0
portion.
是否要为所有类别使用存储桶?我注意到您没有为主要集合指定大小。这就是 size:999999
部分。
Do you want buckets for all the categories? I noticed you do not have size specified for the main aggregation. That's the size: 999999
portion.
这篇关于通过子聚集结果值对术语聚集桶进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!