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

相关推荐
bubble小拾6 小时前
ElasticSearch高级功能详解与读写性能调优
大数据·elasticsearch·搜索引擎
不能放弃治疗6 小时前
重生之我们在ES顶端相遇第 18 章 - Script 使用(进阶)
elasticsearch
hengzhepa7 小时前
ElasticSearch备考 -- Search across cluster
学习·elasticsearch·搜索引擎·全文检索·es
Elastic 中国社区官方博客9 小时前
Elasticsearch:使用 LLM 实现传统搜索自动化
大数据·人工智能·elasticsearch·搜索引擎·ai·自动化·全文检索
慕雪华年10 小时前
【WSL】wsl中ubuntu无法通过useradd添加用户
linux·ubuntu·elasticsearch
Elastic 中国社区官方博客12 小时前
使用 Vertex AI Gemini 模型和 Elasticsearch Playground 快速创建 RAG 应用程序
大数据·人工智能·elasticsearch·搜索引擎·全文检索
alfiy13 小时前
Elasticsearch学习笔记(四) Elasticsearch集群安全配置一
笔记·学习·elasticsearch
alfiy13 小时前
Elasticsearch学习笔记(五)Elastic stack安全配置二
笔记·学习·elasticsearch
丶21361 天前
【大数据】Elasticsearch 实战应用总结
大数据·elasticsearch·搜索引擎
闲人编程1 天前
elasticsearch实战应用
大数据·python·elasticsearch·实战应用