从弹性搜索聚合中删除“”桶

Removing '' buckets from elasticsearch aggregation

如何让 elasticsearch aggregation 在不修改 query 项的情况下从聚合结果中删除某些存储桶键。例如,我想从存储桶列表中删除 '' 个空键。我可以使用某种 having 子句吗?

我的查询是这样的

{
  "query": {
    "terms": {
      ...
    }
  },
  "aggs": {
        "name_match": {
          "terms": {
            "field": "name_match",
            "min_doc_count": 2,
            "size": 20
        }
    }
  }
}

结果:

{
    "hits": {
     ...
    },
    "aggregations": {
          "name_match": {
             "buckets": [
                {
                   "key": "A",
                   "doc_count": 136
                },
                {
                   "key": "B",
                   "doc_count": 16
                },
                {
                   "key": "",
                   "doc_count": 24
                }]
              }
    }
}

想要的结果

{
    "hits": {
     ...
    },
    "aggregations": {
          "name_match": {
             "buckets": [
                {
                   "key": "A",
                   "doc_count": 136
                },
                {
                   "key": "B",
                   "doc_count": 16
                }]
              }
    }
}

是的,可以使用 terms aggregation 中可用的 exclude 参数。此参数采用需要从存储桶中排除的键数组。要排除特定键,您的聚合应按如下所示重写:

{
  "query": {
    "terms": {
      ...
    }
  },
  "aggs": {
        "name_match": {
          "terms": {
            "field": "name_match",
            "exclude": [""],
            "min_doc_count": 2,
            "size": 20
        }
    }
  }
}

您可以阅读有关此主题的内容 here

希望对您有所帮助!