geobounds-aggregation.asciidoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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> `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. Example:
  75. [source,console]
  76. --------------------------------------------------
  77. PUT /places
  78. {
  79. "mappings": {
  80. "properties": {
  81. "geometry": {
  82. "type": "geo_shape"
  83. }
  84. }
  85. }
  86. }
  87. POST /places/_bulk?refresh
  88. {"index":{"_id":1}}
  89. {"name": "NEMO Science Museum", "geometry": "POINT(4.912350 52.374081)" }
  90. {"index":{"_id":2}}
  91. {"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 ] ] ] } }
  92. POST /places/_search?size=0
  93. {
  94. "aggs": {
  95. "viewport": {
  96. "geo_bounds": {
  97. "field": "geometry"
  98. }
  99. }
  100. }
  101. }
  102. --------------------------------------------------
  103. // TEST
  104. [source,console-result]
  105. --------------------------------------------------
  106. {
  107. ...
  108. "aggregations": {
  109. "viewport": {
  110. "bounds": {
  111. "top_left": {
  112. "lat": 52.39420966710895,
  113. "lon": 4.912349972873926
  114. },
  115. "bottom_right": {
  116. "lat": 52.374080987647176,
  117. "lon": 4.969425117596984
  118. }
  119. }
  120. }
  121. }
  122. }
  123. --------------------------------------------------
  124. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]