Geo Centroid Aggregation 求地理位置中心点坐标值

一种度量聚合,用于计算地理点字段的所有坐标值的加权质心。

Example:

  1. PUT /museums
  2. {
  3. "mappings": {
  4. "properties": {
  5. "location": {
  6. "type": "geo_point"
  7. }
  8. }
  9. }
  10. }
  11. POST /museums/_bulk?refresh
  12. {"index":{"_id":1}}
  13. {"location": "52.374081,4.912350", "city": "Amsterdam", "name": "NEMO Science Museum"}
  14. {"index":{"_id":2}}
  15. {"location": "52.369219,4.901618", "city": "Amsterdam", "name": "Museum Het Rembrandthuis"}
  16. {"index":{"_id":3}}
  17. {"location": "52.371667,4.914722", "city": "Amsterdam", "name": "Nederlands Scheepvaartmuseum"}
  18. {"index":{"_id":4}}
  19. {"location": "51.222900,4.405200", "city": "Antwerp", "name": "Letterenhuis"}
  20. {"index":{"_id":5}}
  21. {"location": "48.861111,2.336389", "city": "Paris", "name": "Musée du Louvre"}
  22. {"index":{"_id":6}}
  23. {"location": "48.860000,2.327000", "city": "Paris", "name": "Musée d'Orsay"}
  24. POST /museums/_search?size=0
  25. {
  26. "aggs" : {
  27. "centroid" : {
  28. "geo_centroid" : {
  29. "field" : "location" @1
  30. }
  31. }
  32. }
  33. }

@1: geo_centroid聚合指定用于计算质心的字段。(注意:字段必须是地理点类型)

上述聚合演示了如何计算犯罪类型为入室盗窃的所有文档的位置字段的质心

上述聚合的响应:

  1. {
  2. ...
  3. "aggregations": {
  4. "centroid": {
  5. "location": {
  6. "lat": 51.00982965203002,
  7. "lon": 3.9662131341174245
  8. },
  9. "count": 6
  10. }
  11. }
  12. }

当作为子聚合与其他桶聚合组合时,geo_centroid聚合更有趣。

Example:

  1. POST /museums/_search?size=0
  2. {
  3. "aggs" : {
  4. "cities" : {
  5. "terms" : { "field" : "city.keyword" },
  6. "aggs" : {
  7. "centroid" : {
  8. "geo_centroid" : { "field" : "location" }
  9. }
  10. }
  11. }
  12. }
  13. }

上面的示例使用geo_centroid作为术语桶聚合的子聚合,用于查找每个城市中博物馆的中心位置。

上述聚合的响应:

  1. {
  2. ...
  3. "aggregations": {
  4. "cities": {
  5. "sum_other_doc_count": 0,
  6. "doc_count_error_upper_bound": 0,
  7. "buckets": [
  8. {
  9. "key": "Amsterdam",
  10. "doc_count": 3,
  11. "centroid": {
  12. "location": {
  13. "lat": 52.371655656024814,
  14. "lon": 4.909563297405839
  15. },
  16. "count": 3
  17. }
  18. },
  19. {
  20. "key": "Paris",
  21. "doc_count": 2,
  22. "centroid": {
  23. "location": {
  24. "lat": 48.86055548675358,
  25. "lon": 2.3316944623366
  26. },
  27. "count": 2
  28. }
  29. },
  30. {
  31. "key": "Antwerp",
  32. "doc_count": 1,
  33. "centroid": {
  34. "location": {
  35. "lat": 51.22289997059852,
  36. "lon": 4.40519998781383
  37. },
  38. "count": 1
  39. }
  40. }
  41. ]
  42. }
  43. }
  44. }