2013-11-24 06:13:08 -05:00
[[search-aggregations-bucket-geodistance-aggregation]]
2014-05-12 19:35:58 -04:00
=== Geo Distance Aggregation
2013-11-24 06:13:08 -05:00
2013-12-17 10:55:25 -05:00
A multi-bucket aggregation that works on `geo_point` fields and conceptually works very similar to the <<search-aggregations-bucket-range-aggregation,range>> aggregation. The user can define a point of origin and a set of distance range buckets. The aggregation evaluate the distance of each document value from the origin point and determines the buckets it belongs to based on the ranges (a document belongs to a bucket if the distance between the document and the origin falls within the distance range of the bucket).
2013-11-24 06:13:08 -05:00
[source,js]
--------------------------------------------------
2017-03-30 21:19:07 -04:00
PUT /museums
{
"mappings": {
"doc": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
}
POST /museums/doc/_bulk?refresh
{"index":{"_id":1}}
{"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
{"index":{"_id":2}}
{"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
{"index":{"_id":3}}
{"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
{"index":{"_id":4}}
{"location": "51.222900,4.405200", "name": "Letterenhuis"}
{"index":{"_id":5}}
{"location": "48.861111,2.336389", "name": "Musée du Louvre"}
{"index":{"_id":6}}
{"location": "48.860000,2.327000", "name": "Musée d'Orsay"}
POST /museums/_search?size=0
2013-11-24 06:13:08 -05:00
{
"aggs" : {
"rings_around_amsterdam" : {
"geo_distance" : {
"field" : "location",
"origin" : "52.3760, 4.894",
"ranges" : [
2017-03-30 21:19:07 -04:00
{ "to" : 100000 },
{ "from" : 100000, "to" : 300000 },
{ "from" : 300000 }
2013-11-24 06:13:08 -05:00
]
}
}
}
}
--------------------------------------------------
2017-03-30 21:19:07 -04:00
// CONSOLE
2013-11-24 06:13:08 -05:00
Response:
[source,js]
--------------------------------------------------
{
2017-03-30 21:19:07 -04:00
...
2013-11-24 06:13:08 -05:00
"aggregations": {
2017-03-30 21:19:07 -04:00
"rings_around_amsterdam" : {
2014-01-28 11:46:26 -05:00
"buckets": [
{
2017-03-30 21:19:07 -04:00
"key": "*-100000.0",
"from": 0.0,
"to": 100000.0,
2014-01-28 11:46:26 -05:00
"doc_count": 3
},
{
2017-03-30 21:19:07 -04:00
"key": "100000.0-300000.0",
"from": 100000.0,
"to": 300000.0,
2014-01-28 11:46:26 -05:00
"doc_count": 1
},
{
2017-03-30 21:19:07 -04:00
"key": "300000.0-*",
"from": 300000.0,
"doc_count": 2
2014-01-28 11:46:26 -05:00
}
]
}
2013-11-24 06:13:08 -05:00
}
}
--------------------------------------------------
2017-03-30 21:19:07 -04:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
2013-11-24 06:13:08 -05:00
2015-08-06 11:24:29 -04:00
The specified field must be of type `geo_point` (which can only be set explicitly in the mappings). And it can also hold an array of `geo_point` fields, in which case all will be taken into account during aggregation. The origin point can accept all formats supported by the <<geo-point,`geo_point` type>>:
2013-11-24 06:13:08 -05:00
* Object format: `{ "lat" : 52.3760, "lon" : 4.894 }` - this is the safest format as it is the most explicit about the `lat` & `lon` values
* String format: `"52.3760, 4.894"` - where the first number is the `lat` and the second is the `lon`
* Array format: `[4.894, 52.3760]` - which is based on the `GeoJson` standard and where the first number is the `lon` and the second one is the `lat`
2017-03-30 21:19:07 -04:00
By default, the distance unit is `m` (meters) but it can also accept: `mi` (miles), `in` (inches), `yd` (yards), `km` (kilometers), `cm` (centimeters), `mm` (millimeters).
2013-11-24 06:13:08 -05:00
[source,js]
--------------------------------------------------
2017-03-30 21:19:07 -04:00
POST /museums/_search?size=0
2013-11-24 06:13:08 -05:00
{
"aggs" : {
"rings" : {
"geo_distance" : {
"field" : "location",
"origin" : "52.3760, 4.894",
2017-03-30 21:19:07 -04:00
"unit" : "km", <1>
2013-11-24 06:13:08 -05:00
"ranges" : [
{ "to" : 100 },
{ "from" : 100, "to" : 300 },
{ "from" : 300 }
]
}
}
}
}
--------------------------------------------------
2017-03-30 21:19:07 -04:00
// CONSOLE
// TEST[continued]
2013-11-24 06:13:08 -05:00
2017-03-30 21:19:07 -04:00
<1> The distances will be computed in kilometers
2013-11-24 06:13:08 -05:00
2016-08-05 19:29:01 -04:00
There are two distance calculation modes: `arc` (the default), and `plane`. The `arc` calculation is the most accurate. The `plane` is the fastest but least accurate. Consider using `plane` when your search context is "narrow", and spans smaller geographical areas (~5km). `plane` will return higher error margins for searches across very large areas (e.g. cross continent search). The distance calculation type can be set using the `distance_type` parameter:
2013-11-24 06:13:08 -05:00
[source,js]
--------------------------------------------------
2017-03-30 21:19:07 -04:00
POST /museums/_search?size=0
2013-11-24 06:13:08 -05:00
{
"aggs" : {
"rings" : {
"geo_distance" : {
"field" : "location",
"origin" : "52.3760, 4.894",
2017-03-30 21:19:07 -04:00
"unit" : "km",
2013-11-24 06:13:08 -05:00
"distance_type" : "plane",
"ranges" : [
{ "to" : 100 },
{ "from" : 100, "to" : 300 },
{ "from" : 300 }
]
}
}
}
}
2013-12-17 10:55:25 -05:00
--------------------------------------------------
2017-03-30 21:19:07 -04:00
// CONSOLE
// TEST[continued]
2017-04-18 09:57:50 -04:00
==== Keyed Response
Setting the `keyed` flag to `true` will associate a unique string key with each bucket and return the ranges as a hash rather than an array:
[source,js]
--------------------------------------------------
POST /museums/_search?size=0
{
"aggs" : {
"rings_around_amsterdam" : {
"geo_distance" : {
"field" : "location",
"origin" : "52.3760, 4.894",
"ranges" : [
{ "to" : 100000 },
{ "from" : 100000, "to" : 300000 },
{ "from" : 300000 }
],
"keyed": true
}
}
}
}
--------------------------------------------------
// CONSOLE
// TEST[continued]
Response:
[source,js]
--------------------------------------------------
{
...
"aggregations": {
"rings_around_amsterdam" : {
"buckets": {
"*-100000.0": {
"from": 0.0,
"to": 100000.0,
"doc_count": 3
},
"100000.0-300000.0": {
"from": 100000.0,
"to": 300000.0,
"doc_count": 1
},
"300000.0-*": {
"from": 300000.0,
"doc_count": 2
}
}
}
}
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
It is also possible to customize the key for each range:
[source,js]
--------------------------------------------------
POST /museums/_search?size=0
{
"aggs" : {
"rings_around_amsterdam" : {
"geo_distance" : {
"field" : "location",
"origin" : "52.3760, 4.894",
"ranges" : [
{ "to" : 100000, "key": "first_ring" },
{ "from" : 100000, "to" : 300000, "key": "second_ring" },
{ "from" : 300000, "key": "third_ring" }
],
"keyed": true
}
}
}
}
--------------------------------------------------
// CONSOLE
// TEST[continued]
Response:
[source,js]
--------------------------------------------------
{
...
"aggregations": {
"rings_around_amsterdam" : {
"buckets": {
"first_ring": {
"from": 0.0,
"to": 100000.0,
"doc_count": 3
},
"second_ring": {
"from": 100000.0,
"to": 300000.0,
"doc_count": 1
},
"third_ring": {
"from": 300000.0,
"doc_count": 2
}
}
}
}
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]