geo-bounding-box-query.asciidoc 11 KB

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