geo-bounding-box-query.asciidoc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. [[query-dsl-geo-bounding-box-query]]
  2. === Geo-bounding box query
  3. ++++
  4. <titleabbrev>Geo-bounding box</titleabbrev>
  5. ++++
  6. A query allowing to filter hits based on a point location using a
  7. bounding box. Assuming the following indexed document:
  8. [source,console]
  9. --------------------------------------------------
  10. PUT /my_locations
  11. {
  12. "mappings": {
  13. "properties": {
  14. "pin": {
  15. "properties": {
  16. "location": {
  17. "type": "geo_point"
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. PUT /my_locations/_doc/1
  25. {
  26. "pin": {
  27. "location": {
  28. "lat": 40.12,
  29. "lon": -71.34
  30. }
  31. }
  32. }
  33. --------------------------------------------------
  34. // TESTSETUP
  35. Then the following simple query can be executed with a
  36. `geo_bounding_box` filter:
  37. [source,console]
  38. --------------------------------------------------
  39. GET my_locations/_search
  40. {
  41. "query": {
  42. "bool": {
  43. "must": {
  44. "match_all": {}
  45. },
  46. "filter": {
  47. "geo_bounding_box": {
  48. "pin.location": {
  49. "top_left": {
  50. "lat": 40.73,
  51. "lon": -74.1
  52. },
  53. "bottom_right": {
  54. "lat": 40.01,
  55. "lon": -71.12
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. --------------------------------------------------
  64. [discrete]
  65. ==== Query Options
  66. [cols="<,<",options="header",]
  67. |=======================================================================
  68. |Option |Description
  69. |`_name` |Optional name field to identify the filter
  70. |`validation_method` |Set to `IGNORE_MALFORMED` to
  71. accept geo points with invalid latitude or longitude, set to
  72. `COERCE` to also try to infer correct latitude or longitude. (default is `STRICT`).
  73. |`type` |Set to one of `indexed` or `memory` to defines whether this filter will
  74. be executed in memory or indexed. See <<geo-bbox-type,Type>> below for further details
  75. Default is `memory`.
  76. |=======================================================================
  77. [[query-dsl-geo-bounding-box-query-accepted-formats]]
  78. [discrete]
  79. ==== Accepted Formats
  80. In much the same way the geo_point type can accept different
  81. representations of the geo point, the filter can accept it as well:
  82. [discrete]
  83. ===== Lat Lon As Properties
  84. [source,console]
  85. --------------------------------------------------
  86. GET my_locations/_search
  87. {
  88. "query": {
  89. "bool": {
  90. "must": {
  91. "match_all": {}
  92. },
  93. "filter": {
  94. "geo_bounding_box": {
  95. "pin.location": {
  96. "top_left": {
  97. "lat": 40.73,
  98. "lon": -74.1
  99. },
  100. "bottom_right": {
  101. "lat": 40.01,
  102. "lon": -71.12
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. --------------------------------------------------
  111. [discrete]
  112. ===== Lat Lon As Array
  113. Format in `[lon, lat]`, note, the order of lon/lat here in order to
  114. conform with http://geojson.org/[GeoJSON].
  115. [source,console]
  116. --------------------------------------------------
  117. GET my_locations/_search
  118. {
  119. "query": {
  120. "bool": {
  121. "must": {
  122. "match_all": {}
  123. },
  124. "filter": {
  125. "geo_bounding_box": {
  126. "pin.location": {
  127. "top_left": [ -74.1, 40.73 ],
  128. "bottom_right": [ -71.12, 40.01 ]
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. --------------------------------------------------
  136. [discrete]
  137. ===== Lat Lon As String
  138. Format in `lat,lon`.
  139. [source,console]
  140. --------------------------------------------------
  141. GET my_locations/_search
  142. {
  143. "query": {
  144. "bool": {
  145. "must": {
  146. "match_all": {}
  147. },
  148. "filter": {
  149. "geo_bounding_box": {
  150. "pin.location": {
  151. "top_left": "40.73, -74.1",
  152. "bottom_right": "40.01, -71.12"
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. --------------------------------------------------
  160. [discrete]
  161. ===== Bounding Box as Well-Known Text (WKT)
  162. [source,console]
  163. --------------------------------------------------
  164. GET my_locations/_search
  165. {
  166. "query": {
  167. "bool": {
  168. "must": {
  169. "match_all": {}
  170. },
  171. "filter": {
  172. "geo_bounding_box": {
  173. "pin.location": {
  174. "wkt": "BBOX (-74.1, -71.12, 40.73, 40.01)"
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. --------------------------------------------------
  182. [discrete]
  183. ===== Geohash
  184. [source,console]
  185. --------------------------------------------------
  186. GET my_locations/_search
  187. {
  188. "query": {
  189. "bool": {
  190. "must": {
  191. "match_all": {}
  192. },
  193. "filter": {
  194. "geo_bounding_box": {
  195. "pin.location": {
  196. "top_left": "dr5r9ydj2y73",
  197. "bottom_right": "drj7teegpus6"
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. --------------------------------------------------
  205. When geohashes are used to specify the bounding the edges of the
  206. bounding box, the geohashes are treated as rectangles. The bounding
  207. box is defined in such a way that its top left corresponds to the top
  208. left corner of the geohash specified in the `top_left` parameter and
  209. its bottom right is defined as the bottom right of the geohash
  210. specified in the `bottom_right` parameter.
  211. In order to specify a bounding box that would match entire area of a
  212. geohash the geohash can be specified in both `top_left` and
  213. `bottom_right` parameters:
  214. [source,console]
  215. --------------------------------------------------
  216. GET my_locations/_search
  217. {
  218. "query": {
  219. "geo_bounding_box": {
  220. "pin.location": {
  221. "top_left": "dr",
  222. "bottom_right": "dr"
  223. }
  224. }
  225. }
  226. }
  227. --------------------------------------------------
  228. In this example, the geohash `dr` will produce the bounding box
  229. query with the top left corner at `45.0,-78.75` and the bottom right
  230. corner at `39.375,-67.5`.
  231. [discrete]
  232. ==== Vertices
  233. The vertices of the bounding box can either be set by `top_left` and
  234. `bottom_right` or by `top_right` and `bottom_left` parameters. More
  235. over the names `topLeft`, `bottomRight`, `topRight` and `bottomLeft`
  236. are supported. Instead of setting the values pairwise, one can use
  237. the simple names `top`, `left`, `bottom` and `right` to set the
  238. values separately.
  239. [source,console]
  240. --------------------------------------------------
  241. GET my_locations/_search
  242. {
  243. "query": {
  244. "bool": {
  245. "must": {
  246. "match_all": {}
  247. },
  248. "filter": {
  249. "geo_bounding_box": {
  250. "pin.location": {
  251. "top": 40.73,
  252. "left": -74.1,
  253. "bottom": 40.01,
  254. "right": -71.12
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. --------------------------------------------------
  262. [discrete]
  263. ==== geo_point Type
  264. The filter *requires* the `geo_point` type to be set on the relevant
  265. field.
  266. [discrete]
  267. ==== Multi Location Per Document
  268. The filter can work with multiple locations / points per document. Once
  269. a single location / point matches the filter, the document will be
  270. included in the filter
  271. [discrete]
  272. [[geo-bbox-type]]
  273. ==== Type
  274. The type of the bounding box execution by default is set to `memory`,
  275. which means in memory checks if the doc falls within the bounding box
  276. range. In some cases, an `indexed` option will perform faster (but note
  277. that the `geo_point` type must have lat and lon indexed in this case).
  278. Note, when using the indexed option, multi locations per document field
  279. are not supported. Here is an example:
  280. [source,console]
  281. --------------------------------------------------
  282. GET my_locations/_search
  283. {
  284. "query": {
  285. "bool": {
  286. "must": {
  287. "match_all": {}
  288. },
  289. "filter": {
  290. "geo_bounding_box": {
  291. "pin.location": {
  292. "top_left": {
  293. "lat": 40.73,
  294. "lon": -74.1
  295. },
  296. "bottom_right": {
  297. "lat": 40.10,
  298. "lon": -71.12
  299. }
  300. },
  301. "type": "indexed"
  302. }
  303. }
  304. }
  305. }
  306. }
  307. --------------------------------------------------
  308. [discrete]
  309. ==== Ignore Unmapped
  310. When set to `true` the `ignore_unmapped` option will ignore an unmapped field
  311. and will not match any documents for this query. This can be useful when
  312. querying multiple indexes which might have different mappings. When set to
  313. `false` (the default value) the query will throw an exception if the field
  314. is not mapped.
  315. [discrete]
  316. ==== Notes on Precision
  317. Geopoints have limited precision and are always rounded down during index time.
  318. During the query time, upper boundaries of the bounding boxes are rounded down,
  319. while lower boundaries are rounded up. As a result, the points along on the
  320. lower bounds (bottom and left edges of the bounding box) might not make it into
  321. the bounding box due to the rounding error. At the same time points alongside
  322. the upper bounds (top and right edges) might be selected by the query even if
  323. they are located slightly outside the edge. The rounding error should be less
  324. than 4.20e-8 degrees on the latitude and less than 8.39e-8 degrees on the
  325. longitude, which translates to less than 1cm error even at the equator.