示例:基于地理位置富化数据
geo_match enrich 策略使用 geo_shape 查询,根据地理位置将丰富数据匹配到传入文档。
以下示例创建了一个 geo_match enrich 策略,该策略根据一组坐标将邮政编码添加到传入文档中。然后,它将该 geo_match enrich 策略添加到写入管道(ingest pipeline)中的处理器。
使用 创建索引 API 创建一个包含至少一个 geo_shape 字段的源索引。
PUT /postal_codes
{
"mappings": {
"properties": {
"location": {
"type": "geo_shape"
},
"postal_code": {
"type": "keyword"
}
}
}
}
使用 索引 API 将丰富数据索引到此源索引中。
PUT /postal_codes/_doc/1?refresh=wait_for
{
"location": {
"type": "envelope",
"coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
},
"postal_code": "96598"
}
使用 创建 enrich 策略 API 来创建一个 geo_match 策略类型的 enrich 策略。此策略必须包含
- 一个或多个源索引
- 一个
match_field,即源索引中用于匹配传入文档的geo_shape字段 - 您希望附加到传入文档的源索引中的丰富字段
PUT /_enrich/policy/postal_policy
{
"geo_match": {
"indices": "postal_codes",
"match_field": "location",
"enrich_fields": [ "location", "postal_code" ]
}
}
使用 执行丰富策略 API 为该策略创建一个丰富索引。
POST /_enrich/policy/postal_policy/_execute?wait_for_completion=false
使用 创建或更新管道 API 创建一个摄取管道。在管道中,添加一个包含以下内容的 丰富处理器
- 您的丰富策略。
- 传入文档中用于匹配来自 enrich 索引的文档地理形状的
field。 - 用于存储附加到传入文档的丰富数据的
target_field。此字段包含您丰富策略中指定的match_field和enrich_fields。 shape_relation,它指示处理器如何将传入文档中的地理形状与来自 enrich 索引的文档中的地理形状进行匹配。有关有效选项和更多信息,请参阅 空间关系。
PUT /_ingest/pipeline/postal_lookup
{
"processors": [
{
"enrich": {
"description": "Add 'geo_data' based on 'geo_location'",
"policy_name": "postal_policy",
"field": "geo_location",
"target_field": "geo_data",
"shape_relation": "INTERSECTS"
}
}
]
}
使用摄取管道索引一个文档。传入的文档应包含您丰富处理器中指定的 field。
PUT /users/_doc/0?pipeline=postal_lookup
{
"first_name": "Mardy",
"last_name": "Brown",
"geo_location": "POINT (13.5 52.5)"
}
要验证丰富处理器是否匹配并附加了相应的字段数据,请使用 get API 查看已索引的文档。
GET /users/_doc/0
API 返回以下响应
{
"found": true,
"_index": "users",
"_id": "0",
"_version": 1,
"_seq_no": 55,
"_primary_term": 1,
"_source": {
"geo_data": {
"location": {
"type": "envelope",
"coordinates": [[13.0, 53.0], [14.0, 52.0]]
},
"postal_code": "96598"
},
"first_name": "Mardy",
"last_name": "Brown",
"geo_location": "POINT (13.5 52.5)"
}
}