如何在 Elasticsearch 中查询 IP 范围?

How to query IP range in Elastic search?

我想在ELK中查询IP范围from:172.16.0.0到172.31.0.0

我尝试了两种查询方式,都失败了

{
  "query": {
    "bool": {
      "should": [
        {
          "regexp": {
            "DstIP": "172.(3[0-1]|1[6-9]|2[0-9]).*"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}
{
  "query": {
    "range": {
      "DstIP": {
        "gte": "172.16.0.0",
        "lte": "172.31.0.0"
      }
    }
  }
}

如何在ELK中查询IP范围?

要使范围查询在 IP 值上正常工作,必须将字段数据类型定义为 ip

下面是带有映射、示例文档和搜索查询的工作示例。

映射:

{
  "mappings": {
    "properties": {
      "dest": {
        "type": "ip"
      }
    }
  }
}

索引数据:

然后我拿了几个这样的示例文档:

{ "dest":"172.16.0.0"}
{ "dest":"172.31.0.0"}
{ "dest":"172.21.0.0"}
{ "dest":"172.1.0.0" }
{ "dest":"172.12.0.0"}

搜索查询:

{
  "query": {
    "range": {
      "dest": {
        "gte": "172.16.0.0",
        "lte": "172.31.0.0"
      }
    }
  }
}

搜索结果:

 "hits": [
         {
            "_index": "foo4",
            "_type": "_doc",
            "_id": "1",
            "_score": 1.0,
            "_source": {
               "dest": "172.16.0.0"
            }
         },
         {
            "_index": "foo4",
            "_type": "_doc",
            "_id": "2",
            "_score": 1.0,
            "_source": {
               "dest": "172.31.0.0"
            }
         },
         {
            "_index": "foo4",
            "_type": "_doc",
            "_id": "3",
            "_score": 1.0,
            "_source": {
               "dest": "172.21.0.0"
            }
         }
      ]