bucket_script里面的过滤器聚合抛出错误 [英] bucket_script inside filter aggregation throws error

查看:791
本文介绍了bucket_script里面的过滤器聚合抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试过滤一个过滤器汇总块中的空桶,我从弹性搜索中得到一个错误。没有这个响应是巨大的,因为我正在查询很多指标和嵌套聚合(这是简单的更大的查询的一部分)

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

弹性响应:

{
  "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字段)
不将国家/地区桶返回给客户端。

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.

推荐答案

您需要一个 bucket_selector ,脚本稍微不同,放置在更高的级别上:

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

这篇关于bucket_script里面的过滤器聚合抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆