geocentroid-aggregation.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. [[search-aggregations-metrics-geocentroid-aggregation]]
  2. === Geo Centroid Aggregation
  3. A metric aggregation that computes the weighted https://en.wikipedia.org/wiki/Centroid[centroid] from all coordinate values for a <<geo-point>> field.
  4. Example:
  5. [source,console]
  6. --------------------------------------------------
  7. PUT /museums
  8. {
  9. "mappings": {
  10. "properties": {
  11. "location": {
  12. "type": "geo_point"
  13. }
  14. }
  15. }
  16. }
  17. POST /museums/_bulk?refresh
  18. {"index":{"_id":1}}
  19. {"location": "52.374081,4.912350", "city": "Amsterdam", "name": "NEMO Science Museum"}
  20. {"index":{"_id":2}}
  21. {"location": "52.369219,4.901618", "city": "Amsterdam", "name": "Museum Het Rembrandthuis"}
  22. {"index":{"_id":3}}
  23. {"location": "52.371667,4.914722", "city": "Amsterdam", "name": "Nederlands Scheepvaartmuseum"}
  24. {"index":{"_id":4}}
  25. {"location": "51.222900,4.405200", "city": "Antwerp", "name": "Letterenhuis"}
  26. {"index":{"_id":5}}
  27. {"location": "48.861111,2.336389", "city": "Paris", "name": "Musée du Louvre"}
  28. {"index":{"_id":6}}
  29. {"location": "48.860000,2.327000", "city": "Paris", "name": "Musée d'Orsay"}
  30. POST /museums/_search?size=0
  31. {
  32. "aggs" : {
  33. "centroid" : {
  34. "geo_centroid" : {
  35. "field" : "location" <1>
  36. }
  37. }
  38. }
  39. }
  40. --------------------------------------------------
  41. <1> The `geo_centroid` aggregation specifies the field to use for computing the centroid. (NOTE: field must be a <<geo-point>> type)
  42. The above aggregation demonstrates how one would compute the centroid of the location field for all documents with a crime type of burglary
  43. The response for the above aggregation:
  44. [source,console-result]
  45. --------------------------------------------------
  46. {
  47. ...
  48. "aggregations": {
  49. "centroid": {
  50. "location": {
  51. "lat": 51.00982965203002,
  52. "lon": 3.9662131341174245
  53. },
  54. "count": 6
  55. }
  56. }
  57. }
  58. --------------------------------------------------
  59. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  60. The `geo_centroid` aggregation is more interesting when combined as a sub-aggregation to other bucket aggregations.
  61. Example:
  62. [source,console]
  63. --------------------------------------------------
  64. POST /museums/_search?size=0
  65. {
  66. "aggs" : {
  67. "cities" : {
  68. "terms" : { "field" : "city.keyword" },
  69. "aggs" : {
  70. "centroid" : {
  71. "geo_centroid" : { "field" : "location" }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. --------------------------------------------------
  78. // TEST[continued]
  79. The above example uses `geo_centroid` as a sub-aggregation to a
  80. <<search-aggregations-bucket-terms-aggregation, terms>> bucket aggregation
  81. for finding the central location for museums in each city.
  82. The response for the above aggregation:
  83. [source,console-result]
  84. --------------------------------------------------
  85. {
  86. ...
  87. "aggregations": {
  88. "cities": {
  89. "sum_other_doc_count": 0,
  90. "doc_count_error_upper_bound": 0,
  91. "buckets": [
  92. {
  93. "key": "Amsterdam",
  94. "doc_count": 3,
  95. "centroid": {
  96. "location": {
  97. "lat": 52.371655656024814,
  98. "lon": 4.909563297405839
  99. },
  100. "count": 3
  101. }
  102. },
  103. {
  104. "key": "Paris",
  105. "doc_count": 2,
  106. "centroid": {
  107. "location": {
  108. "lat": 48.86055548675358,
  109. "lon": 2.3316944623366
  110. },
  111. "count": 2
  112. }
  113. },
  114. {
  115. "key": "Antwerp",
  116. "doc_count": 1,
  117. "centroid": {
  118. "location": {
  119. "lat": 51.22289997059852,
  120. "lon": 4.40519998781383
  121. },
  122. "count": 1
  123. }
  124. }
  125. ]
  126. }
  127. }
  128. }
  129. --------------------------------------------------
  130. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  131. [WARNING]
  132. .Using `geo_centroid` as a sub-aggregation of `geohash_grid`
  133. ====
  134. The <<search-aggregations-bucket-geohashgrid-aggregation,`geohash_grid`>>
  135. aggregation places documents, not individual geo-points, into buckets. If a
  136. document's `geo_point` field contains <<array,multiple values>>, the document
  137. could be assigned to multiple buckets, even if one or more of its geo-points are
  138. outside the bucket boundaries.
  139. If a `geocentroid` sub-aggregation is also used, each centroid is calculated
  140. using all geo-points in a bucket, including those outside the bucket boundaries.
  141. This can result in centroids outside of bucket boundaries.
  142. ====