bucket_script inside filter aggregation throws error

纵饮孤独 提交于 2019-12-02 04:29:55

You need a bucket_selector for that and the script slightly different aaand placed on a higher level:

{
  "size": 0,
  "aggs": {
    "groupby_country": {
      "terms": {
        "field": "country",
        "size": 2000
      },
      "aggs": {
        "exists__x__filter": {
          "filter": {
            "bool": {
              "filter": [
                {
                  "exists": {
                    "field": "x"
                  }
                }
              ]
            }
          },
          "aggs": {
            "sum": {
              "sum": {
                "script": "def val = doc['x'].value; if(val>0) Math.min(val , 20000)"
              }
            }
          }
        },
        "average_distinct": {
          "bucket_selector": {
            "buckets_path": {
              "count": "exists__x__filter._count"
            },
            "script": "params.count > 0"
          }
        }
      }
    }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!