如何执行IP范围查询/过滤器 [英] How to execute an IP Range Query/Filter

查看:64
本文介绍了如何执行IP范围查询/过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取IP范围查询以处理一组文档,但没有任何结果.

I am trying to get an IP range query to work over a set of documents, and am getting no results.

映射(我已经尝试了 analyzed not_analyzed ):

Mapping (I've tried both analyzed and not_analyzed):

   "mappings": {
      "addy": {
         "properties": {
            "add": {
               "type": "ip",
               "not_analyzed":"true"
            }
         }
      }
   }

数据看起来像这样(它的许多实例具有不同的值)

The data looks like this (many instances of this with varying values)

   "_source": {
       "add": "192.168.1.15"
   }

现在,我去看了官方的ES文档,但是没有IP范围示例,但是我在Git上找到了一个,没用.它看起来如下:

Now, I went looking at the official ES docs, but there was not IP range example, but I found one on the Git , which didn't work. It looks as follows:

    "query": {
        "query_string": {
           "default_field": "add",
           "query": "add:[192.168.1.5 TO 192.168.1.15]"
        }
    }

当我用手指指着我的字段和地址时,上面的方法引发了一些鼓励性的解析错误,但最终没有返回结果.

The above threw some encourage parse errors when I was fat fingering my fields and addresses, but in the end returned no results.

我还尝试了标准范围语法:

I also tried standard range syntax:

"filter": {
    "range": {
       "add": {
          "from": "192.168.1.5",
          "to": "192.168.1.25"
       }
    }
}

也未返回任何结果.如何查询IP地址范围?

Which also returned no results. How do I query a range of IP addresses?

推荐答案

ip 类型不需要任何 not_analyzed 设置,仅适用于字符串字段.无论如何,我已经能够像这样重新创建您的索引:

The ip type doesn't take any not_analyzed setting, that's only for string fields. Anyway, I've been able to recreate your index like this:

curl -XPUT localhost:9200/addies -d '{
   "mappings": {
      "addy": {
          "properties":{
             "add": { "type": "ip"}
          }
      }
   }
}'

然后我创建了几个示例文档,如下所示:

Then I've created a couple sample documents like this:

curl -XPUT localhost:9200/addies/addy/1 -d '{"add": "192.168.1.100"}'
curl -XPUT localhost:9200/addies/addy/2 -d '{"add": "192.168.1.101"}'
curl -XPUT localhost:9200/addies/addy/3 -d '{"add": "192.168.1.102"}'
curl -XPUT localhost:9200/addies/addy/4 -d '{"add": "192.168.1.110"}'

最后,使用 query_string 查询,我只能像这样检索前三个文档:

And finally, using a query_string query I could retrieve only the first three documents like this:

curl -XPOST localhost:9200/addies/addy/_search -d '{
  "query": {
    "query_string": {
      "query": "add:[192.168.1.100 TO 192.168.1.102]"
    }
  }
}'

更新:

请注意,以下 range 查询也可以正常工作,并返回相同的结果:

Please note that the following range query also works fine and returns the same results:

curl -XPOST localhost:9200/addies/addy/_search -d '{
  "query": {
    "range": {
      "add": {
        "gte": "192.168.1.100",
        "lte": "192.168.1.102"
      }
    }
  }
}'

这篇关于如何执行IP范围查询/过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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