Geo Polygon Query(地理多边形查询)

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

译文链接 :Geo Polygon Query(地理多边形查询)

贡献者 : @yangbinApacheCNApache中文网

一个查询,允许包含仅在点的多边形内的命中。 这是一个例子:

  1. GET /_search
  2. {
  3. "query": {
  4. "bool" : {
  5. "must" : {
  6. "match_all" : {}
  7. },
  8. "filter" : {
  9. "geo_polygon" : {
  10. "person.location" : {
  11. "points" : [
  12. {"lat" : 40, "lon" : -70},
  13. {"lat" : 30, "lon" : -80},
  14. {"lat" : 20, "lon" : -90}
  15. ]
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }

Query Options(查询选项)

Option(选项) Description(描述)
_name(名称) 可选名称字段来标识过滤器
ignore_malformed(忽略格式错误) [5.0.0] 设置为 true 以接受无效纬度或经度的地理点(默认值为假)。
validation_method(验证方法) 设置为 IGNORE_MALFORMED 以接受无效纬度或经度的地理点,COERCE 尝试推断正确的纬度或经度,或 STRICT(默认为 STRICT)。

Allowed Formats(允许格式)

Lat Long as Array(Lat Long 作为阵列)

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

  1. GET /_search
  2. {
  3. "query": {
  4. "bool" : {
  5. "must" : {
  6. "match_all" : {}
  7. },
  8. "filter" : {
  9. "geo_polygon" : {
  10. "person.location" : {
  11. "points" : [
  12. [-70, 40],
  13. [-80, 30],
  14. [-90, 20]
  15. ]
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }

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_polygon" : {
  10. "person.location" : {
  11. "points" : [
  12. "40, -70",
  13. "30, -80",
  14. "20, -90"
  15. ]
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }

Geohash(地理散列)

  1. GET /_search
  2. {
  3. "query": {
  4. "bool" : {
  5. "must" : {
  6. "match_all" : {}
  7. },
  8. "filter" : {
  9. "geo_polygon" : {
  10. "person.location" : {
  11. "points" : [
  12. "drn5x1g8cu2y",
  13. "30, -80",
  14. "20, -90"
  15. ]
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }

geo_point Type(geo_point类型)

该查询需要在相关字段上设置 geo_point 类型。

Ignore Unmapped(忽略未映射)

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