Search code examples
elasticsearchelasticsearch-5

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
}

what I'm trying to do is: if for a give country bucket, there is no field x (i for example country UK - 2 documents doesn't have the "x" field) don't return the country bucket to the client.


Solution

  • You need a bucket_selector for that and have the script slightly different and 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"
              }
            }
          }
        }
      }
    }