geocentroid-aggregation.asciidoc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 geo fields.
  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. [discrete]
  132. [role="xpack"]
  133. ==== Geo Centroid Aggregation on `geo_shape` fields
  134. The centroid metric for geo-shapes is more nuanced than for points. The centroid of a specific aggregation bucket
  135. containing shapes is the centroid of the highest-dimensionality shape type in the bucket. For example, if a bucket contains
  136. shapes comprising of polygons and lines, then the lines do not contribute to the centroid metric. Each type of shape's
  137. centroid is calculated differently. Envelopes and circles ingested via the <<ingest-circle-processor>> are treated
  138. as polygons.
  139. |===
  140. |Geometry Type | Centroid Calculation
  141. |[Multi]Point
  142. |equally weighted average of all the coordinates
  143. |[Multi]LineString
  144. |a weighted average of all the centroids of each segment, where the weight of each segment is its length in degrees
  145. |[Multi]Polygon
  146. |a weighted average of all the centroids of all the triangles of a polygon where the triangles are formed by every two consecutive vertices and the starting-point.
  147. holes have negative weights. weights represent the area of the triangle in deg^2 calculated
  148. |GeometryCollection
  149. |The centroid of all the underlying geometries with the highest dimension. If Polygons and Lines and/or Points, then lines and/or points are ignored.
  150. If Lines and Points, then points are ignored
  151. |===
  152. Example:
  153. [source,console]
  154. --------------------------------------------------
  155. PUT /places
  156. {
  157. "mappings": {
  158. "properties": {
  159. "geometry": {
  160. "type": "geo_shape"
  161. }
  162. }
  163. }
  164. }
  165. POST /places/_bulk?refresh
  166. {"index":{"_id":1}}
  167. {"name": "NEMO Science Museum", "geometry": "POINT(4.912350 52.374081)" }
  168. {"index":{"_id":2}}
  169. {"name": "Sportpark De Weeren", "geometry": { "type": "Polygon", "coordinates": [ [ [ 4.965305328369141, 52.39347642069457 ], [ 4.966979026794433, 52.391721758934835 ], [ 4.969425201416015, 52.39238958618537 ], [ 4.967944622039794, 52.39420969150824 ], [ 4.965305328369141, 52.39347642069457 ] ] ] } }
  170. POST /places/_search?size=0
  171. {
  172. "aggs": {
  173. "centroid": {
  174. "geo_centroid": {
  175. "field": "geometry"
  176. }
  177. }
  178. }
  179. }
  180. --------------------------------------------------
  181. // TEST
  182. [source,console-result]
  183. --------------------------------------------------
  184. {
  185. ...
  186. "aggregations": {
  187. "centroid": {
  188. "location": {
  189. "lat": 52.39296147599816,
  190. "lon": 4.967404240742326
  191. },
  192. "count": 2
  193. }
  194. }
  195. }
  196. --------------------------------------------------
  197. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  198. [WARNING]
  199. .Using `geo_centroid` as a sub-aggregation of `geohash_grid`
  200. ====
  201. The <<search-aggregations-bucket-geohashgrid-aggregation,`geohash_grid`>>
  202. aggregation places documents, not individual geo-points, into buckets. If a
  203. document's `geo_point` field contains <<array,multiple values>>, the document
  204. could be assigned to multiple buckets, even if one or more of its geo-points are
  205. outside the bucket boundaries.
  206. If a `geocentroid` sub-aggregation is also used, each centroid is calculated
  207. using all geo-points in a bucket, including those outside the bucket boundaries.
  208. This can result in centroids outside of bucket boundaries.
  209. ====