多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 地理重心聚合 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-aggregations-metrics-geocentroid-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-aggregations-metrics-geocentroid-aggregation.html) 译文链接 : [地理重心聚合](/pages/viewpage.action?pageId=9406913) 贡献者 : @wangxiang,[ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina) 地理重心聚合是一个度量聚合,从文档中的地理数据类型([Geo-point datatype](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/geo-point.html "Geo-point datatype"))字段获取所有坐标值中计算出矩心([centroid](https://en.wikipedia.org/wiki/Centroid))。 例如: ``` { "query" : { "match" : { "crime" : "burglary" } }, "aggs" : { "centroid" : { "geo_centroid" : { "field" : "location"   }  } }  } ``` 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 } } } ] } ```