形状查询 geo_shape
查找与指定的地理形状相交、包含或不相交的地理形状的文档。
示例1. 查询形状内的文档
# 写入geo_shape索引
PUT /example
{
"mappings": {
"_doc": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
}
POST /example/_doc?refresh
{
"name": "Wind & Wetter, Berlin, Germany",
"location": {
"type": "point",
"coordinates": [13.400544, 52.530286]
}
}
# 查询在矩形内的所有文档
GET /example/_search
{
"query":{
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "envelope",
"coordinates" : [[13.0, 53.0], [14.0, 52.0]]
},
"relation": "within"
}
}
}
}
}
}
# 查询多边形内的所有文档
GET /example/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "polygon",
"coordinates": [
[
[48.854609,2.443977],
[48.854609,2.442428],
[48.855702,2.442198],
[48.855702,2.444138],
[48.854609,2.443977]
]
]
},
"relation": "within"
}
}
}
}
}
}
示例2. 查询与圆形相交的所有文档
PUT /circle-example
{
"mappings": {
"_doc": {
"properties": {
"location": {
"type": "geo_shape",
"strategy": "recursive"
}
}
}
}
}
POST /circle-example/_doc?refresh
{
"name": "Leting Sooh",
"location": {
"type" : "circle",
"coordinates" : [70.0, 1.0],
"radius" : "25mi"
}
}
GET /circle-example/_search
{
"query":{
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "circle",
"coordinates": [70.3, 1.2] ,
"radius":"2km"
},
"relation": "INTERSECTS"
}
}
}
}
}
}
引用查询
查询时引用一个包含geo_shape的索引文档做更简洁的形状查询。
例如输入一个索引,设置为上海市的地理位置形状, 当我们查询上海市内的文档时可以直接引用它而不需要每次都提供它的坐标经纬度。
以下是一个示例:
PUT /shapes
{
"mappings": {
"_doc": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
}
PUT /shapes/_doc/deu
{
"location": {
"type": "envelope",
"coordinates" : [[13.0, 53.0], [14.0, 52.0]]
}
}
GET /example/_search
{
"query": {
"bool": {
"filter": {
"geo_shape": {
"location": {
"indexed_shape": {
"index": "shapes",
"type": "_doc",
"id": "deu",
"path": "location"
}
}
}
}
}
}
}
参数说明
Ignore Unmapped
在查询具有不同mapping的多个索引时,ignore_unmapped 选项将忽略未映射的字段。当设置为 false(默认值)时,如果字段未映射,查询将抛出异常。
recursive 位置与形状的空间关系
- INTERSECTS 相交
- DISJOINT 不相交
- WITHIN 内
- CONTAINS 包含