geocentroid-aggregation.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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,js]
  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. // CONSOLE
  42. <1> The `geo_centroid` aggregation specifies the field to use for computing the centroid. (NOTE: field must be a <<geo-point>> type)
  43. The above aggregation demonstrates how one would compute the centroid of the location field for all documents with a crime type of burglary
  44. The response for the above aggregation:
  45. [source,js]
  46. --------------------------------------------------
  47. {
  48. ...
  49. "aggregations": {
  50. "centroid": {
  51. "location": {
  52. "lat": 51.00982965203002,
  53. "lon": 3.9662131341174245
  54. },
  55. "count": 6
  56. }
  57. }
  58. }
  59. --------------------------------------------------
  60. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  61. The `geo_centroid` aggregation is more interesting when combined as a sub-aggregation to other bucket aggregations.
  62. Example:
  63. [source,js]
  64. --------------------------------------------------
  65. POST /museums/_search?size=0
  66. {
  67. "aggs" : {
  68. "cities" : {
  69. "terms" : { "field" : "city.keyword" },
  70. "aggs" : {
  71. "centroid" : {
  72. "geo_centroid" : { "field" : "location" }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. --------------------------------------------------
  79. // CONSOLE
  80. // TEST[continued]
  81. The above example uses `geo_centroid` as a sub-aggregation to a
  82. <<search-aggregations-bucket-terms-aggregation, terms>> bucket aggregation
  83. for finding the central location for museums in each city.
  84. The response for the above aggregation:
  85. [source,js]
  86. --------------------------------------------------
  87. {
  88. ...
  89. "aggregations": {
  90. "cities": {
  91. "sum_other_doc_count": 0,
  92. "doc_count_error_upper_bound": 0,
  93. "buckets": [
  94. {
  95. "key": "Amsterdam",
  96. "doc_count": 3,
  97. "centroid": {
  98. "location": {
  99. "lat": 52.371655656024814,
  100. "lon": 4.909563297405839
  101. },
  102. "count": 3
  103. }
  104. },
  105. {
  106. "key": "Paris",
  107. "doc_count": 2,
  108. "centroid": {
  109. "location": {
  110. "lat": 48.86055548675358,
  111. "lon": 2.3316944623366
  112. },
  113. "count": 2
  114. }
  115. },
  116. {
  117. "key": "Antwerp",
  118. "doc_count": 1,
  119. "centroid": {
  120. "location": {
  121. "lat": 51.22289997059852,
  122. "lon": 4.40519998781383
  123. },
  124. "count": 1
  125. }
  126. }
  127. ]
  128. }
  129. }
  130. }
  131. --------------------------------------------------
  132. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]