2014-05-29 05:37:03 -04:00
[[search-aggregations-metrics-geobounds-aggregation]]
=== Geo Bounds Aggregation
2020-06-18 15:36:24 -04:00
A metric aggregation that computes the bounding box containing all geo values for a field.
2014-05-29 05:37:03 -04:00
Example:
2019-09-05 10:11:25 -04:00
[source,console]
2014-05-29 05:37:03 -04:00
--------------------------------------------------
2019-01-18 08:11:18 -05:00
PUT /museums
2017-03-30 21:19:07 -04:00
{
"mappings": {
2019-01-18 08:11:18 -05:00
"properties": {
"location": {
"type": "geo_point"
2017-03-30 21:19:07 -04:00
}
}
}
}
2019-01-18 08:11:18 -05:00
POST /museums/_bulk?refresh
2017-03-30 21:19:07 -04:00
{"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
2014-05-29 05:37:03 -04:00
{
"query" : {
2017-03-30 21:19:07 -04:00
"match" : { "name" : "musée" }
2014-05-29 05:37:03 -04:00
},
"aggs" : {
"viewport" : {
"geo_bounds" : {
2015-01-19 08:10:53 -05:00
"field" : "location", <1>
"wrap_longitude" : true <2>
2014-05-29 05:37:03 -04:00
}
}
}
}
--------------------------------------------------
<1> The `geo_bounds` aggregation specifies the field to use to obtain the bounds
<2> `wrap_longitude` is an optional parameter which specifies whether the bounding box should be allowed to overlap the international date line. The default value is `true`
The above aggregation demonstrates how one would compute the bounding box of the location field for all documents with a business type of shop
The response for the above aggregation:
2019-09-06 16:09:09 -04:00
[source,console-result]
2014-05-29 05:37:03 -04:00
--------------------------------------------------
{
...
"aggregations": {
"viewport": {
"bounds": {
"top_left": {
2017-03-30 21:19:07 -04:00
"lat": 48.86111099738628,
"lon": 2.3269999679178
2014-05-29 05:37:03 -04:00
},
"bottom_right": {
2017-03-30 21:19:07 -04:00
"lat": 48.85999997612089,
"lon": 2.3363889567553997
2014-05-29 05:37:03 -04:00
}
}
}
}
}
--------------------------------------------------
2017-03-30 21:19:07 -04:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
2020-06-18 15:36:24 -04:00
[discrete]
[role="xpack"]
==== Geo Bounds Aggregation on `geo_shape` fields
The Geo Bounds Aggregation is also supported on `geo_shape` fields.
Example:
[source,console]
--------------------------------------------------
PUT /places
{
"mappings": {
"properties": {
"geometry": {
"type": "geo_shape"
}
}
}
}
POST /places/_bulk?refresh
{"index":{"_id":1}}
{"name": "NEMO Science Museum", "geometry": "POINT(4.912350 52.374081)" }
{"index":{"_id":2}}
{"name": "Sportpark De Weeren", "geometry": { "type": "Polygon", "coordinates": [ [ [ 4.965305328369141, 52.39347642069457 ], [ 4.966979026794433, 52.391721758934835 ], [ 4.969425201416015, 52.39238958618537 ], [ 4.967944622039794, 52.39420969150824 ], [ 4.965305328369141, 52.39347642069457 ] ] ] } }
POST /places/_search?size=0
{
"aggs" : {
"viewport" : {
"geo_bounds" : {
"field" : "geometry"
}
}
}
}
--------------------------------------------------
// TEST
[source,console-result]
--------------------------------------------------
{
...
"aggregations": {
"viewport": {
"bounds": {
"top_left": {
"lat": 52.39420966710895,
"lon": 4.912349972873926
},
"bottom_right": {
"lat": 52.374080987647176,
"lon": 4.969425117596984
}
}
}
}
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]