對(duì)于那些經(jīng)常會(huì)在查詢中使用的形狀,可以把它們索引起來(lái)以便在查詢中可以方便地直接引用名字。
以之前的阿姆斯特丹中央為例,我們可以把它存儲(chǔ)為一個(gè)類型為 neighborhood
的文檔。
首先,我們仿照之前設(shè)置 landmark
時(shí)的方式建立一個(gè)映射:
PUT /attractions/_mapping/neighborhood
{
"properties": {
"name": {
"type": "string"
},
"location": {
"type": "geo_shape"
}
}
}
然后我們索引阿姆斯特丹中央對(duì)應(yīng)的形狀:
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]
]]
}
}
形狀索引好之后,我們就可以在查詢中通過(guò) index
, type
和 id
來(lái)引用它了:
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 就知道需要從指定的文檔和路徑檢索出對(duì)應(yīng)的形狀了。 阿姆斯特丹中央這個(gè)形狀沒有什么特別的。同樣地,我們也可以使用已經(jīng)索引好的阿姆斯特丹達(dá)姆廣場(chǎng)。 這個(gè)查詢查找出與阿姆斯特丹達(dá)姆廣場(chǎng)有交集的臨近點(diǎn): ```json GET /attractions/neighborhood/_search { "query": { "geo_shape": { "location": { "indexed_shape": { "index": "attractions", "type": "landmark", "id": "dam_square", "path": "location" } } } } } ```