Elasticsearch:Geoshape query

Geoshape 查询可以用于过滤使用 geo_shapegeo_point 类型索引的文档。

geo_shape 查询使用与 geo_shape 或 geo_point 映射相同的索引来查找具有与查询形状相关的形状的文档,并使用指定的空间关系:相交(intersect)、包含(contained)、包含(within)或不相交 (disjoin)。

该查询支持两种定义查询形状的方法,一种是提供整个形状定义,另一种是引用在另一个索引中预先索引的形状的名称。 下面通过示例定义了这两种格式。

内联形状定义

与 geo_point 类型类似,geo_shape 查询使用 GeoJSON 来表示形状。有关如何制作 GeoJSON,请参考我的另外一篇文章 "Elasticsearch:如何制作 GeoJSON 文件并进行地理位置搜索"。

sample.json

markdown 复制代码
1.  {
2.      "type": "FeatureCollection",
3.      "features": [
4.      {
5.          "type": "Feature",
6.          "properties": {},
7.          "geometry": {
8.              "type": "Polygon",
9.              "coordinates": [
10.                  [
11.                      [ 
12.                        13.0,
13.                        52.0 
14.                      ],
15.                      [
16.                        14.0,
17.                        52.0
18.                      ],
19.                      [
20.                        14.0,
21.                        53.0
22.                      ],
23.                      [
24.                        13.0,
25.                        53.0
26.                      ],
27.                      [
28.                        13.0,
29.                        52.0
30.                      ]  
31.                    ]
32.              ]
33.          }
34.      }
35.  ]
36.  }

给定以下索引,其中位置作为 geo_shape 字段:

bash 复制代码
1.  PUT /example
2.  {
3.    "mappings": {
4.      "properties": {
5.        "location": {
6.          "type": "geo_shape"
7.        }
8.      }
9.    }
10.  }

12.  POST /example/_doc?refresh
13.  {
14.    "name": "Wind & Wetter, Berlin, Germany",
15.    "location": {
16.      "type": "point",
17.      "coordinates": [ 13.400544, 52.530286 ]
18.    }
19.  }

以下查询将使用 Elasticsearch 的信封 GeoJSON 扩展查找点:

bash 复制代码
1.  GET /example/_search
2.  {
3.    "query": {
4.      "bool": {
5.        "must": {
6.          "match_all": {}
7.        },
8.        "filter": {
9.          "geo_shape": {
10.            "location": {
11.              "shape": {
12.                "type": "envelope",
13.                "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
14.              },
15.              "relation": "within"
16.            }
17.          }
18.        }
19.      }
20.    }
21.  }

类似地,可以在 geo_point 字段上查询上述查询。

bash 复制代码
1.  PUT /example_points
2.  {
3.    "mappings": {
4.      "properties": {
5.        "location": {
6.          "type": "geo_point"
7.        }
8.      }
9.    }
10.  }

12.  PUT /example_points/_doc/1?refresh
13.  {
14.    "name": "Wind & Wetter, Berlin, Germany",
15.    "location": [13.400544, 52.530286]
16.  }

使用相同的查询,将返回具有匹配 geo_point 字段的文档。

markdown 复制代码
`

1.  GET /example_points/_search
2.  {
3.    "query": {
4.      "bool": {
5.        "must": {
6.          "match_all": {}
7.        },
8.        "filter": {
9.          "geo_shape": {
10.            "location": {
11.              "shape": {
12.                "type": "envelope",
13.                "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
14.              },
15.              "relation": "intersects"
16.            }
17.          }
18.        }
19.      }
20.    }
21.  }

`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

预索引形状

该查询还支持使用已在另一个索引中建立索引的形状。 当你有预定义的形状列表并且你想要使用逻辑名称(例如 New Zealand 新西兰)引用该列表而不是每次都提供坐标时,这特别有用。 在这种情况下,只需提供:

  • id - 包含预索引形状的文档的 ID。
  • index - 预索引形状所在的索引的名称。 默认为 shapes。
  • path - 指定为包含预索引形状的路径的字段。 默认为 shape。
  • routing - 形状文档的路由(如果需要)。

以下是将过滤器与预索引形状一起使用的示例:

lua 复制代码
1.  PUT /shapes
2.  {
3.    "mappings": {
4.      "properties": {
5.        "location": {
6.          "type": "geo_shape"
7.        }
8.      }
9.    }
10.  }

12.  PUT /shapes/_doc/deu
13.  {
14.    "location": {
15.      "type": "envelope",
16.      "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
17.    }
18.  }

20.  GET /example/_search
21.  {
22.    "query": {
23.      "bool": {
24.        "filter": {
25.          "geo_shape": {
26.            "location": {
27.              "indexed_shape": {
28.                "index": "shapes",
29.                "id": "deu",
30.                "path": "location"
31.              }
32.            }
33.          }
34.        }
35.      }
36.    }
37.  }

空间关系

以下是搜索地理字段时可用的空间关系运算符的完整列表:

  • INTERSECTS -(默认)返回 geo_shape 或 geo_point 字段与查询几何图形相交的所有文档。
  • DISJOINT - 返回其 geo_shape 或 geo_point 字段与查询几何图形没有任何共同点的所有文档。
  • WITHIN - 返回 geo_shape 或 geo_point 字段位于查询几何图形内的所有文档。 不支持线几何形状。
  • CONTAINS - 返回 geo_shape 或 geo_point 字段包含查询几何图形的所有文档。

忽略未映射的

当设置为 true 时,ignore_unmapped 选项将忽略未映射的字段,并且不会匹配此查询的任何文档。 当查询可能具有不同映射的多个索引时,这非常有用。 当设置为 false(默认值)时,如果字段未映射,查询将引发异常。

注意

当数据在 geo_shape 字段中作为形状数组进行索引时,这些数组将被视为一个形状。 因此,以下请求是等效的。

bash 复制代码
1.  PUT /test/_doc/1
2.  {
3.    "location": [
4.      {
5.        "coordinates": [46.25,20.14],
6.        "type": "point"
7.      },
8.      {
9.        "coordinates": [47.49,19.04],
10.        "type": "point"
11.      }
12.    ]
13.  }
lua 复制代码
1.  PUT /test/_doc/1
2.  {
3.    "location":
4.      {
5.        "coordinates": [[46.25,20.14],[47.49,19.04]],
6.        "type": "multipoint"
7.      }
8.  }

geo_shape 查询假定 geo_shape 字段使用默认方向 RIGHT(逆时针)。 请参见 Polygon orientation

相关推荐
Elasticsearch1 天前
ES8 向量功能窥探系列(二):向量数据的存储与优化
elasticsearch
安审若无1 天前
Elasticsearch中的监控(Monitoring)功能介绍
elasticsearch·搜索引擎·全文检索
leo_hush1 天前
elasticsearch基本操作笔记
elasticsearch
咸鱼求放生2 天前
es在Linux安装
大数据·elasticsearch·搜索引擎
xyhshen2 天前
k8s下离线搭建elasticsearch
elasticsearch·容器·kubernetes
@泽栖2 天前
ES数据聚合
elasticsearch·搜索引擎
張萠飛2 天前
Linux下如何使用shell脚本导出elasticsearch中某一个index的数据为本地csv文件
linux·运维·elasticsearch
LanLance2 天前
ES101系列09 | 运维、监控与性能优化
java·运维·后端·elasticsearch·云原生·性能优化·golang
疯狂的沙粒2 天前
如何通过git命令查看项目连接的仓库地址?
大数据·git·elasticsearch
IT成长日记2 天前
Elasticsearch集群手动分片分配指南:原理与实践
大数据·elasticsearch·手动分片分配