2015-09-16 17:54:54 -04:00
|
|
|
[[search-aggregations-metrics-geocentroid-aggregation]]
|
|
|
|
=== Geo Centroid Aggregation
|
|
|
|
|
2017-04-17 21:27:43 -04:00
|
|
|
A metric aggregation that computes the weighted https://en.wikipedia.org/wiki/Centroid[centroid] from all coordinate values for a <<geo-point>> field.
|
2015-09-16 17:54:54 -04:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
[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", "city": "Amsterdam", "name": "NEMO Science Museum"}
|
|
|
|
{"index":{"_id":2}}
|
|
|
|
{"location": "52.369219,4.901618", "city": "Amsterdam", "name": "Museum Het Rembrandthuis"}
|
|
|
|
{"index":{"_id":3}}
|
|
|
|
{"location": "52.371667,4.914722", "city": "Amsterdam", "name": "Nederlands Scheepvaartmuseum"}
|
|
|
|
{"index":{"_id":4}}
|
|
|
|
{"location": "51.222900,4.405200", "city": "Antwerp", "name": "Letterenhuis"}
|
|
|
|
{"index":{"_id":5}}
|
|
|
|
{"location": "48.861111,2.336389", "city": "Paris", "name": "Musée du Louvre"}
|
|
|
|
{"index":{"_id":6}}
|
|
|
|
{"location": "48.860000,2.327000", "city": "Paris", "name": "Musée d'Orsay"}
|
|
|
|
|
|
|
|
POST /museums/_search?size=0
|
2015-09-16 17:54:54 -04:00
|
|
|
{
|
|
|
|
"aggs" : {
|
|
|
|
"centroid" : {
|
|
|
|
"geo_centroid" : {
|
|
|
|
"field" : "location" <1>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-03-30 21:19:07 -04:00
|
|
|
// CONSOLE
|
2015-09-16 17:54:54 -04:00
|
|
|
|
|
|
|
<1> The `geo_centroid` aggregation specifies the field to use for computing the centroid. (NOTE: field must be a <<geo-point>> type)
|
|
|
|
|
|
|
|
The above aggregation demonstrates how one would compute the centroid of the location field for all documents with a crime type of burglary
|
|
|
|
|
|
|
|
The response for the above aggregation:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
...
|
|
|
|
"aggregations": {
|
|
|
|
"centroid": {
|
|
|
|
"location": {
|
2017-04-18 09:17:21 -04:00
|
|
|
"lat": 51.00982963107526,
|
|
|
|
"lon": 3.9662130922079086
|
2017-04-28 10:25:22 -04:00
|
|
|
},
|
|
|
|
"count": 6
|
2015-09-16 17:54:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-03-30 21:19:07 -04:00
|
|
|
// TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
|
2015-09-16 17:54:54 -04:00
|
|
|
|
|
|
|
The `geo_centroid` aggregation is more interesting when combined as a sub-aggregation to other bucket aggregations.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-03-30 21:19:07 -04:00
|
|
|
POST /museums/_search?size=0
|
2015-09-16 17:54:54 -04:00
|
|
|
{
|
|
|
|
"aggs" : {
|
2017-03-30 21:19:07 -04:00
|
|
|
"cities" : {
|
|
|
|
"terms" : { "field" : "city.keyword" },
|
2015-09-16 17:54:54 -04:00
|
|
|
"aggs" : {
|
|
|
|
"centroid" : {
|
|
|
|
"geo_centroid" : { "field" : "location" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-03-30 21:19:07 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TEST[continued]
|
2015-09-16 17:54:54 -04:00
|
|
|
|
2017-03-30 21:19:07 -04:00
|
|
|
The above example uses `geo_centroid` as a sub-aggregation to a
|
|
|
|
<<search-aggregations-bucket-terms-aggregation, terms>> bucket aggregation
|
|
|
|
for finding the central location for museums in each city.
|
2015-09-16 17:54:54 -04:00
|
|
|
|
|
|
|
The response for the above aggregation:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
...
|
2017-03-30 21:19:07 -04:00
|
|
|
"aggregations": {
|
|
|
|
"cities": {
|
|
|
|
"sum_other_doc_count": 0,
|
|
|
|
"doc_count_error_upper_bound": 0,
|
|
|
|
"buckets": [
|
|
|
|
{
|
|
|
|
"key": "Amsterdam",
|
|
|
|
"doc_count": 3,
|
|
|
|
"centroid": {
|
|
|
|
"location": {
|
|
|
|
"lat": 52.371655656024814,
|
2017-04-18 09:17:21 -04:00
|
|
|
"lon": 4.909563297405839
|
2017-04-28 10:25:22 -04:00
|
|
|
},
|
|
|
|
"count": 3
|
2017-03-30 21:19:07 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "Paris",
|
|
|
|
"doc_count": 2,
|
|
|
|
"centroid": {
|
|
|
|
"location": {
|
2017-04-18 09:17:21 -04:00
|
|
|
"lat": 48.86055548675358,
|
|
|
|
"lon": 2.3316944623366
|
2017-04-28 10:25:22 -04:00
|
|
|
},
|
|
|
|
"count": 2
|
2017-03-30 21:19:07 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "Antwerp",
|
|
|
|
"doc_count": 1,
|
|
|
|
"centroid": {
|
|
|
|
"location": {
|
2017-04-18 09:17:21 -04:00
|
|
|
"lat": 51.22289997059852,
|
|
|
|
"lon": 4.40519998781383
|
2017-04-28 10:25:22 -04:00
|
|
|
},
|
|
|
|
"count": 1
|
2017-03-30 21:19:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2015-09-16 17:54:54 -04:00
|
|
|
}
|
2017-03-30 21:19:07 -04:00
|
|
|
}
|
2015-09-16 17:54:54 -04:00
|
|
|
}
|
2017-03-30 21:19:07 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
|