对于那些经常会在查询中使用的形状,可以把它们索引起来以便在查询中可以方便地直接引用名字。 以之前的阿姆斯特丹中央为例,我们可以把它存储为一个类型为 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": { <1>
"index": "attractions",
"type": "neighborhood",
"id": "central_amsterdam",
"path": "location"
}
}
}
}
}
指定 indexed_shape
而不是 shape
,Elasticesearch 就知道需要从指定的文档和路径检索出对应的形状了。
阿姆斯特丹中央这个形状没有什么特别的。同样地,我们也可以使用已经索引好的阿姆斯特丹达姆广场。 这个查询查找出与阿姆斯特丹达姆广场有交集的临近点:
GET /attractions/neighborhood/_search
{
"query": {
"geo_shape": {
"location": {
"indexed_shape": {
"index": "attractions",
"type": "landmark",
"id": "dam_square",
"path": "location"
}
}
}
}
}