Elasticsearch:Geoshape query - 过滤含有地理位置的文档

857 篇文章 560 订阅

Geoshape query 是用来过滤含有 geo_shape 或者 geo_point 类型的文档。在使用这个查询之前,你需要在你的索引 mapping 中显式地定义它们的数据类型,比如:

PUT /example
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

在上面,我们定义 location 的数据类型为 geo_shape。

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}

在上面,我们定义 location 的数据类型为 geo_point。

geo_shape 查询使用与 geo_shape 映射相同的方格表示来查找具有与查询形状相关的形状的文档,使用指定的空间关系:相交、包含、在内部或不相交。 它还将使用为字段映射定义的相同前缀树配置。

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

内置形状定义

与 geo_point 类型类似,geo_shape 查询使用 GeoJSON 来表示形状。

给定以下索引,位置为 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 的信封(envelope) GeoJSON 扩展找到该点:

GET /example/_search?filter_path=**.hits
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
            },
            "relation": "within"
          }
        }
      }
    }
  }
}

上面搜索的结果为:

{
  "hits": {
    "hits": [
      {
        "_index": "example",
        "_id": "N6ikhYIBT1laGVGsUcSa",
        "_score": 1,
        "_source": {
          "name": "Wind & Wetter, Berlin, Germany",
          "location": {
            "type": "point",
            "coordinates": [
              13.400544,
              52.530286
            ]
          }
        }
      }
    ]
  }
}

这个,我们可以从地图上进行显示:

很显然,坐标位置为 [ 13.400544, 52.530286 ] 的点处于上面的矩形框里。它能够被正确地搜索到。

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

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?filter_path=**.hits
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

上面查询的结果为:

{
  "hits": {
    "hits": [
      {
        "_index": "example_points",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "Wind & Wetter, Berlin, Germany",
          "location": [
            13.400544,
            52.530286
          ]
        }
      }
    ]
  }
}

预索引形状

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

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

以下是使用带有预索引形状的过滤器的示例:

PUT /shapes
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

PUT /shapes/_doc/deu
{
  "location": {
    "type": "envelope",
    "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
  }
}

在上面,我们创建了一个叫做 shapes 的索引。我们为它创建了一个 id 为 deu 的文档。它的显示区域就是上面地图中的黄色部分。我们接下来使用如下的搜索:

GET /example/_search
{
  "query": {
    "bool": {
      "filter": {
        "geo_shape": {
          "location": {
            "indexed_shape": {
              "index": "shapes",
              "id": "deu",
              "path": "location"
            }
          }
        }
      }
    }
  }
}

搜索的结果和之前的是一样的。只不过现在的 envelope 是定义在另外一个索引中,而不是之前在查询的语句中定义的。这样的做法在实际的使用中有很多意义,比如我可以使用一个索引定义需要经常查询的区域。我们在使用时,可以针对这些区域分别进行查询。我们无需每次查询时在查询语句中定义区域,取而代之的是一个比较有意义的 id 名称。这样使用更加人性化一些。

空间关系

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

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

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

注意:当数据在 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"
    }
}

geo_shape 查询假定 geo_shape 字段使用 RIGHT(逆时针)的默认方向。多边形的方向指示其顶点的顺序:RIGHT(逆时针)或 LEFT(顺时针)。 Elasticsearch 使用多边形的方向来确定它是否穿过国际日期变更线(+/-180° 经度)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值