geobounds-aggregation.asciidoc 4.3 KB

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