本地英文版地址: ../en/query-dsl-geo-shape-query.html
过滤使用geo_shape或geo_point类型索引的文档。
要求映射的数据类型为geo_shape或geo_point。
geo_shape查询使用与geo_shape映射相同的网格正方形表示来查找具有与查询形状相交的形状的文档。
它还将使用为字段映射定义的相同的前缀树(Prefix Tree)配置。
该查询支持两种定义查询形状的方法,要么提供完整的形状定义,要么引用另一个索引中预索引的形状的名称。 这两种格式都在下面用例子进行了定义。
与geo_shape类型相似,geo_shape查询使用GeoJSON来表示形状。
下面创建一个 location 字段为geo_shape类型的索引,并索引一个文档:
PUT /example
{
"mappings": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
# 新增并索引一个文档
POST /example/_doc?refresh
{
"name": "Wind & Wetter, Berlin, Germany",
"location": {
"type": "point",
"coordinates": [13.400544, 52.530286]
}
}
下面这个查询将使用Elasticsearch的envelopeGeoJSON扩展查找坐标点:
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"
}
}
}
}
}
}
类似地,可以在geo_point字段上执行上述查询。这里我们建立一个含 geo_point 字段的索引example_points,并索引一个文档:
PUT /example_points
{
"mappings": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
# 新增并索引一个文档
PUT /example_points/_doc/1?refresh
{
"name": "Wind & Wetter, Berlin, Germany",
"location": [13.400544, 52.530286]
}
使用相同的查询,返回具有匹配的geo_point字段的文档:
GET /example_points/_search
{
"query":{
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "envelope",
"coordinates" : [[13.0, 53.0], [14.0, 52.0]]
},
"relation": "intersects"
}
}
}
}
}
}
{
"took" : 17,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "example_points",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name": "Wind & Wetter, Berlin, Germany",
"location": [13.400544, 52.530286]
}
}
]
}
}
该查询还支持使用已经在另一个索引中索引了的形状。 当你有一个预定义的形状列表,并且想使用逻辑名称(例如 New Zealand)引用该列表,而不是每次都必须提供坐标时,特别有用。 在这种情况下,只需提供:
-
id- 预索引形状的文档ID。 -
index- 预索引形状所在的索引的名称。默认为shapes。 -
path- 指定包含预索引形状的路径的字段。默认为shape。 -
routing- 形状文档的路由(如果需要)。
下面是一个将过滤用于预索引形状的示例。我们先建立一个名为shapes的索引,然后索引一个文档进去,文档ID为deu。这里还用到了最上面建立的example索引。
# 建索引
PUT /shapes
{
"mappings": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
# 添加并索引一个文档,id为deu
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",
"id": "deu",
"path": "location"
}
}
}
}
}
}
}
geo_shape策略映射参数决定了在搜索时可以使用哪些空间关系运算符。
以下是搜索geo_shape类型的字段时可用的空间关系运算符的完整列表:
-
INTERSECTS- (默认) 返回geo_shape字段与查询几何形状 相交 的所有文档。 -
DISJOINT- 返回geo_shape字段与查询几何形状 完全不同 的所有文档。 -
WITHIN- 返回geo_shape字段 包含于 查询几何形状中的所有文档。 -
CONTAINS- 返回geo_shape字段 包含 查询几何形状中的所有文档。
搜索geo_point类型的字段时,有一个受支持的空间关系运算符:
-
INTERSECTS- (默认) 返回geo_point字段与查询几何形状 相交 的所有文档。
忽略未映射的字段 (ignore unmapped)
如果ignore_unmapped选项设置为true,将忽略未映射的字段,并且此查询不会匹配任何文档。
这在查询可能具有不同映射的多个索引时非常有用。
当设置为false(默认值)时,如果字段未映射,查询将抛出异常。
-
如果
search.allow_expensive_queries设置为false,则不会对用PrefixTrees实现的地理位置形状执行 geo_shape 查询。 -
当数据在
geo_shape字段中作为形状数组进行索引时,这些数组被视为一个形状。因此,下面的两个请求是等效的:
PUT /test/_doc/1
{
"location": [
{
"coordinates": [46.25,20.14],
"type": "point"
},
{
"coordinates": [47.49,19.04],
"type": "point"
}
]
}
PUT /test/_doc/1
{
"location":
{
"coordinates": [[46.25,20.14],[47.49,19.04]],
"type": "multipoint"
}
}