加载中

示例:通过匹配数值范围富化数据

range 丰富策略 使用 term 查询 将传入文档中的数字、日期或 IP 地址与丰富索引中相同类型的范围进行匹配。不支持将范围匹配到范围。

以下示例创建了一个 range 丰富策略,该策略根据 IP 地址将描述性网络名称和负责部门添加到传入文档中。然后,它将该丰富策略添加到摄取管道的处理器中。

使用 创建索引 API 和适当的映射来创建源索引。

				PUT /networks
					{
  "mappings": {
    "properties": {
      "range": { "type": "ip_range" },
      "name": { "type": "keyword" },
      "department": { "type": "keyword" }
    }
  }
}
		

以下索引 API 请求将一个新文档索引到该索引中。

				PUT /networks/_doc/1?refresh=wait_for
					{
  "range": "10.100.0.0/16",
  "name": "production",
  "department": "OPS"
}
		

使用创建丰富策略 API 来创建具有 range 策略类型的丰富策略。此策略必须包含

  • 一个或多个源索引
  • 一个 match_field,即源索引中用于匹配传入文档的字段
  • 您希望附加到传入文档的源索引中的丰富字段

由于我们计划根据 IP 地址来丰富文档,因此策略的 match_field 必须是 ip_range 字段。

				PUT /_enrich/policy/networks-policy
					{
  "range": {
    "indices": "networks",
    "match_field": "range",
    "enrich_fields": ["name", "department"]
  }
}
		

使用 执行丰富策略 API 为该策略创建一个丰富索引。

				POST /_enrich/policy/networks-policy/_execute?wait_for_completion=false
		

使用 创建或更新管道 API 创建一个摄取管道。在管道中,添加一个包含以下内容的 丰富处理器

  • 您的丰富策略。
  • 传入文档中用于匹配来自丰富索引中文档的 field
  • 用于存储附加到传入文档的丰富数据的 target_field。此字段包含您丰富策略中指定的 match_fieldenrich_fields
				PUT /_ingest/pipeline/networks_lookup
					{
  "processors" : [
    {
      "enrich" : {
        "description": "Add 'network' data based on 'ip'",
        "policy_name": "networks-policy",
        "field" : "ip",
        "target_field": "network",
        "max_matches": "10"
      }
    }
  ]
}
		

使用摄取管道索引一个文档。传入的文档应包含您丰富处理器中指定的 field

				PUT /my-index-000001/_doc/my_id?pipeline=networks_lookup
					{
  "ip": "10.100.34.1"
}
		

要验证丰富处理器是否匹配并附加了相应的字段数据,请使用 get API 查看已索引的文档。

				GET /my-index-000001/_doc/my_id
		

API 返回以下响应

{
  "_index" : "my-index-000001",
  "_id" : "my_id",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "ip" : "10.100.34.1",
    "network" : [
      {
        "name" : "production",
        "range" : "10.100.0.0/16",
        "department" : "OPS"
      }
    ]
  }
}
		
© . This website operates independently and is not affiliated with or endorsed by Elasticsearch B.V. All brand names, logos, and trademarks are the property of their respective owners.