- 定义映射(需指定坐标系)
PUT /regions
{
"mappings": {
"properties": {
"name": { "type": "keyword" },
"area": {
"type": "geo_shape",
"tree": "quadtree", // 索引树类型(默认)
"precision": "100m" // 精度控制(越高越精确,存储越大)
}
}
}
}
- 插入地理形状数据
2.1 多边形(Polygon)
POST /regions/_doc/1
{
"name": "北京五环区域",
"area": {
"type": "polygon",
"coordinates": [
[
[116.20, 39.70], // 多边形顶点坐标(需闭合)
[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", // 查询点是否在区域内
"coordinates": [116.3975, 39.9087] // 天安门坐标
},
"relation": "intersects" // 关系类型:相交
}
}
}
}
// 结果:返回包含该点的区域(如北京五环区域)
3.2 使用预定义形状(引用索引中的形状)
POST /regions/_search
{
"query": {
"geo_shape": {
"area": {
"indexed_shape": {
"index": "regions", // 形状来源索引
"id": "1", // 文档ID
"path": "area" // 字段路径
},
"relation": "within" // 关系类型:完全包含在目标形状内
}
}
}
}