形状查询
edit形状查询
edit类似于geo_shape,Elasticsearch支持索引任意二维(非地理空间)几何图形,使其能够绘制虚拟世界、体育场馆、主题公园和CAD图纸。
Elasticsearch 支持两种类型的笛卡尔数据:
point 字段,支持 x/y 对,以及
shape 字段,支持点、线、圆、多边形、多多边形等。
本组中的查询包括:
-
shapequery -
查找包含以下内容的文档:
-
shapes这些形状要么相交,要么被包含,要么在指定形状内,要么不相交 -
points这些点与指定形状相交
-
形状查询
edit查询包含使用 shape 类型索引的字段的文档。
需要 shape 映射。
查询支持两种定义目标形状的方式,一种是通过提供完整的形状定义,另一种是通过引用预先在另一个索引中索引的形状的名称或ID。下面通过示例定义了这两种格式。
内联形状定义
edit类似于 geo_shape 查询,shape 查询使用
GeoJSON 或
Well Known Text
(WKT) 来表示形状。
给定以下索引:
PUT /example
{
"mappings": {
"properties": {
"geometry": {
"type": "shape"
}
}
}
}
PUT /example/_doc/1?refresh=wait_for
{
"name": "Lucky Landing",
"geometry": {
"type": "point",
"coordinates": [ 1355.400544, 5255.530286 ]
}
}
以下查询将使用Elasticsearch的envelope GeoJSON扩展来查找点:
GET /example/_search
{
"query": {
"shape": {
"geometry": {
"shape": {
"type": "envelope",
"coordinates": [ [ 1355.0, 5355.0 ], [ 1400.0, 5200.0 ] ]
},
"relation": "within"
}
}
}
}
预索引形状
edit查询还支持使用已在另一个索引中索引的形状。这在您有一个预定义的形状列表对您的应用程序有用,并且您希望使用逻辑名称(例如新西兰)引用这些形状,而不是每次都提供它们的坐标时特别有用。在这种情况下,只需提供:
-
id- 包含预索引形状的文档的ID。 -
index- 预索引形状所在的索引名称。默认为shapes。 -
path- 指定为路径的字段,包含预索引形状。默认为shape。 -
routing- 如果需要,形状文档的路由。
以下是使用带有预索引形状的过滤器的示例:
PUT /shapes
{
"mappings": {
"properties": {
"geometry": {
"type": "shape"
}
}
}
}
PUT /shapes/_doc/footprint
{
"geometry": {
"type": "envelope",
"coordinates": [ [ 1355.0, 5355.0 ], [ 1400.0, 5200.0 ] ]
}
}
GET /example/_search
{
"query": {
"shape": {
"geometry": {
"indexed_shape": {
"index": "shapes",
"id": "footprint",
"path": "geometry"
}
}
}
}
}