geo-bounding-box-query.asciidoc 9.9 KB

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