geocentroid-aggregation.asciidoc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. [[search-aggregations-metrics-geocentroid-aggregation]]
  2. === Geo Centroid Aggregation
  3. A metric aggregation that computes the weighted centroid from all coordinate values for a <<geo-point>> field.
  4. Example:
  5. [source,js]
  6. --------------------------------------------------
  7. {
  8. "query" : {
  9. "match" : { "crime" : "burglary" }
  10. },
  11. "aggs" : {
  12. "centroid" : {
  13. "geo_centroid" : {
  14. "field" : "location" <1>
  15. }
  16. }
  17. }
  18. }
  19. --------------------------------------------------
  20. <1> The `geo_centroid` aggregation specifies the field to use for computing the centroid. (NOTE: field must be a <<geo-point>> type)
  21. The above aggregation demonstrates how one would compute the centroid of the location field for all documents with a crime type of burglary
  22. The response for the above aggregation:
  23. [source,js]
  24. --------------------------------------------------
  25. {
  26. ...
  27. "aggregations": {
  28. "centroid": {
  29. "location": {
  30. "lat": 80.45,
  31. "lon": -160.22
  32. }
  33. }
  34. }
  35. }
  36. --------------------------------------------------
  37. The `geo_centroid` aggregation is more interesting when combined as a sub-aggregation to other bucket aggregations.
  38. Example:
  39. [source,js]
  40. --------------------------------------------------
  41. {
  42. "query" : {
  43. "match" : { "crime" : "burglary" }
  44. },
  45. "aggs" : {
  46. "towns" : {
  47. "terms" : { "field" : "town" },
  48. "aggs" : {
  49. "centroid" : {
  50. "geo_centroid" : { "field" : "location" }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. --------------------------------------------------
  57. The above example uses `geo_centroid` as a sub-aggregation to a <<search-aggregations-bucket-terms-aggregation, terms>> bucket aggregation
  58. for finding the central location for all crimes of type burglary in each town.
  59. The response for the above aggregation:
  60. [source,js]
  61. --------------------------------------------------
  62. {
  63. ...
  64. "buckets": [
  65. {
  66. "key": "Los Altos",
  67. "doc_count": 113,
  68. "centroid": {
  69. "location": {
  70. "lat": 37.3924582824111,
  71. "lon": -122.12104808539152
  72. }
  73. }
  74. },
  75. {
  76. "key": "Mountain View",
  77. "doc_count": 92,
  78. "centroid": {
  79. "location": {
  80. "lat": 37.382152481004596,
  81. "lon": -122.08116559311748
  82. }
  83. }
  84. }
  85. ]
  86. }
  87. --------------------------------------------------