bucket_script inside filter aggregation throws error

后端 未结 1 529
一向
一向 2021-01-23 17:16

I am trying to filter empty buckets in side a filter aggregation block, and I get an error from elasticsearch. without this the response is huge, as I am querying lots of metric

相关标签:
1条回答
  • 2021-01-23 18:05

    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"
              }
            }
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题