2015-05-05 02:27:52 -04:00
|
|
|
[[query-dsl-geo-distance-query]]
|
2015-06-03 19:59:22 -04:00
|
|
|
=== Geo Distance Query
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Filters documents that include only hits that exists within a specific
|
2016-05-24 05:58:43 -04:00
|
|
|
distance from a geo point. Assuming the following mapping and indexed
|
|
|
|
document:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
PUT /my_locations
|
|
|
|
{
|
|
|
|
"mappings": {
|
2017-12-14 11:47:53 -05:00
|
|
|
"_doc": {
|
2016-05-24 05:58:43 -04:00
|
|
|
"properties": {
|
|
|
|
"pin": {
|
|
|
|
"properties": {
|
|
|
|
"location": {
|
|
|
|
"type": "geo_point"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT /my_locations/_doc/1
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
|
|
|
"pin" : {
|
|
|
|
"location" : {
|
|
|
|
"lat" : 40.12,
|
|
|
|
"lon" : -71.34
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TESTSETUP
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Then the following simple query can be executed with a `geo_distance`
|
|
|
|
filter:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
GET /my_locations/_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"bool" : {
|
|
|
|
"must" : {
|
|
|
|
"match_all" : {}
|
|
|
|
},
|
|
|
|
"filter" : {
|
|
|
|
"geo_distance" : {
|
|
|
|
"distance" : "200km",
|
|
|
|
"pin.location" : {
|
|
|
|
"lat" : 40,
|
|
|
|
"lon" : -70
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-08 07:50:40 -05:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[float]
|
2015-06-03 19:59:22 -04:00
|
|
|
==== Accepted Formats
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
In much the same way the `geo_point` type can accept different
|
2016-10-05 08:54:32 -04:00
|
|
|
representations of the geo point, the filter can accept it as well:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[float]
|
2015-06-03 19:59:22 -04:00
|
|
|
===== Lat Lon As Properties
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
GET /my_locations/_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"bool" : {
|
|
|
|
"must" : {
|
|
|
|
"match_all" : {}
|
|
|
|
},
|
|
|
|
"filter" : {
|
|
|
|
"geo_distance" : {
|
|
|
|
"distance" : "12km",
|
|
|
|
"pin.location" : {
|
|
|
|
"lat" : 40,
|
|
|
|
"lon" : -70
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[float]
|
2015-06-03 19:59:22 -04:00
|
|
|
===== Lat Lon As Array
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Format in `[lon, lat]`, note, the order of lon/lat here in order to
|
|
|
|
conform with http://geojson.org/[GeoJSON].
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
GET /my_locations/_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"bool" : {
|
|
|
|
"must" : {
|
|
|
|
"match_all" : {}
|
|
|
|
},
|
|
|
|
"filter" : {
|
|
|
|
"geo_distance" : {
|
|
|
|
"distance" : "12km",
|
|
|
|
"pin.location" : [-70, 40]
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[float]
|
2015-06-03 19:59:22 -04:00
|
|
|
===== Lat Lon As String
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Format in `lat,lon`.
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
GET /my_locations/_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"bool" : {
|
|
|
|
"must" : {
|
|
|
|
"match_all" : {}
|
|
|
|
},
|
|
|
|
"filter" : {
|
|
|
|
"geo_distance" : {
|
|
|
|
"distance" : "12km",
|
|
|
|
"pin.location" : "40,-70"
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[float]
|
2015-06-03 19:59:22 -04:00
|
|
|
===== Geohash
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
GET /my_locations/_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"bool" : {
|
|
|
|
"must" : {
|
|
|
|
"match_all" : {}
|
|
|
|
},
|
|
|
|
"filter" : {
|
|
|
|
"geo_distance" : {
|
|
|
|
"distance" : "12km",
|
|
|
|
"pin.location" : "drm3btev3e86"
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[float]
|
2015-06-03 19:59:22 -04:00
|
|
|
==== Options
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The following are options allowed on the filter:
|
|
|
|
|
2013-11-08 07:50:40 -05:00
|
|
|
[horizontal]
|
|
|
|
|
|
|
|
`distance`::
|
|
|
|
|
|
|
|
The radius of the circle centred on the specified location. Points which
|
|
|
|
fall into this circle are considered to be matches. The `distance` can be
|
|
|
|
specified in various units. See <<distance-units>>.
|
|
|
|
|
|
|
|
`distance_type`::
|
|
|
|
|
2016-08-05 19:29:01 -04:00
|
|
|
How to compute the distance. Can either be `arc` (default), or `plane` (faster, but inaccurate on long distances and close to the poles).
|
2013-11-08 07:50:40 -05:00
|
|
|
|
2015-08-07 16:55:22 -04:00
|
|
|
`_name`::
|
|
|
|
|
|
|
|
Optional name field to identify the query
|
|
|
|
|
2016-04-28 08:06:27 -04:00
|
|
|
`validation_method`::
|
2015-08-07 16:55:22 -04:00
|
|
|
|
2016-04-28 08:06:27 -04:00
|
|
|
Set to `IGNORE_MALFORMED` to accept geo points with invalid latitude or
|
|
|
|
longitude, set to `COERCE` to additionally try and infer correct
|
|
|
|
coordinates (default is `STRICT`).
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[float]
|
2015-06-03 19:59:22 -04:00
|
|
|
==== geo_point Type
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The filter *requires* the `geo_point` type to be set on the relevant
|
|
|
|
field.
|
|
|
|
|
|
|
|
[float]
|
2015-06-03 19:59:22 -04:00
|
|
|
==== Multi Location Per Document
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The `geo_distance` filter can work with multiple locations / points per
|
|
|
|
document. Once a single location / point matches the filter, the
|
|
|
|
document will be included in the filter.
|
|
|
|
|
2016-04-14 06:38:48 -04:00
|
|
|
[float]
|
|
|
|
==== 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.
|