我正在尝试过滤过滤器聚合块中的空存储桶,并且我从elasticsearch 中收到错误。如果没有这个,响应会很大,因为我正在查询大量指标和嵌套聚合(为了简单起见,这是更大查询的一部分)
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 "
}
}
}
}
}
}
}
}
弹性响应:
{
"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
}
我想做的是: 如果对于给定的国家/地区存储桶,没有字段 x(例如国家 UK - 2 文档没有“x”字段) 不要将国家/地区存储桶返回给客户端。
您需要一个
bucket_selector
并让脚本略有不同并放置在更高的级别:
{
"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"
}
}
}
}
}
}