示例:基于地理位置丰富数据
编辑示例:基于地理位置丰富数据
编辑geo_match
富化策略 使用 geo_shape
查询,根据地理位置将富化数据与传入文档进行匹配。
以下示例创建一个 geo_match
富化策略,该策略基于一组坐标将邮政编码添加到传入的文档。然后,它将 geo_match
富化策略添加到摄取管道中的处理器。
使用 创建索引 API 创建一个包含至少一个 geo_shape
字段的源索引。
resp = client.indices.create( index="postal_codes", mappings={ "properties": { "location": { "type": "geo_shape" }, "postal_code": { "type": "keyword" } } }, ) print(resp)
response = client.indices.create( index: 'postal_codes', body: { mappings: { properties: { location: { type: 'geo_shape' }, postal_code: { type: 'keyword' } } } } ) puts response
const response = await client.indices.create({ index: "postal_codes", mappings: { properties: { location: { type: "geo_shape", }, postal_code: { type: "keyword", }, }, }, }); console.log(response);
PUT /postal_codes { "mappings": { "properties": { "location": { "type": "geo_shape" }, "postal_code": { "type": "keyword" } } } }
使用 索引 API 将富化数据索引到此源索引中。
resp = client.index( index="postal_codes", id="1", refresh="wait_for", document={ "location": { "type": "envelope", "coordinates": [ [ 13, 53 ], [ 14, 52 ] ] }, "postal_code": "96598" }, ) print(resp)
response = client.index( index: 'postal_codes', id: 1, refresh: 'wait_for', body: { location: { type: 'envelope', coordinates: [ [ 13, 53 ], [ 14, 52 ] ] }, postal_code: '96598' } ) puts response
const response = await client.index({ index: "postal_codes", id: 1, refresh: "wait_for", document: { location: { type: "envelope", coordinates: [ [13, 53], [14, 52], ], }, postal_code: "96598", }, }); console.log(response);
PUT /postal_codes/_doc/1?refresh=wait_for { "location": { "type": "envelope", "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ] }, "postal_code": "96598" }
使用 创建富化策略 API 创建一个 geo_match
策略类型的富化策略。此策略必须包括:
- 一个或多个源索引
match_field
,即用于匹配传入文档的源索引中的geo_shape
字段- 您想要附加到传入文档的源索引中的富化字段
resp = client.enrich.put_policy( name="postal_policy", geo_match={ "indices": "postal_codes", "match_field": "location", "enrich_fields": [ "location", "postal_code" ] }, ) print(resp)
response = client.enrich.put_policy( name: 'postal_policy', body: { geo_match: { indices: 'postal_codes', match_field: 'location', enrich_fields: [ 'location', 'postal_code' ] } } ) puts response
const response = await client.enrich.putPolicy({ name: "postal_policy", geo_match: { indices: "postal_codes", match_field: "location", enrich_fields: ["location", "postal_code"], }, }); console.log(response);
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 创建一个摄取管道。在管道中,添加一个 富化处理器,其中包含:
- 您的富化策略。
- 用于匹配富化索引中文档的地理形状的传入文档的
field
。 - 用于存储传入文档附加的富化数据的
target_field
。此字段包含您的富化策略中指定的match_field
和enrich_fields
。 shape_relation
,它指示处理器如何将传入文档中的地理形状与富化索引中文档中的地理形状匹配。有关有效选项和更多信息,请参阅 空间关系。
resp = client.ingest.put_pipeline( id="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" } } ], ) print(resp)
const response = await client.ingest.putPipeline({ id: "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", }, }, ], }); console.log(response);
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
。
resp = client.index( index="users", id="0", pipeline="postal_lookup", document={ "first_name": "Mardy", "last_name": "Brown", "geo_location": "POINT (13.5 52.5)" }, ) print(resp)
const response = await client.index({ index: "users", id: 0, pipeline: "postal_lookup", document: { first_name: "Mardy", last_name: "Brown", geo_location: "POINT (13.5 52.5)", }, }); console.log(response);
PUT /users/_doc/0?pipeline=postal_lookup { "first_name": "Mardy", "last_name": "Brown", "geo_location": "POINT (13.5 52.5)" }
要验证富化处理器是否匹配并附加了相应的字段数据,请使用 获取 API 查看索引的文档。
resp = client.get( index="users", id="0", ) print(resp)
response = client.get( index: 'users', id: 0 ) puts response
const response = await client.get({ index: "users", id: 0, }); console.log(response);
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)" } }