Geo Centroid Aggregation

edit

A 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 geo_centroid aggregation specifies the field to use for computing the centroid. (NOTE: field must be a Geo-point datatype 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:

{
    ...

    "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
              }
           }
        }
    ]
}