bucket_script 内部过滤器聚合抛出错误

bucket_script inside filter aggregation throws error

我正在尝试过滤过滤器聚合块中的空桶,但我从 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(例如英国国家/地区 - 2 个文档没有 "x" 字段) 不要 return 国家桶给客户。

你需要一个 bucket_selector 并且脚本略有不同 aaand 放在更高的级别:

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