Elasticsearch聚合后排序 [英] Sorting after aggregation in Elasticsearch
本文介绍了Elasticsearch聚合后排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这种结构的文档:
{
FIELD1:string,
FIELD2:
[ {SUBFIELD:number}, {SUBFIELD:number}...]
}
我想对 FIELD2.SUBFIELDs 中数字总和的结果进行排序:
I want to sort on the result of the sum of numbers in FIELD2.SUBFIELDs:
GET myindex/_search
{
"size":0,
"aggs": {
"a1": {
"terms": {
"field": "FIELD1",
"size":0
},
"aggs":{
"a2":{
"sum":{
"field":"FIELD2.SUBFIELD"
}
}
}
}
}
}
如果我这样做,我会得到未排序的桶,但我想要按a2"值排序的桶.我怎么能做到这一点?谢谢!
If I do this I obtain buckets not sorted, but I want buckets sorted by "a2" value. How I can do this? Thank you!
推荐答案
您几乎拥有了它.您只需要添加一个 order
属性 到您的 a1
术语聚合,如下所示:
You almost had it. You just need to add an order
property to your a1
terms aggregations, like this:
GET myindex/_search
{
"size":0,
"aggs": {
"a1": {
"terms": {
"field": "FIELD1",
"size":0,
"order": {"a2": "desc"} <--- add this
},
"aggs":{
"a2":{
"sum":{
"field":"FIELD2.SUBFIELD"
}
}
}
}
}
}
这篇关于Elasticsearch聚合后排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文