嵌套对象的布尔过滤器 [英] Boolean filter on nested objects

查看:73
本文介绍了嵌套对象的布尔过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将布尔运算符与嵌套对象一起使用时,我遇到了一些麻烦. 这是我的映射:

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

我想获取的文件恰好包含两个指定的ip,甚至可能更多.

假设我的文档具有以下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.

解决方案

您可以在

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

http://sense.qbox.io/gist/d6b5f4e4c0d2977a04b1795f4bbb0503>

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

假设您的索引是按照我设置我的测试对象的方式设置的,那么该查询应该为您提供所需的信息(请参见下面的代码).在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/cd3a0ec61cb1348d5cc2bd1ef444f4898f6d8>

I'm having some troubles using boolean operators with nested objects. This is my mapping:

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

I want to get documents which contains exactly two specified ips and maybe more.

Suppose that my document has the followinh ips:

    DOC 1
        192.168.1.0
        192.168.0.1
        10.0.0.9

    DOC 2
       192.168.0.1

I want to retrieve only DOC 1 by searching with this filter:

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

The problem is that both DOC 1 and DOC2 are retrieved.

解决方案

You can use "execution":"and" on your terms filter like this:

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

Here is some code I used to test it:

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

EDIT: Nested version (I misunderstood the question).

This query should give you what you need, assuming your index is set up the way I set up mine for testing (see code below). There need to be two nested clauses in the must list since we are looking for a document that contains two different nested documents, one with each 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"
                     }
                  }
               }
            }
         ]
      }
   }
}

Here is the code I used to set it up:

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

这篇关于嵌套对象的布尔过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆