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
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"
}
}
}
}
}
}