2015-05-05 02:27:52 -04:00
|
|
|
[[query-dsl-geo-polygon-query]]
|
2019-07-18 10:18:11 -04:00
|
|
|
=== Geo-polygon query
|
|
|
|
++++
|
|
|
|
<titleabbrev>Geo-polygon</titleabbrev>
|
|
|
|
++++
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2018-11-06 01:49:18 -05:00
|
|
|
A query returning hits that only fall within a polygon of
|
2013-08-28 19:24:34 -04:00
|
|
|
points. Here is an example:
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"query": {
|
|
|
|
"bool": {
|
|
|
|
"must": {
|
|
|
|
"match_all": {}
|
|
|
|
},
|
|
|
|
"filter": {
|
|
|
|
"geo_polygon": {
|
|
|
|
"person.location": {
|
|
|
|
"points": [
|
|
|
|
{ "lat": 40, "lon": -70 },
|
|
|
|
{ "lat": 30, "lon": -80 },
|
|
|
|
{ "lat": 20, "lon": -90 }
|
|
|
|
]
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2015-08-07 16:55:22 -04:00
|
|
|
==== Query Options
|
|
|
|
|
|
|
|
[cols="<,<",options="header",]
|
|
|
|
|=======================================================================
|
|
|
|
|Option |Description
|
|
|
|
|`_name` |Optional name field to identify the filter
|
|
|
|
|
2016-04-28 07:56:50 -04:00
|
|
|
|`validation_method` |Set to `IGNORE_MALFORMED` to accept geo points with
|
|
|
|
invalid latitude or longitude, `COERCE` to try and infer correct latitude
|
|
|
|
or longitude, or `STRICT` (default is `STRICT`).
|
2015-08-18 06:16:49 -04:00
|
|
|
|=======================================================================
|
2015-08-07 16:55:22 -04:00
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2015-06-03 19:59:22 -04:00
|
|
|
==== Allowed Formats
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2015-06-03 19:59:22 -04:00
|
|
|
===== Lat Long as Array
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2018-11-06 01:49:18 -05:00
|
|
|
Format as `[lon, lat]`
|
|
|
|
|
|
|
|
Note: the order of lon/lat here must
|
2013-08-28 19:24:34 -04:00
|
|
|
conform with http://geojson.org/[GeoJSON].
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"query": {
|
|
|
|
"bool": {
|
|
|
|
"must": {
|
|
|
|
"match_all": {}
|
|
|
|
},
|
|
|
|
"filter": {
|
|
|
|
"geo_polygon": {
|
|
|
|
"person.location": {
|
|
|
|
"points": [
|
|
|
|
[ -70, 40 ],
|
|
|
|
[ -80, 30 ],
|
|
|
|
[ -90, 20 ]
|
|
|
|
]
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2015-06-03 19:59:22 -04:00
|
|
|
===== Lat Lon as String
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Format in `lat,lon`.
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"query": {
|
|
|
|
"bool": {
|
|
|
|
"must": {
|
|
|
|
"match_all": {}
|
|
|
|
},
|
|
|
|
"filter": {
|
|
|
|
"geo_polygon": {
|
|
|
|
"person.location": {
|
|
|
|
"points": [
|
|
|
|
"40, -70",
|
|
|
|
"30, -80",
|
|
|
|
"20, -90"
|
|
|
|
]
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2015-06-03 19:59:22 -04:00
|
|
|
===== Geohash
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"query": {
|
|
|
|
"bool": {
|
|
|
|
"must": {
|
|
|
|
"match_all": {}
|
|
|
|
},
|
|
|
|
"filter": {
|
|
|
|
"geo_polygon": {
|
|
|
|
"person.location": {
|
|
|
|
"points": [
|
|
|
|
"drn5x1g8cu2y",
|
|
|
|
"30, -80",
|
|
|
|
"20, -90"
|
|
|
|
]
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2015-06-03 19:59:22 -04:00
|
|
|
==== geo_point Type
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
The query *requires* the <<geo-point,`geo_point`>> type to be set on the
|
|
|
|
relevant field.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2016-04-14 06:38:48 -04:00
|
|
|
==== Ignore Unmapped
|
|
|
|
|
|
|
|
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.
|