Elasticsearch Geo Centroid Aggregation - Syntax, Example, and Tips

Pulse - Elasticsearch Operations Done Right

On this page

Syntax and Documentation Example Usage Common Issues Best Practices Frequently Asked Questions

This aggregation calculates the average position (centroid) of all geo_point values within the aggregation context. It's useful for finding the central point of a set of geographical coordinates, which can be helpful in various geospatial analysis scenarios.

Syntax and Documentation

The basic syntax for a Geo Centroid Aggregation is:

{
  "aggs": {
    "centroid": {
      "geo_centroid": {
        "field": "location"
      }
    }
  }
}

For more detailed information, refer to the official Elasticsearch documentation on Geo Centroid Aggregation.

Example Usage

Here's an example of how you might use the Geo Centroid Aggregation to find the central point of all stores in a particular city:

GET /stores/_search
{
  "size": 0,
  "aggs": {
    "city_centroid": {
      "filter": {
        "term": { "city": "New York" }
      },
      "aggs": {
        "centroid": {
          "geo_centroid": {
            "field": "location"
          }
        }
      }
    }
  }
}

This query will return the central point (centroid) of all store locations in New York City.

Common Issues

  1. Missing or null values: If the field contains missing or null values, they will be ignored in the calculation.
  2. Non-geo_point fields: The aggregation will fail if used on a field that is not of type geo_point.
  3. Empty buckets: If used within a bucket aggregation that produces empty buckets, those buckets will have null values for the centroid.

Best Practices

  1. Ensure that the field you're aggregating on is properly mapped as a geo_point type.
  2. Consider using this aggregation in combination with other geo aggregations for more comprehensive geospatial analysis.
  3. When dealing with a large number of points, be aware that the centroid might not always be the most representative central point, especially if the points are not evenly distributed.

Frequently Asked Questions

Q: Can I use Geo Centroid Aggregation with nested fields?
A: Yes, you can use Geo Centroid Aggregation with nested fields. You'll need to use a nested aggregation first, then apply the geo_centroid aggregation to the nested field.

Q: How does Geo Centroid Aggregation handle points across the 180th meridian?
A: Elasticsearch handles points across the 180th meridian correctly when calculating the centroid. It uses a spherical model of the Earth for these calculations.

Q: Can I weight certain points more heavily in the centroid calculation?
A: The standard Geo Centroid Aggregation doesn't support weighting. However, you could potentially achieve this by duplicating important points or using a script to modify the calculation.

Q: What's the difference between Geo Centroid and Geo Bounds aggregation?
A: Geo Centroid calculates a single central point, while Geo Bounds calculates a bounding box that contains all the points in the set.

Q: Is Geo Centroid Aggregation affected by the choice of map projection?
A: Elasticsearch uses a spherical model for geo calculations, which is generally sufficient for most use cases. For extremely precise calculations near the poles, you might need to consider the effects of map projections.

Subscribe to the Pulse Newsletter

Get early access to new Pulse features, insightful blogs & exclusive events , webinars, and workshops.