geo-bounding-box-query.asciidoc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. |=======================================================================
  157. [[query-dsl-geo-bounding-box-query-accepted-formats]]
  158. [discrete]
  159. ==== Accepted Formats
  160. In much the same way the `geo_point` type can accept different
  161. representations of the geo point, the filter can accept it as well:
  162. [discrete]
  163. ===== Lat Lon As Properties
  164. [source,console]
  165. --------------------------------------------------
  166. GET my_locations/_search
  167. {
  168. "query": {
  169. "bool": {
  170. "must": {
  171. "match_all": {}
  172. },
  173. "filter": {
  174. "geo_bounding_box": {
  175. "pin.location": {
  176. "top_left": {
  177. "lat": 40.73,
  178. "lon": -74.1
  179. },
  180. "bottom_right": {
  181. "lat": 40.01,
  182. "lon": -71.12
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. --------------------------------------------------
  191. [discrete]
  192. ===== Lat Lon As Array
  193. Format in `[lon, lat]`, note, the order of lon/lat here in order to
  194. conform with http://geojson.org/[GeoJSON].
  195. [source,console]
  196. --------------------------------------------------
  197. GET my_locations/_search
  198. {
  199. "query": {
  200. "bool": {
  201. "must": {
  202. "match_all": {}
  203. },
  204. "filter": {
  205. "geo_bounding_box": {
  206. "pin.location": {
  207. "top_left": [ -74.1, 40.73 ],
  208. "bottom_right": [ -71.12, 40.01 ]
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. --------------------------------------------------
  216. [discrete]
  217. ===== Lat Lon As String
  218. Format in `lat,lon`.
  219. [source,console]
  220. --------------------------------------------------
  221. GET my_locations/_search
  222. {
  223. "query": {
  224. "bool": {
  225. "must": {
  226. "match_all": {}
  227. },
  228. "filter": {
  229. "geo_bounding_box": {
  230. "pin.location": {
  231. "top_left": "POINT (-74.1 40.73)",
  232. "bottom_right": "POINT (-71.12 40.01)"
  233. }
  234. }
  235. }
  236. }
  237. }
  238. }
  239. --------------------------------------------------
  240. [discrete]
  241. ===== Bounding Box as Well-Known Text (WKT)
  242. [source,console]
  243. --------------------------------------------------
  244. GET my_locations/_search
  245. {
  246. "query": {
  247. "bool": {
  248. "must": {
  249. "match_all": {}
  250. },
  251. "filter": {
  252. "geo_bounding_box": {
  253. "pin.location": {
  254. "wkt": "BBOX (-74.1, -71.12, 40.73, 40.01)"
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. --------------------------------------------------
  262. [discrete]
  263. ===== Geohash
  264. [source,console]
  265. --------------------------------------------------
  266. GET my_locations/_search
  267. {
  268. "query": {
  269. "bool": {
  270. "must": {
  271. "match_all": {}
  272. },
  273. "filter": {
  274. "geo_bounding_box": {
  275. "pin.location": {
  276. "top_left": "dr5r9ydj2y73",
  277. "bottom_right": "drj7teegpus6"
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284. --------------------------------------------------
  285. When geohashes are used to specify the bounding the edges of the
  286. bounding box, the geohashes are treated as rectangles. The bounding
  287. box is defined in such a way that its top left corresponds to the top
  288. left corner of the geohash specified in the `top_left` parameter and
  289. its bottom right is defined as the bottom right of the geohash
  290. specified in the `bottom_right` parameter.
  291. In order to specify a bounding box that would match entire area of a
  292. geohash the geohash can be specified in both `top_left` and
  293. `bottom_right` parameters:
  294. [source,console]
  295. --------------------------------------------------
  296. GET my_locations/_search
  297. {
  298. "query": {
  299. "geo_bounding_box": {
  300. "pin.location": {
  301. "top_left": "dr",
  302. "bottom_right": "dr"
  303. }
  304. }
  305. }
  306. }
  307. --------------------------------------------------
  308. In this example, the geohash `dr` will produce the bounding box
  309. query with the top left corner at `45.0,-78.75` and the bottom right
  310. corner at `39.375,-67.5`.
  311. [discrete]
  312. ==== Vertices
  313. The vertices of the bounding box can either be set by `top_left` and
  314. `bottom_right` or by `top_right` and `bottom_left` parameters. More
  315. over the names `topLeft`, `bottomRight`, `topRight` and `bottomLeft`
  316. are supported. Instead of setting the values pairwise, one can use
  317. the simple names `top`, `left`, `bottom` and `right` to set the
  318. values separately.
  319. [source,console]
  320. --------------------------------------------------
  321. GET my_locations/_search
  322. {
  323. "query": {
  324. "bool": {
  325. "must": {
  326. "match_all": {}
  327. },
  328. "filter": {
  329. "geo_bounding_box": {
  330. "pin.location": {
  331. "top": 40.73,
  332. "left": -74.1,
  333. "bottom": 40.01,
  334. "right": -71.12
  335. }
  336. }
  337. }
  338. }
  339. }
  340. }
  341. --------------------------------------------------
  342. [discrete]
  343. ==== Multi Location Per Document
  344. The filter can work with multiple locations / points per document. Once
  345. a single location / point matches the filter, the document will be
  346. included in the filter
  347. [discrete]
  348. ==== Ignore Unmapped
  349. When set to `true` the `ignore_unmapped` option will ignore an unmapped field
  350. and will not match any documents for this query. This can be useful when
  351. querying multiple indexes which might have different mappings. When set to
  352. `false` (the default value) the query will throw an exception if the field
  353. is not mapped.
  354. [discrete]
  355. ==== Notes on Precision
  356. Geopoints have limited precision and are always rounded down during index time.
  357. During the query time, upper boundaries of the bounding boxes are rounded down,
  358. while lower boundaries are rounded up. As a result, the points along on the
  359. lower bounds (bottom and left edges of the bounding box) might not make it into
  360. the bounding box due to the rounding error. At the same time points alongside
  361. the upper bounds (top and right edges) might be selected by the query even if
  362. they are located slightly outside the edge. The rounding error should be less
  363. than 4.20e-8 degrees on the latitude and less than 8.39e-8 degrees on the
  364. longitude, which translates to less than 1cm error even at the equator.
  365. Geoshapes also have limited precision due to rounding. Geoshape edges along the
  366. bounding box's bottom and left edges may not match a `geo_bounding_box` query.
  367. Geoshape edges slightly outside the box's top and right edges may still match
  368. the query.