elasticsearch - 弹性搜索聚合查找所有记录中是否存在字段
问题描述
我在 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"
}
}
}
}
}
}
但bool
和not
似乎不能一起使用。
如何使用 Elastic Search 聚合来实现这一点?
解决方案
替换not
为must_not
,
"aggregations": {
"fooContainsNull": {
"filter": {
"bool": {
"must_not": {
"exists": {
"field": "foo"
}
}
}
}
}
}
这将为您提供一些没有字段foo
反对的文档fooContainsNull
。因此,您可以通过检查此数字是 0 还是大于 0 来构建逻辑。
推荐阅读
- c++ - 在 gcc 中将二维数组初始化为 0 时的值不正确
- javascript - 电子应用程序:如何使 dialog.showOpenDialog 模态
- datatables - 数据表 - 隐藏数据直到搜索
- arrays - 如何访问数据框列中的数组元素(scala)
- accessibility - Jaws 快捷键,用于将焦点从 JAWS 查找对话框转移到搜索词出现
- javascript - 文本输入不为空时启用表单提交按钮的最简单方法
- amazon-web-services - 创建 DMS 复制实例时选择 VPC
- sql-server - 如何使用 sql server 创建与具有等级或任何其他功能的更改日期时间相关联的序列号
- javascript - 位置 1 处的 JSON 中的意外标记
- ignite - 点燃 AffinityKeyMapped 和 AffinityKeyMapper