geocentroid-aggregation.asciidoc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. [[search-aggregations-metrics-geocentroid-aggregation]]
  2. === Geo-centroid aggregation
  3. ++++
  4. <titleabbrev>Geo-centroid</titleabbrev>
  5. ++++
  6. A metric aggregation that computes the weighted {wikipedia}/Centroid[centroid] from all coordinate values for geo fields.
  7. Example:
  8. [source,console]
  9. --------------------------------------------------
  10. PUT /museums
  11. {
  12. "mappings": {
  13. "properties": {
  14. "location": {
  15. "type": "geo_point"
  16. }
  17. }
  18. }
  19. }
  20. POST /museums/_bulk?refresh
  21. {"index":{"_id":1}}
  22. {"location": "52.374081,4.912350", "city": "Amsterdam", "name": "NEMO Science Museum"}
  23. {"index":{"_id":2}}
  24. {"location": "52.369219,4.901618", "city": "Amsterdam", "name": "Museum Het Rembrandthuis"}
  25. {"index":{"_id":3}}
  26. {"location": "52.371667,4.914722", "city": "Amsterdam", "name": "Nederlands Scheepvaartmuseum"}
  27. {"index":{"_id":4}}
  28. {"location": "51.222900,4.405200", "city": "Antwerp", "name": "Letterenhuis"}
  29. {"index":{"_id":5}}
  30. {"location": "48.861111,2.336389", "city": "Paris", "name": "Musée du Louvre"}
  31. {"index":{"_id":6}}
  32. {"location": "48.860000,2.327000", "city": "Paris", "name": "Musée d'Orsay"}
  33. POST /museums/_search?size=0
  34. {
  35. "aggs": {
  36. "centroid": {
  37. "geo_centroid": {
  38. "field": "location" <1>
  39. }
  40. }
  41. }
  42. }
  43. --------------------------------------------------
  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,console-result]
  48. --------------------------------------------------
  49. {
  50. ...
  51. "aggregations": {
  52. "centroid": {
  53. "location": {
  54. "lat": 51.00982965203002,
  55. "lon": 3.9662131341174245
  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,console]
  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. // 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,console-result]
  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.909563297405839
  102. },
  103. "count": 3
  104. }
  105. },
  106. {
  107. "key": "Paris",
  108. "doc_count": 2,
  109. "centroid": {
  110. "location": {
  111. "lat": 48.86055548675358,
  112. "lon": 2.3316944623366
  113. },
  114. "count": 2
  115. }
  116. },
  117. {
  118. "key": "Antwerp",
  119. "doc_count": 1,
  120. "centroid": {
  121. "location": {
  122. "lat": 51.22289997059852,
  123. "lon": 4.40519998781383
  124. },
  125. "count": 1
  126. }
  127. }
  128. ]
  129. }
  130. }
  131. }
  132. --------------------------------------------------
  133. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  134. [discrete]
  135. [role="xpack"]
  136. [[geocentroid-aggregation-geo-shape]]
  137. ==== Geo Centroid Aggregation on `geo_shape` fields
  138. The centroid metric for geoshapes is more nuanced than for points. The centroid of a specific aggregation bucket
  139. containing shapes is the centroid of the highest-dimensionality shape type in the bucket. For example, if a bucket contains
  140. shapes comprising of polygons and lines, then the lines do not contribute to the centroid metric. Each type of shape's
  141. centroid is calculated differently. Envelopes and circles ingested via the <<ingest-circle-processor>> are treated
  142. as polygons.
  143. |===
  144. |Geometry Type | Centroid Calculation
  145. |[Multi]Point
  146. |equally weighted average of all the coordinates
  147. |[Multi]LineString
  148. |a weighted average of all the centroids of each segment, where the weight of each segment is its length in degrees
  149. |[Multi]Polygon
  150. |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.
  151. holes have negative weights. weights represent the area of the triangle in deg^2 calculated
  152. |GeometryCollection
  153. |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.
  154. If Lines and Points, then points are ignored
  155. |===
  156. Example:
  157. [source,console]
  158. --------------------------------------------------
  159. PUT /places
  160. {
  161. "mappings": {
  162. "properties": {
  163. "geometry": {
  164. "type": "geo_shape"
  165. }
  166. }
  167. }
  168. }
  169. POST /places/_bulk?refresh
  170. {"index":{"_id":1}}
  171. {"name": "NEMO Science Museum", "geometry": "POINT(4.912350 52.374081)" }
  172. {"index":{"_id":2}}
  173. {"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 ] ] ] } }
  174. POST /places/_search?size=0
  175. {
  176. "aggs": {
  177. "centroid": {
  178. "geo_centroid": {
  179. "field": "geometry"
  180. }
  181. }
  182. }
  183. }
  184. --------------------------------------------------
  185. // TEST
  186. [source,console-result]
  187. --------------------------------------------------
  188. {
  189. ...
  190. "aggregations": {
  191. "centroid": {
  192. "location": {
  193. "lat": 52.39296147599816,
  194. "lon": 4.967404240742326
  195. },
  196. "count": 2
  197. }
  198. }
  199. }
  200. --------------------------------------------------
  201. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  202. [WARNING]
  203. .Using `geo_centroid` as a sub-aggregation of `geohash_grid`
  204. ====
  205. The <<search-aggregations-bucket-geohashgrid-aggregation,`geohash_grid`>>
  206. aggregation places documents, not individual geopoints, into buckets. If a
  207. document's `geo_point` field contains <<array,multiple values>>, the document
  208. could be assigned to multiple buckets, even if one or more of its geopoints are
  209. outside the bucket boundaries.
  210. If a `geocentroid` sub-aggregation is also used, each centroid is calculated
  211. using all geopoints in a bucket, including those outside the bucket boundaries.
  212. This can result in centroids outside of bucket boundaries.
  213. ====