Geo Bounding Box Query(地理边框查询)

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-geo-bounding-box-query.html

译文链接 : Geo Bounding Box Query(地理边框查询)

贡献者 : @yangbinApacheCNApache中文网

允许使用边框对基于点位置的点击进行过滤的查询。 假设以下索引文档:

  1. PUT /my_locations
  2. {
  3. "mappings": {
  4. "location": {
  5. "properties": {
  6. "pin": {
  7. "properties": {
  8. "location": {
  9. "type": "geo_point"
  10. }
  11. }
  12. }
  13. }
  14. }
  15. }
  16. }
  17. PUT /my_locations/location/1
  18. {
  19. "pin" : {
  20. "location" : {
  21. "lat" : 40.12,
  22. "lon" : -71.34
  23. }
  24. }
  25. }

然后可以使用 geo_bounding_box 过滤器执行以下简单查询:

  1. GET /_search
  2. {
  3. "query": {
  4. "bool" : {
  5. "must" : {
  6. "match_all" : {}
  7. },
  8. "filter" : {
  9. "geo_bounding_box" : {
  10. "pin.location" : {
  11. "top_left" : {
  12. "lat" : 40.73,
  13. "lon" : -74.1
  14. },
  15. "bottom_right" : {
  16. "lat" : 40.01,
  17. "lon" : -71.12
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }

Query Options(查询选项)

Option(选项) Description(描述)
_name(名称) 可选名称字段来标识过滤器。
ignore_malformed(忽略异常) [5.0.0] 设置为 true 以接受无效纬度或经度的地理点(默认值为假)。
validation_method(验证方法) 设置为 IGNORE_MALFORMED 以接受无效纬度或经度的地理点,设置为 COERCE 也尝试推断正确的纬度或经度。 (默认为 STRICT)。
type(类型) 设置为索引或内存之一,以定义此过滤器是否将在内存或索引中执行。 有关详细信息,请参阅下面的 Type 默认值是 Memory。

Accepted Formats(接受格式)

以同样的方式,geo_point 类型可以接受地理点的不同表示,过滤器也可以接受它:

Lat Lon As Properties(Lat Lon 作为属性)

  1. GET /_search
  2. {
  3. "query": {
  4. "bool" : {
  5. "must" : {
  6. "match_all" : {}
  7. },
  8. "filter" : {
  9. "geo_bounding_box" : {
  10. "pin.location" : {
  11. "top_left" : {
  12. "lat" : 40.73,
  13. "lon" : -74.1
  14. },
  15. "bottom_right" : {
  16. "lat" : 40.01,
  17. "lon" : -71.12
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }

Lat Lon As Array (Lat Lon 作为排列)

格式在 [lon,lat] 中,注意,这里的 lon / lat 的顺序是为了符合 GeoJSON

  1. GET /_search
  2. {
  3. "query": {
  4. "bool" : {
  5. "must" : {
  6. "match_all" : {}
  7. },
  8. "filter" : {
  9. "geo_bounding_box" : {
  10. "pin.location" : {
  11. "top_left" : [-74.1, 40.73],
  12. "bottom_right" : [-71.12, 40.01]
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

Lat Lon As String(Lat Lon 作为字符串)

格式在 lat,lon

  1. GET /_search
  2. {
  3. "query": {
  4. "bool" : {
  5. "must" : {
  6. "match_all" : {}
  7. },
  8. "filter" : {
  9. "geo_bounding_box" : {
  10. "pin.location" : {
  11. "top_left" : "40.73, -74.1",
  12. "bottom_right" : "40.01, -71.12"
  13. }
  14. }
  15. }
  16. }
  17. }

Geohash(地理散列)

  1. GET /_search
  2. {
  3. "query": {
  4. "bool" : {
  5. "must" : {
  6. "match_all" : {}
  7. },
  8. "filter" : {
  9. "geo_bounding_box" : {
  10. "pin.location" : {
  11. "top_left" : "dr5r9ydj2y73",
  12. "bottom_right" : "drj7teegpus6"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

Vertices(顶点)

边框的顶点可以由 top_left bottom_righttop_rightand bottom_left 参数设置。 更多的名称 topLeft,bottomRight,topRight和bottomLeft 是支持的。 而不是成对设置值,可以使用简单的名称顶部,左侧,底部和右侧单独设置值。

  1. GET /_search
  2. {
  3. "query": {
  4. "bool" : {
  5. "must" : {
  6. "match_all" : {}
  7. },
  8. "filter" : {
  9. "geo_bounding_box" : {
  10. "pin.location" : {
  11. "top" : 40.73,
  12. "left" : -74.1,
  13. "bottom" : 40.01,
  14. "right" : -71.12
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }

geo_point Type(geo_point类型)

过滤器需要在相关字段上设置 geo_point 类型。

Multi Location Per Document(每个文档的多个位置)

过滤器可以与每个文档的多个位置/点配合使用。 一旦单个位置/点与过滤器匹配,文档将被包含在过滤器中。

Type(类型)

默认情况下,边框执行的类型设置为内存,这意味着在内存中检查文档是否在边框范围内。 在某些情况下,索引选项的执行速度会更快(但是请注意,在这种情况下,geo_point 类型必须具有 latlon索引)。 请注意,使用索引选项时,不支持每个文档字段的多个位置。 这是一个例子:

  1. GET /_search
  2. {
  3. "query": {
  4. "bool" : {
  5. "must" : {
  6. "match_all" : {}
  7. },
  8. "filter" : {
  9. "geo_bounding_box" : {
  10. "pin.location" : {
  11. "top_left" : {
  12. "lat" : 40.73,
  13. "lon" : -74.1
  14. },
  15. "bottom_right" : {
  16. "lat" : 40.10,
  17. "lon" : -71.12
  18. }
  19. },
  20. "type" : "indexed"
  21. }
  22. }
  23. }
  24. }
  25. }

Ignore Unmapped(忽略未映射)

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