嵌套对象的布尔过滤器

Boolean filter on nested objects

我在对嵌套对象使用布尔运算符时遇到了一些问题。 这是我的映射:

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

我想获取恰好包含两个指定 ips 甚至更多的文档。

假设我的文档有以下ips:

    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

您可以将 "minimum_match" 与 "terms" 查询一起使用

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

这将return包含术语查询中所有值的记录。