地理质心
geo_centroid
聚合计算一组 geo_point
值的地理中心或焦点。它将质心位置作为纬度-经度对返回。
参数
geo_centroid
聚合接受以下参数。
参数 | 必需/可选 | 数据类型 | 描述 |
---|---|---|---|
field | 必需 | 字符串 | 包含用于计算地理质心的地理点值的字段名称。 |
示例
以下示例返回电子商务样本数据中每个订单的 geoip.location
的 geo_centroid
。每个 geoip.location
都是一个地理点。
GET /opensearch_dashboards_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"centroid": {
"geo_centroid": {
"field": "geoip.location"
}
}
}
}
示例响应
响应包含一个 centroid
对象,其中包含表示所有已索引数据点的质心位置的 lat
和 lon
属性。
{
"took": 35,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4675,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"centroid": {
"location": {
"lat": 35.54990372113027,
"lon": -9.079764742533712
},
"count": 4675
}
}
}
质心位置位于摩洛哥以北大西洋。考虑到数据库中订单广泛的地理分布,这并没有太大的意义。
嵌套在其他聚合下
您可以将 geo_centroid
聚合嵌套在桶聚合中,以计算数据的子集的质心。
示例:在 terms 聚合下嵌套
您可以将 geo_centroid
聚合嵌套在字符串字段的 terms
桶下。
要查找每个大陆订单的 geoip
质心位置,请在 geoip.continent_name
字段中进行子聚合。
GET /opensearch_dashboards_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"continents": {
"terms": {
"field": "geoip.continent_name"
},
"aggs": {
"centroid": {
"geo_centroid": {
"field": "geoip.location"
}
}
}
}
}
}
这会返回每个大陆桶的质心位置。
响应
{
"took": 34,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4675,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"continents": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Asia",
"doc_count": 1220,
"centroid": {
"location": {
"lat": 28.023606536509163,
"lon": 47.83377046025068
},
"count": 1220
}
},
{
"key": "North America",
"doc_count": 1206,
"centroid": {
"location": {
"lat": 39.06542286878007,
"lon": -85.36152573149485
},
"count": 1206
}
},
{
"key": "Europe",
"doc_count": 1172,
"centroid": {
"location": {
"lat": 48.125767892293325,
"lon": 2.7529009746915243
},
"count": 1172
}
},
{
"key": "Africa",
"doc_count": 899,
"centroid": {
"location": {
"lat": 30.780756367941297,
"lon": 13.464182392125318
},
"count": 899
}
},
{
"key": "South America",
"doc_count": 178,
"centroid": {
"location": {
"lat": 4.599999985657632,
"lon": -74.10000007599592
},
"count": 178
}
}
]
}
}
}