在查询中使用已索引的形状 | Elasticsearch: 权威指南 | Elastic
2024-11-13
对于那些经常会在查询中使用的形状,可以把它们索引起来以便在查询中可以方便地直接引用名字。
以之前的阿姆斯特丹中部为例,我们可以把它存储成一个类型为 neighborhood
的文档。
首先,我们仿照之前设置 landmark
时的方式建立映射:
PUT /attractions/_mapping/neighborhood { "properties": { "name": { "type": "string" }, "location": { "type": "geo_shape" } } }
然后我们索引阿姆斯特丹中部对应的形状:
PUT /attractions/neighborhood/central_amsterdam { "name" : "Central Amsterdam", "location" : { "type" : "polygon", "coordinates" : [[ [4.88330,52.38617], [4.87463,52.37254], [4.87875,52.36369], [4.88939,52.35850], [4.89840,52.35755], [4.91909,52.36217], [4.92656,52.36594], [4.93368,52.36615], [4.93342,52.37275], [4.92690,52.37632], [4.88330,52.38617] ]] } }
形状索引好之后,我们就可以在查询中通过 index
, type
和 id
来引用它了:
GET /attractions/landmark/_search { "query": { "geo_shape": { "location": { "relation": "within", "indexed_shape": { "index": "attractions", "type": "neighborhood", "id": "central_amsterdam", "path": "location" } } } } }
阿姆斯特丹中部这个形状没有什么特别的。同样地,我们也可以在查询中使用已经索引好的达姆广场。这个查询可以找出与达姆广场有交集的临近点:
GET /attractions/neighborhood/_search { "query": { "geo_shape": { "location": { "indexed_shape": { "index": "attractions", "type": "landmark", "id": "dam_square", "path": "location" } } } } }
官方地址:https://www.elastic.co/guide/cn/elasticsearch/guide/current/indexed-geo-shapes.html