ElasticSearch:通过 logstash 填充 ip_range 类型字段

ElasticSearch: populating ip_range type field via logstash

我正在 ElasticSearch 6.8 (https://www.elastic.co/guide/en/elasticsearch/reference/6.8/range.html) 中试验 ip_range 字段类型,并努力寻找一种方法通过 logstash[ 将 ip 数据正确加载到字段中=39=]

我能够通过 Kibana Dev Tools 加载一些示例数据,但无法找到通过 logstash 执行相同操作的方法。

索引定义

PUT test_ip_range
{
  "mapping": {
    "_doc": {
      "properties": {
        "ip_from_to_range": {
          "type": "ip_range"
        },
        "ip_from": {
          "type": "ip"
        },
        "ip_to": {
          "type": "ip"
        }
      }
    }
  }
}

添加示例文档:

PUT test_ip_range/_doc/3
{
  "ip_from_to_range" : 
  {
    "gte" : "<dotted_ip_from>",
    "lte": "<dotted_ip_to>"
  }
}

Logstash 配置(从数据库读取)

input {
  jdbc {
  ...
  statement => "SELECT ip_from, ip_to, <???> AS ip_from_to_range FROM sample_ip_data"
  }
}
output {
  stdout { codec => json_lines }
  elasticsearch {
  "hosts" => "<host>"
  "index" => "test_ip_range"
  "document_type" => "_doc"
  }
}

问题:

如何通过 logstaship_fromip_to 数据库字段分别放入 ip_from_to_rangegtelte 部分配置??

我知道我也可以在 CIDR 表示法中插入 ip 范围,但希望能够同时拥有这两个选项 - 以 CIDR 表示法加载和作为范围加载。

经过反复试验,终于弄明白了 logstash 配置。

我已经发布了一个类似的问题 here,这最终让我在这个用例的语法方面也走上了正确的轨道。

input { ... }
filter {
  mutate {
    add_field => {
      "[ip_from_to_range]" => 
      '{
        "gte": "%{ip_from}",
        "lte": "%{ip_to}"
       }'
    }
  }
  json {
    source => "ip_from_to_range"
    target => "ip_from_to_range"
  }
}
output { ... }

过滤部分说明

  1. mutate add_field:创建一个新字段 [ip_from_to_range],其值为 json 字符串 ('{...}')。字段为[field_name]很重要,否则下一步将字符串解析成json对象是行不通的
  2. json:将字符串表示解析为json对象