geobounds-aggregation.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. [[search-aggregations-metrics-geobounds-aggregation]]
  2. === Geo-bounds aggregation
  3. ++++
  4. <titleabbrev>Geo-bounds</titleabbrev>
  5. ++++
  6. A metric aggregation that computes the bounding box containing all geo values for a field.
  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", "name": "NEMO Science Museum"}
  23. {"index":{"_id":2}}
  24. {"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
  25. {"index":{"_id":3}}
  26. {"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
  27. {"index":{"_id":4}}
  28. {"location": "51.222900,4.405200", "name": "Letterenhuis"}
  29. {"index":{"_id":5}}
  30. {"location": "48.861111,2.336389", "name": "Musée du Louvre"}
  31. {"index":{"_id":6}}
  32. {"location": "48.860000,2.327000", "name": "Musée d'Orsay"}
  33. POST /museums/_search?size=0
  34. {
  35. "query": {
  36. "match": { "name": "musée" }
  37. },
  38. "aggs": {
  39. "viewport": {
  40. "geo_bounds": {
  41. "field": "location", <1>
  42. "wrap_longitude": true <2>
  43. }
  44. }
  45. }
  46. }
  47. --------------------------------------------------
  48. <1> The `geo_bounds` aggregation specifies the field to use to obtain the bounds.
  49. <2> [[geo-bounds-wrap-longitude]] `wrap_longitude` is an optional parameter which specifies whether the bounding box should be allowed to overlap the international date line. The default value is `true`.
  50. The above aggregation demonstrates how one would compute the bounding box of the location field for all documents with a business type of shop.
  51. The response for the above aggregation:
  52. [source,console-result]
  53. --------------------------------------------------
  54. {
  55. ...
  56. "aggregations": {
  57. "viewport": {
  58. "bounds": {
  59. "top_left": {
  60. "lat": 48.86111099738628,
  61. "lon": 2.3269999679178
  62. },
  63. "bottom_right": {
  64. "lat": 48.85999997612089,
  65. "lon": 2.3363889567553997
  66. }
  67. }
  68. }
  69. }
  70. }
  71. --------------------------------------------------
  72. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  73. [discrete]
  74. [role="xpack"]
  75. [[geobounds-aggregation-geo-shape]]
  76. ==== Geo Bounds Aggregation on `geo_shape` fields
  77. The Geo Bounds Aggregation is also supported on `geo_shape` fields.
  78. If <<geo-bounds-wrap-longitude,`wrap_longitude`>> is set to `true`
  79. (the default), the bounding box can overlap the international date line and
  80. return a bounds where the `top_left` longitude is larger than the `top_right`
  81. longitude.
  82. For example, the upper right longitude will typically be greater than the lower
  83. left longitude of a geographic bounding box. However, when the area
  84. crosses the 180° meridian, the value of the lower left longitude will be
  85. greater than the value of the upper right longitude. See
  86. http://docs.opengeospatial.org/is/12-063r5/12-063r5.html#30[Geographic bounding box] on the Open Geospatial Consortium website for more information.
  87. Example:
  88. [source,console]
  89. --------------------------------------------------
  90. PUT /places
  91. {
  92. "mappings": {
  93. "properties": {
  94. "geometry": {
  95. "type": "geo_shape"
  96. }
  97. }
  98. }
  99. }
  100. POST /places/_bulk?refresh
  101. {"index":{"_id":1}}
  102. {"name": "NEMO Science Museum", "geometry": "POINT(4.912350 52.374081)" }
  103. {"index":{"_id":2}}
  104. {"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 ] ] ] } }
  105. POST /places/_search?size=0
  106. {
  107. "aggs": {
  108. "viewport": {
  109. "geo_bounds": {
  110. "field": "geometry"
  111. }
  112. }
  113. }
  114. }
  115. --------------------------------------------------
  116. // TEST
  117. [source,console-result]
  118. --------------------------------------------------
  119. {
  120. ...
  121. "aggregations": {
  122. "viewport": {
  123. "bounds": {
  124. "top_left": {
  125. "lat": 52.39420966710895,
  126. "lon": 4.912349972873926
  127. },
  128. "bottom_right": {
  129. "lat": 52.374080987647176,
  130. "lon": 4.969425117596984
  131. }
  132. }
  133. }
  134. }
  135. }
  136. --------------------------------------------------
  137. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]