cartesian-centroid-aggregation.asciidoc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. [[search-aggregations-metrics-cartesian-centroid-aggregation]]
  2. === Cartesian-centroid aggregation
  3. ++++
  4. <titleabbrev>Cartesian-centroid</titleabbrev>
  5. ++++
  6. A metric aggregation that computes the weighted {wikipedia}/Centroid[centroid] from all coordinate values for point and shape fields.
  7. Example:
  8. [source,console]
  9. --------------------------------------------------
  10. PUT /museums
  11. {
  12. "mappings": {
  13. "properties": {
  14. "location": {
  15. "type": "point"
  16. }
  17. }
  18. }
  19. }
  20. POST /museums/_bulk?refresh
  21. {"index":{"_id":1}}
  22. {"location": "POINT (491.2350 5237.4081)", "city": "Amsterdam", "name": "NEMO Science Museum"}
  23. {"index":{"_id":2}}
  24. {"location": "POINT (490.1618 5236.9219)", "city": "Amsterdam", "name": "Museum Het Rembrandthuis"}
  25. {"index":{"_id":3}}
  26. {"location": "POINT (491.4722 5237.1667)", "city": "Amsterdam", "name": "Nederlands Scheepvaartmuseum"}
  27. {"index":{"_id":4}}
  28. {"location": "POINT (440.5200 5122.2900)", "city": "Antwerp", "name": "Letterenhuis"}
  29. {"index":{"_id":5}}
  30. {"location": "POINT (233.6389 4886.1111)", "city": "Paris", "name": "Musée du Louvre"}
  31. {"index":{"_id":6}}
  32. {"location": "POINT (232.7000 4886.0000)", "city": "Paris", "name": "Musée d'Orsay"}
  33. POST /museums/_search?size=0
  34. {
  35. "aggs": {
  36. "centroid": {
  37. "cartesian_centroid": {
  38. "field": "location" <1>
  39. }
  40. }
  41. }
  42. }
  43. --------------------------------------------------
  44. <1> The `cartesian_centroid` aggregation specifies the field to use for computing the centroid, which must be a <<point>> or a <<shape>> type.
  45. The above aggregation demonstrates how one would compute the centroid of the location field for all museums' documents.
  46. The response for the above aggregation:
  47. [source,console-result]
  48. --------------------------------------------------
  49. {
  50. ...
  51. "aggregations": {
  52. "centroid": {
  53. "location": {
  54. "x": 396.6213124593099,
  55. "y": 5100.982991536458
  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 `cartesian_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. "cartesian_centroid": { "field": "location" }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. --------------------------------------------------
  81. // TEST[continued]
  82. The above example uses `cartesian_centroid` as a sub-aggregation to a
  83. <<search-aggregations-bucket-terms-aggregation, terms>> bucket aggregation for finding the central location for museums in each city.
  84. The response for the above aggregation:
  85. [source,console-result]
  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. "x": 490.9563293457031,
  100. "y": 5237.16552734375
  101. },
  102. "count": 3
  103. }
  104. },
  105. {
  106. "key": "Paris",
  107. "doc_count": 2,
  108. "centroid": {
  109. "location": {
  110. "x": 233.16944885253906,
  111. "y": 4886.0556640625
  112. },
  113. "count": 2
  114. }
  115. },
  116. {
  117. "key": "Antwerp",
  118. "doc_count": 1,
  119. "centroid": {
  120. "location": {
  121. "x": 440.5199890136719,
  122. "y": 5122.2900390625
  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,/]
  133. [discrete]
  134. [role="xpack"]
  135. [[cartesian-centroid-aggregation-geo-shape]]
  136. ==== Cartesian Centroid Aggregation on `shape` fields
  137. The centroid metric for shapes is more nuanced than for points.
  138. The centroid of a specific aggregation bucket containing shapes is the centroid of the highest-dimensionality shape type in the bucket.
  139. For example, if a bucket contains shapes consisting of polygons and lines, then the lines do not contribute to the centroid metric.
  140. Each type of shape's centroid is calculated differently.
  141. Envelopes and circles ingested via the <<ingest-circle-processor>> are treated as polygons.
  142. |===
  143. |Geometry Type | Centroid Calculation
  144. |[Multi]Point
  145. |equally weighted average of all the coordinates
  146. |[Multi]LineString
  147. |a weighted average of all the centroids of each segment, where the weight of each segment is its length in the same units as the coordinates
  148. |[Multi]Polygon
  149. |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.
  150. holes have negative weights. weights represent the area of the triangle is calculated in the square of the units of the coordinates
  151. |GeometryCollection
  152. |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.
  153. If Lines and Points, then points are ignored
  154. |===
  155. Example:
  156. [source,console]
  157. --------------------------------------------------
  158. PUT /places
  159. {
  160. "mappings": {
  161. "properties": {
  162. "geometry": {
  163. "type": "shape"
  164. }
  165. }
  166. }
  167. }
  168. POST /places/_bulk?refresh
  169. {"index":{"_id":1}}
  170. {"name": "NEMO Science Museum", "geometry": "POINT(491.2350 5237.4081)" }
  171. {"index":{"_id":2}}
  172. {"name": "Sportpark De Weeren", "geometry": { "type": "Polygon", "coordinates": [ [ [ 496.5305328369141, 5239.347642069457 ], [ 496.6979026794433, 5239.1721758934835 ], [ 496.9425201416015, 5239.238958618537 ], [ 496.7944622039794, 5239.420969150824 ], [ 496.5305328369141, 5239.347642069457 ] ] ] } }
  173. POST /places/_search?size=0
  174. {
  175. "aggs": {
  176. "centroid": {
  177. "cartesian_centroid": {
  178. "field": "geometry"
  179. }
  180. }
  181. }
  182. }
  183. --------------------------------------------------
  184. // TEST
  185. [source,console-result]
  186. --------------------------------------------------
  187. {
  188. ...
  189. "aggregations": {
  190. "centroid": {
  191. "location": {
  192. "x": 496.74041748046875,
  193. "y": 5239.29638671875
  194. },
  195. "count": 2
  196. }
  197. }
  198. }
  199. --------------------------------------------------
  200. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]