在弹性搜索中进行聚合后进行排序 [英] Sorting after aggregation in Elasticsearch

查看:197
本文介绍了在弹性搜索中进行聚合后进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
FIELD1:string,
FIELD2:
[{SUBFIELD:number},{SUBFIELD:number} ...]
}

我想对FIELD2.SUBFIELD中的数字总和进行排序:

  GET myindex / _search 
{
size:0,
aggs:{
a1:{
terms:{
field:FIELD1 ,
size:0
},
aggs:{
a2:{
sum:{
:FIELD2.SUBFIELD
}
}
}
}
}
}
pre>

如果我这样做,我获得没有排序的桶,但我想要按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 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"
          }
        }
      }
    }
  }
}

这篇关于在弹性搜索中进行聚合后进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆