geo-bounding-box-query.asciidoc 10.0 KB

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