- 定義映射(需指定坐標(biāo)系)
PUT /regions
{
"mappings": {
"properties": {
"name": { "type": "keyword" },
"area": {
"type": "geo_shape",
"tree": "quadtree", // 索引樹類型(默認(rèn))
"precision": "100m" // 精度控制(越高越精確,存儲越大)
}
}
}
}
- 插入地理形狀數(shù)據(jù)
2.1 多邊形(Polygon)
POST /regions/_doc/1
{
"name": "北京五環(huán)區(qū)域",
"area": {
"type": "polygon",
"coordinates": [
[
[116.20, 39.70], // 多邊形頂點(diǎn)坐標(biāo)(需閉合)
[116.50, 39.70],
[116.50, 40.00],
[116.20, 40.00],
[116.20, 39.70]
]
]
}
}
2.2 圓形(Circle)
POST /regions/_doc/2
{
"name": "上海中心5km范圍",
"area": {
"type": "circle",
"radius": "5km", // 半徑
"coordinates": [121.4998, 31.2397] // 圓心
}
}
- 地理形狀查詢
3.1 查詢與某形狀相交的文檔
GET /regions/_search
{
"query": {
"geo_shape": {
"area": {
"shape": {
"type": "point", // 查詢點(diǎn)是否在區(qū)域內(nèi)
"coordinates": [116.3975, 39.9087] // 天安門坐標(biāo)
},
"relation": "intersects" // 關(guān)系類型:相交
}
}
}
}
// 結(jié)果:返回包含該點(diǎn)的區(qū)域(如北京五環(huán)區(qū)域)
3.2 使用預(yù)定義形狀(引用索引中的形狀)
POST /regions/_search
{
"query": {
"geo_shape": {
"area": {
"indexed_shape": {
"index": "regions", // 形狀來源索引
"id": "1", // 文檔ID
"path": "area" // 字段路徑
},
"relation": "within" // 關(guān)系類型:完全包含在目標(biāo)形狀內(nèi)
}
}
}
}