elasticsearch - 嵌套对象的 boolean 过滤器

标签 elasticsearch filter boolean

我在使用带有嵌套对象的 boolean 运算符时遇到了一些麻烦。 这是我的映射:

   "IP": {
        "properties": {
          "ip": {
            "type": "ip"
          }
        },
        "type": "nested"
      }

我想要获取包含两个指定 ip 甚至更多的文档。

假设我的文档有以下 ip:

    DOC 1
        192.168.1.0
        192.168.0.1
        10.0.0.9

    DOC 2
       192.168.0.1

我想通过使用此过滤器搜索来仅检索 DOC 1:

      "bool": {
        "must": {
          "terms": {
            "IP.ip": [
              "192.168.0.1",
              "10.0.0.9"
            ]
          }
        }
      }

问题是 DOC 1 和 DOC2 均被检索。

最佳答案

您可以在 terms filter 上使用 "execution":"and"像这样:

{
   "filter": {
      "bool": {
         "must": [
            {
               "terms": {
                  "ip": [
                     "192.168.0.1",
                     "10.0.0.9"
                  ],
                  "execution": "and"
               }
            }
         ]
      }
   }
}

这是我用来测试它的一些代码:

http://sense.qbox.io/gist/d6b5f4e4c0d2977a04b1795f4bbb0503f6365dfe

编辑:嵌套版本(我误解了这个问题)。

假设您的索引是按照我设置我的测试方式设置的(请参见下面的代码),那么此查询应该为您提供所需的内容。 must 列表中需要有两个 nested 子句,因为我们正在查找包含两个不同嵌套文档(每个文档对应一个 IP)的文档。

POST /test_index/_search
{
   "filter": {
      "bool": {
         "must": [
            {
               "nested": {
                  "path": "ips",
                  "filter": {
                     "term": {
                        "ip": "192.168.0.1"
                     }
                  }
               }
            },
            {
               "nested": {
                  "path": "ips",
                  "filter": {
                     "term": {
                        "ip": "10.0.0.9"
                     }
                  }
               }
            }
         ]
      }
   }
}

这是我用来设置它的代码:

http://sense.qbox.io/gist/cd3a0ec61cb1348d5cc2bd1ef444f4898f6d8e57

关于elasticsearch - 嵌套对象的 boolean 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32205748/

相关文章:

serialization - 在 React Native 中将 boolean 值存储/保存在 AsyncStorage 中

amazon-web-services - 将索引模板上传到 aws elasticsearch 服务

python - geo_point映射python和StreamSets随Elasticsearch失败

javascript - 如何使用 jscript 断言来过滤 Elmah 1.1 RC 中的异常类型?

java - 在 Java 中将字符串数组转换为 boolean 列表

javascript - 或循环中 If 语句中的 boolean 表达式

php - Elasticsearch:使用无痛脚本获取对象索引

c# - 找出 ElasticLinq 实际上在做什么

javascript - AngularJS - 立即应用指令,格式化货币

c - 在 C 中实现简单的高通和低通滤波器