在弹性搜索中进行聚合后进行排序 [英] Sorting after aggregation in Elasticsearch
本文介绍了在弹性搜索中进行聚合后进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{
FIELD1:string,
FIELD2:
[{SUBFIELD:number},{SUBFIELD:number} ...]
}
我想对FIELD2.SUBFIELD中的数字总和进行排序:
GET myindex / _search
pre>
{
size:0,
aggs:{
a1:{
terms:{
field:FIELD1 ,
size:0
},
aggs:{
a2:{
sum:{
:FIELD2.SUBFIELD
}
}
}
}
}
}
如果我这样做,我获得没有排序的桶,但我想要按a2值排序的桶。
我该怎么做?
谢谢!解决方案你几乎拥有了您只需添加一个
订单
属性到您的a1
条款汇总这个:GET myindex / _search
{
size:0,
aggs:{
a1:{
terms:{
field:FIELD1,
size:0,
order :{a2:desc}< ---添加此
},
aggs:{
a2:{
sum {
field:FIELD2.SUBFIELD
}
}
}
}
}
}
I have docs with this structure:
{ FIELD1:string, FIELD2: [ {SUBFIELD:number}, {SUBFIELD:number}...] }
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" } } } } } }
If I do this I obtain buckets not sorted, but I want buckets sorted by "a2" value. How I can do this? Thank you!
解决方案You almost had it. You just need to add an
order
property to youra1
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" } } } } } }
这篇关于在弹性搜索中进行聚合后进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文