【问题标题】:Aggregate objects in ElasticSearch by IP Prefix通过 IP 前缀聚合 ElasticSearch 中的对象
【发布时间】:2021-05-14 07:28:21
【问题描述】:

我有一个 ElasticSearch 索引,用于存储互联网流量对象,每个对象都包含一个 IP 地址。我想以一种将具有相同 IP 前缀的所有对象收集在同一个存储桶中的方式聚合数据(但不指定特定的前缀)。类似于直方图聚合的东西。这可能吗?

我试过这个:

GET flows/_search
{
  "size": 0,
  "aggs": {
    "ip_ranges": {
      "histogram": {
        "field": "ipAddress",
        "interval": 256
      }
    }
  }
}

但这不起作用,可能是因为 ip 类型字段不支持直方图聚合。你会怎么做呢?

【问题讨论】:

    标签: elasticsearch kibana ip-address elasticsearch-aggregation


    【解决方案1】:

    首先,按照here 的建议,最好的方法是:

    在索引时对IP地址进行分类,然后使用简单的关键字字段来存储c类信息,然后在该字段上使用术语聚合进行计数。

    或者,您可以简单地添加一个multi-field keyword mapping

    PUT myindex
    {
      "mappings": {
        "properties": {
          "ipAddress": {
            "type": "ip",
            "fields": {
              "keyword": {         <---
                "type": "keyword"
              }
            }
          }
        }
      }
    }
    

    然后在查询时提取前缀(⚠️非常低效!):

    GET myindex/_search
    {
      "size": 0,
      "aggs": {
        "my_prefixes": {
          "terms": {
            "script": "/\\./.split(doc['ipAddress.keyword'].value)[0]",
            "size": 10
          }
        }
      }
    }
    

    作为最后一个选项,您可以提前定义感兴趣的间隔并使用ip_range aggregation

    {
      "size": 0,
      "aggs": {
        "my_ip_ranges": {
          "ip_range": {
            "field": "ipAddress",
            "ranges": [
              { "to": "192.168.1.1" },
              { "from": "192.168.1.1" }
            ]
          }
        }
      }
    }
    

    【讨论】:

    • 非常感谢您回答这个问题!这很有帮助:)
    • 不客气!嘿,我的 Elasticsearch 手册很快就会出来——您可以 sign up here 告诉我除了 ES 中的正则表达式之外您还想了解什么。手册出来后我会通知你的!
    猜你喜欢
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多