Bucket_script inside filter aggregation throws error

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, and nested aggregation (this is part of bigger query for simplicity )

GET index/type/_search?ignore_unavailable
{
  "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_script": {
                "buckets_path": {
                  "count": "_count"
                },
                "script": "return params.count "
              }
            }
          }
        }
      }
    }
  }
}

elastic response:

{
  "error": {
    "root_cause": [],
    "type": "reduce_search_phase_exception",
    "reason": "[reduce] ",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "class_cast_exception",
      "reason": "org.elasticsearch.search.aggregations.bucket.filter.InternalFilter cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation"
    }
  },
  "status": 503
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.