geocentroid-aggregation.asciidoc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. 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.966213036328554
  56. }
  57. }
  58. }
  59. }
  60. --------------------------------------------------
  61. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  62. The `geo_centroid` aggregation is more interesting when combined as a sub-aggregation to other bucket aggregations.
  63. Example:
  64. [source,js]
  65. --------------------------------------------------
  66. POST /museums/_search?size=0
  67. {
  68. "aggs" : {
  69. "cities" : {
  70. "terms" : { "field" : "city.keyword" },
  71. "aggs" : {
  72. "centroid" : {
  73. "geo_centroid" : { "field" : "location" }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. --------------------------------------------------
  80. // CONSOLE
  81. // TEST[continued]
  82. The above example uses `geo_centroid` as a sub-aggregation to a
  83. <<search-aggregations-bucket-terms-aggregation, terms>> bucket aggregation
  84. for finding the central location for museums in each city.
  85. The response for the above aggregation:
  86. [source,js]
  87. --------------------------------------------------
  88. {
  89. ...
  90. "aggregations": {
  91. "cities": {
  92. "sum_other_doc_count": 0,
  93. "doc_count_error_upper_bound": 0,
  94. "buckets": [
  95. {
  96. "key": "Amsterdam",
  97. "doc_count": 3,
  98. "centroid": {
  99. "location": {
  100. "lat": 52.371655656024814,
  101. "lon": 4.909563269466162
  102. }
  103. }
  104. },
  105. {
  106. "key": "Paris",
  107. "doc_count": 2,
  108. "centroid": {
  109. "location": {
  110. "lat": 48.86055544484407,
  111. "lon": 2.331694420427084
  112. }
  113. }
  114. },
  115. {
  116. "key": "Antwerp",
  117. "doc_count": 1,
  118. "centroid": {
  119. "location": {
  120. "lat": 51.222899928689,
  121. "lon": 4.405199903994799
  122. }
  123. }
  124. }
  125. ]
  126. }
  127. }
  128. }
  129. --------------------------------------------------
  130. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]