Back to Elasticsearch

Geo-polygon query [query-dsl-geo-polygon-query]

docs/reference/query-languages/query-dsl/query-dsl-geo-polygon-query.md

9.4.03.4 KB
Original Source

Geo-polygon query [query-dsl-geo-polygon-query]

::::{admonition} Deprecated in 7.12. :class: warning

Use Geoshape instead where polygons are defined in GeoJSON or Well-Known Text (WKT). ::::

A query returning hits that only fall within a polygon of points. Here is an example:

console
GET /_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_polygon": {
          "person.location": {
            "points": [
              { "lat": 40, "lon": -70 },
              { "lat": 30, "lon": -80 },
              { "lat": 20, "lon": -90 }
            ]
          }
        }
      }
    }
  }
}

% TEST[warning:Deprecated field [geo_polygon] used, replaced by [[geo_shape] query where polygons are defined in geojson or wkt]]

Query options [_query_options_2]

OptionDescription
_nameOptional name field to identify the filter
validation_methodSet to IGNORE_MALFORMED to accept geo points withinvalid latitude or longitude, COERCE to try and infer correct latitudeor longitude, or STRICT (default is STRICT).

Allowed formats [_allowed_formats]

Lat long as array [_lat_long_as_array]

Format as [lon, lat]

Note: the order of lon/lat here must conform with GeoJSON.

console
GET /_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_polygon": {
          "person.location": {
            "points": [
              [ -70, 40 ],
              [ -80, 30 ],
              [ -90, 20 ]
            ]
          }
        }
      }
    }
  }
}

% TEST[warning:Deprecated field [geo_polygon] used, replaced by [[geo_shape] query where polygons are defined in geojson or wkt]]

Lat lon as string [_lat_lon_as_string_2]

Format in lat,lon.

console
GET /_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_polygon": {
          "person.location": {
            "points": [
              "40, -70",
              "30, -80",
              "20, -90"
            ]
          }
        }
      }
    }
  }
}

% TEST[warning:Deprecated field [geo_polygon] used, replaced by [[geo_shape] query where polygons are defined in geojson or wkt]]

Geohash [_geohash_4]

console
GET /_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_polygon": {
          "person.location": {
            "points": [
              "drn5x1g8cu2y",
              "30, -80",
              "20, -90"
            ]
          }
        }
      }
    }
  }
}

% TEST[warning:Deprecated field [geo_polygon] used, replaced by [[geo_shape] query where polygons are defined in geojson or wkt]]

geo_point type [_geo_point_type]

The query requires the geo_point type to be set on the relevant field.

Ignore unmapped [_ignore_unmapped_3]

When set to true the ignore_unmapped option will ignore an unmapped field and will not match any documents for this query. This can be useful when querying multiple indexes which might have different mappings. When set to false (the default value) the query will throw an exception if the field is not mapped.