WARNING: Version 2.3 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Geo Centroid Aggregation
editGeo Centroid Aggregation
editA metric aggregation that computes the weighted centroid from all coordinate values for a Geo-point datatype field.
Example:
{ "query" : { "match" : { "crime" : "burglary" } }, "aggs" : { "centroid" : { "geo_centroid" : { "field" : "location" } } } }
The |
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:
{ ... "aggregations": { "centroid": { "location": { "lat": 80.45, "lon": -160.22 } } } }
The geo_centroid
aggregation is more interesting when combined as a sub-aggregation to other bucket aggregations.
Example:
{ "query" : { "match" : { "crime" : "burglary" } }, "aggs" : { "towns" : { "terms" : { "field" : "town" }, "aggs" : { "centroid" : { "geo_centroid" : { "field" : "location" } } } } } }
The above example uses geo_centroid
as a sub-aggregation to a terms bucket aggregation
for finding the central location for all crimes of type burglary in each town.
The response for the above aggregation:
{ ... "buckets": [ { "key": "Los Altos", "doc_count": 113, "centroid": { "location": { "lat": 37.3924582824111, "lon": -122.12104808539152 } } }, { "key": "Mountain View", "doc_count": 92, "centroid": { "location": { "lat": 37.382152481004596, "lon": -122.08116559311748 } } } ] }