geo-bounding-box-query.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. [[query-dsl-geo-bounding-box-query]]
  2. === Geo Bounding Box Query
  3. A query allowing to filter hits based on a point location using a
  4. bounding box. Assuming the following indexed document:
  5. [source,js]
  6. --------------------------------------------------
  7. {
  8. "pin" : {
  9. "location" : {
  10. "lat" : 40.12,
  11. "lon" : -71.34
  12. }
  13. }
  14. }
  15. --------------------------------------------------
  16. Then the following simple query can be executed with a
  17. `geo_bounding_box` filter:
  18. [source,js]
  19. --------------------------------------------------
  20. {
  21. "filtered" : {
  22. "query" : {
  23. "match_all" : {}
  24. },
  25. "filter" : {
  26. "geo_bounding_box" : {
  27. "pin.location" : {
  28. "top_left" : {
  29. "lat" : 40.73,
  30. "lon" : -74.1
  31. },
  32. "bottom_right" : {
  33. "lat" : 40.01,
  34. "lon" : -71.12
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. --------------------------------------------------
  42. [float]
  43. ==== Accepted Formats
  44. In much the same way the geo_point type can accept different
  45. representation of the geo point, the filter can accept it as well:
  46. [float]
  47. ===== Lat Lon As Properties
  48. [source,js]
  49. --------------------------------------------------
  50. {
  51. "filtered" : {
  52. "query" : {
  53. "match_all" : {}
  54. },
  55. "filter" : {
  56. "geo_bounding_box" : {
  57. "pin.location" : {
  58. "top_left" : {
  59. "lat" : 40.73,
  60. "lon" : -74.1
  61. },
  62. "bottom_right" : {
  63. "lat" : 40.01,
  64. "lon" : -71.12
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. --------------------------------------------------
  72. [float]
  73. ===== Lat Lon As Array
  74. Format in `[lon, lat]`, note, the order of lon/lat here in order to
  75. conform with http://geojson.org/[GeoJSON].
  76. [source,js]
  77. --------------------------------------------------
  78. {
  79. "filtered" : {
  80. "query" : {
  81. "match_all" : {}
  82. },
  83. "filter" : {
  84. "geo_bounding_box" : {
  85. "pin.location" : {
  86. "top_left" : [-74.1, 40.73],
  87. "bottom_right" : [-71.12, 40.01]
  88. }
  89. }
  90. }
  91. }
  92. }
  93. --------------------------------------------------
  94. [float]
  95. ===== Lat Lon As String
  96. Format in `lat,lon`.
  97. [source,js]
  98. --------------------------------------------------
  99. {
  100. "filtered" : {
  101. "query" : {
  102. "match_all" : {}
  103. },
  104. "filter" : {
  105. "geo_bounding_box" : {
  106. "pin.location" : {
  107. "top_left" : "40.73, -74.1",
  108. "bottom_right" : "40.01, -71.12"
  109. }
  110. }
  111. }
  112. }
  113. }
  114. --------------------------------------------------
  115. [float]
  116. ===== Geohash
  117. [source,js]
  118. --------------------------------------------------
  119. {
  120. "filtered" : {
  121. "query" : {
  122. "match_all" : {}
  123. },
  124. "filter" : {
  125. "geo_bounding_box" : {
  126. "pin.location" : {
  127. "top_left" : "dr5r9ydj2y73",
  128. "bottom_right" : "drj7teegpus6"
  129. }
  130. }
  131. }
  132. }
  133. }
  134. --------------------------------------------------
  135. [float]
  136. ==== Vertices
  137. The vertices of the bounding box can either be set by `top_left` and
  138. `bottom_right` or by `top_right` and `bottom_left` parameters. More
  139. over the names `topLeft`, `bottomRight`, `topRight` and `bottomLeft`
  140. are supported. Instead of setting the values pairwise, one can use
  141. the simple names `top`, `left`, `bottom` and `right` to set the
  142. values separately.
  143. [source,js]
  144. --------------------------------------------------
  145. {
  146. "filtered" : {
  147. "query" : {
  148. "match_all" : {}
  149. },
  150. "filter" : {
  151. "geo_bounding_box" : {
  152. "pin.location" : {
  153. "top" : -74.1,
  154. "left" : 40.73,
  155. "bottom" : -71.12,
  156. "right" : 40.01
  157. }
  158. }
  159. }
  160. }
  161. }
  162. --------------------------------------------------
  163. [float]
  164. ==== geo_point Type
  165. The filter *requires* the `geo_point` type to be set on the relevant
  166. field.
  167. [float]
  168. ==== Multi Location Per Document
  169. The filter can work with multiple locations / points per document. Once
  170. a single location / point matches the filter, the document will be
  171. included in the filter
  172. [float]
  173. ==== Type
  174. The type of the bounding box execution by default is set to `memory`,
  175. which means in memory checks if the doc falls within the bounding box
  176. range. In some cases, an `indexed` option will perform faster (but note
  177. that the `geo_point` type must have lat and lon indexed in this case).
  178. Note, when using the indexed option, multi locations per document field
  179. are not supported. Here is an example:
  180. [source,js]
  181. --------------------------------------------------
  182. {
  183. "filtered" : {
  184. "query" : {
  185. "match_all" : {}
  186. },
  187. "filter" : {
  188. "geo_bounding_box" : {
  189. "pin.location" : {
  190. "top_left" : {
  191. "lat" : 40.73,
  192. "lon" : -74.1
  193. },
  194. "bottom_right" : {
  195. "lat" : 40.10,
  196. "lon" : -71.12
  197. }
  198. },
  199. "type" : "indexed"
  200. }
  201. }
  202. }
  203. }
  204. --------------------------------------------------