# 地理重心聚合(Geo Centroid Aggregation)

地理重心聚合是一个度量聚合，从文档中的地理数据类型（[Geo-point datatype](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/geo-point.html)）字段获取所有坐标值中计算出矩心（[centroid](https://en.wikipedia.org/wiki/Centroid)）。

例如：

```
{
    "query" : {
        "match" : { "crime" : "burglary" }
    },
    "aggs" : {
        "centroid" : {
            "geo_centroid" : {
                "field" : "location"   # 1
            } 
        }
    } 
}
```

| 1 | 地理重心聚合指定字段来计算矩心（注意：字段必须是地理数据类型） |
| - | ------------------------------- |

上面 的聚合表示查询所有犯罪类型为盗窃的文档，并根据文档中的location字段计算出矩心。

上面语句执行的返回值为：

```
{
    ...

    "aggregations": {
        "centroid": {
            "location": {
                "lat": 80.45,
                "lon": -160.22
            }
        }
    }
}
```

当地理重心聚合作为子聚合与分组聚合结合起来使用的时候会非常有趣，例如：

```
{
    "query" : {
        "match" : { "crime" : "burglary" }
    },
    "aggs" : {
        "towns" : {
            "terms" : { "field" : "town" },
            "aggs" : {
                "centroid" : {
                    "geo_centroid" : { "field" : "location" }
                }
            }
        }
    }
}
```

上面的实例用地理重心聚合作为索引分组聚合的子聚合来找出每个城镇中盗窃犯罪最严重的城镇的位置，返回值如下：

```
{
    ...

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/36aggregationsju-he-fen-679029/liang-du-ju-540828-metric-aggregations/di-li-zhong-xin-ju-540828-geo-centroid-aggregation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
