geo-bounding-box-query.asciidoc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. ==== Query Options
  44. [cols="<,<",options="header",]
  45. |=======================================================================
  46. |Option |Description
  47. |`_name` |Optional name field to identify the filter
  48. |`coerce` |Set to `true` to normalize longitude and latitude values to a
  49. standard -180:180 / -90:90 coordinate system. (default is `false`).
  50. |`ignore_malformed` |Set to `true` to
  51. accept geo points with invalid latitude or longitude (default is `false`).
  52. |`type` |Set to one of `indexed` or `memory` to defines whether this filter will
  53. be executed in memory or indexed. See <<Type,Type>> below for further details
  54. Default is `memory`.
  55. |=======================================================================
  56. [float]
  57. ==== Accepted Formats
  58. In much the same way the geo_point type can accept different
  59. representation of the geo point, the filter can accept it as well:
  60. [float]
  61. ===== Lat Lon As Properties
  62. [source,js]
  63. --------------------------------------------------
  64. {
  65. "filtered" : {
  66. "query" : {
  67. "match_all" : {}
  68. },
  69. "filter" : {
  70. "geo_bounding_box" : {
  71. "pin.location" : {
  72. "top_left" : {
  73. "lat" : 40.73,
  74. "lon" : -74.1
  75. },
  76. "bottom_right" : {
  77. "lat" : 40.01,
  78. "lon" : -71.12
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. --------------------------------------------------
  86. [float]
  87. ===== Lat Lon As Array
  88. Format in `[lon, lat]`, note, the order of lon/lat here in order to
  89. conform with http://geojson.org/[GeoJSON].
  90. [source,js]
  91. --------------------------------------------------
  92. {
  93. "filtered" : {
  94. "query" : {
  95. "match_all" : {}
  96. },
  97. "filter" : {
  98. "geo_bounding_box" : {
  99. "pin.location" : {
  100. "top_left" : [-74.1, 40.73],
  101. "bottom_right" : [-71.12, 40.01]
  102. }
  103. }
  104. }
  105. }
  106. }
  107. --------------------------------------------------
  108. [float]
  109. ===== Lat Lon As String
  110. Format in `lat,lon`.
  111. [source,js]
  112. --------------------------------------------------
  113. {
  114. "filtered" : {
  115. "query" : {
  116. "match_all" : {}
  117. },
  118. "filter" : {
  119. "geo_bounding_box" : {
  120. "pin.location" : {
  121. "top_left" : "40.73, -74.1",
  122. "bottom_right" : "40.01, -71.12"
  123. }
  124. }
  125. }
  126. }
  127. }
  128. --------------------------------------------------
  129. [float]
  130. ===== Geohash
  131. [source,js]
  132. --------------------------------------------------
  133. {
  134. "filtered" : {
  135. "query" : {
  136. "match_all" : {}
  137. },
  138. "filter" : {
  139. "geo_bounding_box" : {
  140. "pin.location" : {
  141. "top_left" : "dr5r9ydj2y73",
  142. "bottom_right" : "drj7teegpus6"
  143. }
  144. }
  145. }
  146. }
  147. }
  148. --------------------------------------------------
  149. [float]
  150. ==== Vertices
  151. The vertices of the bounding box can either be set by `top_left` and
  152. `bottom_right` or by `top_right` and `bottom_left` parameters. More
  153. over the names `topLeft`, `bottomRight`, `topRight` and `bottomLeft`
  154. are supported. Instead of setting the values pairwise, one can use
  155. the simple names `top`, `left`, `bottom` and `right` to set the
  156. values separately.
  157. [source,js]
  158. --------------------------------------------------
  159. {
  160. "filtered" : {
  161. "query" : {
  162. "match_all" : {}
  163. },
  164. "filter" : {
  165. "geo_bounding_box" : {
  166. "pin.location" : {
  167. "top" : -74.1,
  168. "left" : 40.73,
  169. "bottom" : -71.12,
  170. "right" : 40.01
  171. }
  172. }
  173. }
  174. }
  175. }
  176. --------------------------------------------------
  177. [float]
  178. ==== geo_point Type
  179. The filter *requires* the `geo_point` type to be set on the relevant
  180. field.
  181. [float]
  182. ==== Multi Location Per Document
  183. The filter can work with multiple locations / points per document. Once
  184. a single location / point matches the filter, the document will be
  185. included in the filter
  186. [float]
  187. ==== Type
  188. The type of the bounding box execution by default is set to `memory`,
  189. which means in memory checks if the doc falls within the bounding box
  190. range. In some cases, an `indexed` option will perform faster (but note
  191. that the `geo_point` type must have lat and lon indexed in this case).
  192. Note, when using the indexed option, multi locations per document field
  193. are not supported. Here is an example:
  194. [source,js]
  195. --------------------------------------------------
  196. {
  197. "filtered" : {
  198. "query" : {
  199. "match_all" : {}
  200. },
  201. "filter" : {
  202. "geo_bounding_box" : {
  203. "pin.location" : {
  204. "top_left" : {
  205. "lat" : 40.73,
  206. "lon" : -74.1
  207. },
  208. "bottom_right" : {
  209. "lat" : 40.10,
  210. "lon" : -71.12
  211. }
  212. },
  213. "type" : "indexed"
  214. }
  215. }
  216. }
  217. }
  218. --------------------------------------------------