Geo Centroid Aggregation

A metric aggregation that computes the weighted centroid from all coordinate values for geo fields.

Example:

  1. PUT /museums
  2. {
  3. "mappings": {
  4. "properties": {
  5. "location": {
  6. "type": "geo_point"
  7. }
  8. }
  9. }
  10. }
  11. POST /museums/_bulk?refresh
  12. {"index":{"_id":1}}
  13. {"location": "52.374081,4.912350", "city": "Amsterdam", "name": "NEMO Science Museum"}
  14. {"index":{"_id":2}}
  15. {"location": "52.369219,4.901618", "city": "Amsterdam", "name": "Museum Het Rembrandthuis"}
  16. {"index":{"_id":3}}
  17. {"location": "52.371667,4.914722", "city": "Amsterdam", "name": "Nederlands Scheepvaartmuseum"}
  18. {"index":{"_id":4}}
  19. {"location": "51.222900,4.405200", "city": "Antwerp", "name": "Letterenhuis"}
  20. {"index":{"_id":5}}
  21. {"location": "48.861111,2.336389", "city": "Paris", "name": "Musée du Louvre"}
  22. {"index":{"_id":6}}
  23. {"location": "48.860000,2.327000", "city": "Paris", "name": "Musée d'Orsay"}
  24. POST /museums/_search?size=0
  25. {
  26. "aggs": {
  27. "centroid": {
  28. "geo_centroid": {
  29. "field": "location"
  30. }
  31. }
  32. }
  33. }

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:

  1. {
  2. ...
  3. "aggregations": {
  4. "centroid": {
  5. "location": {
  6. "lat": 51.00982965203002,
  7. "lon": 3.9662131341174245
  8. },
  9. "count": 6
  10. }
  11. }
  12. }

The geo_centroid aggregation is more interesting when combined as a sub-aggregation to other bucket aggregations.

Example:

  1. POST /museums/_search?size=0
  2. {
  3. "aggs": {
  4. "cities": {
  5. "terms": { "field": "city.keyword" },
  6. "aggs": {
  7. "centroid": {
  8. "geo_centroid": { "field": "location" }
  9. }
  10. }
  11. }
  12. }
  13. }

The above example uses geo_centroid as a sub-aggregation to a terms bucket aggregation for finding the central location for museums in each city.

The response for the above aggregation:

  1. {
  2. ...
  3. "aggregations": {
  4. "cities": {
  5. "sum_other_doc_count": 0,
  6. "doc_count_error_upper_bound": 0,
  7. "buckets": [
  8. {
  9. "key": "Amsterdam",
  10. "doc_count": 3,
  11. "centroid": {
  12. "location": {
  13. "lat": 52.371655656024814,
  14. "lon": 4.909563297405839
  15. },
  16. "count": 3
  17. }
  18. },
  19. {
  20. "key": "Paris",
  21. "doc_count": 2,
  22. "centroid": {
  23. "location": {
  24. "lat": 48.86055548675358,
  25. "lon": 2.3316944623366
  26. },
  27. "count": 2
  28. }
  29. },
  30. {
  31. "key": "Antwerp",
  32. "doc_count": 1,
  33. "centroid": {
  34. "location": {
  35. "lat": 51.22289997059852,
  36. "lon": 4.40519998781383
  37. },
  38. "count": 1
  39. }
  40. }
  41. ]
  42. }
  43. }
  44. }

Geo Centroid Aggregation on geo_shape fields

The centroid metric for geo-shapes is more nuanced than for points. The centroid of a specific aggregation bucket containing shapes is the centroid of the highest-dimensionality shape type in the bucket. For example, if a bucket contains shapes comprising of polygons and lines, then the lines do not contribute to the centroid metric. Each type of shape’s centroid is calculated differently. Envelopes and circles ingested via the Circle are treated as polygons.

Geometry TypeCentroid Calculation

[Multi]Point

equally weighted average of all the coordinates

[Multi]LineString

a weighted average of all the centroids of each segment, where the weight of each segment is its length in degrees

[Multi]Polygon

a weighted average of all the centroids of all the triangles of a polygon where the triangles are formed by every two consecutive vertices and the starting-point. holes have negative weights. weights represent the area of the triangle in deg^2 calculated

GeometryCollection

The centroid of all the underlying geometries with the highest dimension. If Polygons and Lines and/or Points, then lines and/or points are ignored. If Lines and Points, then points are ignored

Example:

  1. PUT /places
  2. {
  3. "mappings": {
  4. "properties": {
  5. "geometry": {
  6. "type": "geo_shape"
  7. }
  8. }
  9. }
  10. }
  11. POST /places/_bulk?refresh
  12. {"index":{"_id":1}}
  13. {"name": "NEMO Science Museum", "geometry": "POINT(4.912350 52.374081)" }
  14. {"index":{"_id":2}}
  15. {"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 ] ] ] } }
  16. POST /places/_search?size=0
  17. {
  18. "aggs": {
  19. "centroid": {
  20. "geo_centroid": {
  21. "field": "geometry"
  22. }
  23. }
  24. }
  25. }
  1. {
  2. ...
  3. "aggregations": {
  4. "centroid": {
  5. "location": {
  6. "lat": 52.39296147599816,
  7. "lon": 4.967404240742326
  8. },
  9. "count": 2
  10. }
  11. }
  12. }

Using geo_centroid as a sub-aggregation of geohash_grid

The geohash_grid aggregation places documents, not individual geo-points, into buckets. If a document’s geo_point field contains multiple values, the document could be assigned to multiple buckets, even if one or more of its geo-points are outside the bucket boundaries.

If a geocentroid sub-aggregation is also used, each centroid is calculated using all geo-points in a bucket, including those outside the bucket boundaries. This can result in centroids outside of bucket boundaries.