geocentroid-aggregation.asciidoc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "_doc": {
  11. "properties": {
  12. "location": {
  13. "type": "geo_point"
  14. }
  15. }
  16. }
  17. }
  18. }
  19. POST /museums/_doc/_bulk?refresh
  20. {"index":{"_id":1}}
  21. {"location": "52.374081,4.912350", "city": "Amsterdam", "name": "NEMO Science Museum"}
  22. {"index":{"_id":2}}
  23. {"location": "52.369219,4.901618", "city": "Amsterdam", "name": "Museum Het Rembrandthuis"}
  24. {"index":{"_id":3}}
  25. {"location": "52.371667,4.914722", "city": "Amsterdam", "name": "Nederlands Scheepvaartmuseum"}
  26. {"index":{"_id":4}}
  27. {"location": "51.222900,4.405200", "city": "Antwerp", "name": "Letterenhuis"}
  28. {"index":{"_id":5}}
  29. {"location": "48.861111,2.336389", "city": "Paris", "name": "Musée du Louvre"}
  30. {"index":{"_id":6}}
  31. {"location": "48.860000,2.327000", "city": "Paris", "name": "Musée d'Orsay"}
  32. POST /museums/_search?size=0
  33. {
  34. "aggs" : {
  35. "centroid" : {
  36. "geo_centroid" : {
  37. "field" : "location" <1>
  38. }
  39. }
  40. }
  41. }
  42. --------------------------------------------------
  43. // CONSOLE
  44. <1> The `geo_centroid` aggregation specifies the field to use for computing the centroid. (NOTE: field must be a <<geo-point>> type)
  45. The above aggregation demonstrates how one would compute the centroid of the location field for all documents with a crime type of burglary
  46. The response for the above aggregation:
  47. [source,js]
  48. --------------------------------------------------
  49. {
  50. ...
  51. "aggregations": {
  52. "centroid": {
  53. "location": {
  54. "lat": 51.009829603135586,
  55. "lon": 3.9662130642682314
  56. },
  57. "count": 6
  58. }
  59. }
  60. }
  61. --------------------------------------------------
  62. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  63. The `geo_centroid` aggregation is more interesting when combined as a sub-aggregation to other bucket aggregations.
  64. Example:
  65. [source,js]
  66. --------------------------------------------------
  67. POST /museums/_search?size=0
  68. {
  69. "aggs" : {
  70. "cities" : {
  71. "terms" : { "field" : "city.keyword" },
  72. "aggs" : {
  73. "centroid" : {
  74. "geo_centroid" : { "field" : "location" }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. --------------------------------------------------
  81. // CONSOLE
  82. // TEST[continued]
  83. The above example uses `geo_centroid` as a sub-aggregation to a
  84. <<search-aggregations-bucket-terms-aggregation, terms>> bucket aggregation
  85. for finding the central location for museums in each city.
  86. The response for the above aggregation:
  87. [source,js]
  88. --------------------------------------------------
  89. {
  90. ...
  91. "aggregations": {
  92. "cities": {
  93. "sum_other_doc_count": 0,
  94. "doc_count_error_upper_bound": 0,
  95. "buckets": [
  96. {
  97. "key": "Amsterdam",
  98. "doc_count": 3,
  99. "centroid": {
  100. "location": {
  101. "lat": 52.371655642054975,
  102. "lon": 4.9095632415264845
  103. },
  104. "count": 3
  105. }
  106. },
  107. {
  108. "key": "Paris",
  109. "doc_count": 2,
  110. "centroid": {
  111. "location": {
  112. "lat": 48.86055548675358,
  113. "lon": 2.331694420427084
  114. },
  115. "count": 2
  116. }
  117. },
  118. {
  119. "key": "Antwerp",
  120. "doc_count": 1,
  121. "centroid": {
  122. "location": {
  123. "lat": 51.22289997059852,
  124. "lon": 4.40519998781383
  125. },
  126. "count": 1
  127. }
  128. }
  129. ]
  130. }
  131. }
  132. }
  133. --------------------------------------------------
  134. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]