首页 > 解决方案 > 弹性搜索聚合查找所有记录中是否存在字段

问题描述

我在 Elastic Search 上有一个索引,大多数记录都有 field foo,有些可能没有。

我想foo通过聚合来了解所有记录是否都有字段。

情况1:

id = 0, foo = 1, bar = 1

做这样的聚合会给我fooContainsNull = false,因为所有记录都包含foo

案例2:

id = 0, foo = 1, bar = 1
id = 1, bar = 1

做这样的聚合会给我fooContainsNull = true,因为记录之一(id = 1)没有foo

我尝试使用过滤器聚合

    "aggregations": {
        "fooContainsNull": {
          "filter": {
            "bool": {
              "not": {
                "exists": {
                  "field": "foo"
                }
              }
            }
          }
        }
    }

boolnot似乎不能一起使用。

如何使用 Elastic Search 聚合来实现这一点?

标签: elasticsearch

解决方案


替换notmust_not,

"aggregations": {
        "fooContainsNull": {
          "filter": {
            "bool": {
              "must_not": {
                "exists": {
                  "field": "foo"
                }
              }
            }
          }
        }
    }

这将为您提供一些没有字段foo反对的文档fooContainsNull。因此,您可以通过检查此数字是 0 还是大于 0 来构建逻辑。


推荐阅读