geo-bounding-box-query.asciidoc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. [float]
  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. [float]
  78. ==== Accepted Formats
  79. In much the same way the geo_point type can accept different
  80. representations of the geo point, the filter can accept it as well:
  81. [float]
  82. ===== Lat Lon As Properties
  83. [source,console]
  84. --------------------------------------------------
  85. GET my_locations/_search
  86. {
  87. "query": {
  88. "bool" : {
  89. "must" : {
  90. "match_all" : {}
  91. },
  92. "filter" : {
  93. "geo_bounding_box" : {
  94. "pin.location" : {
  95. "top_left" : {
  96. "lat" : 40.73,
  97. "lon" : -74.1
  98. },
  99. "bottom_right" : {
  100. "lat" : 40.01,
  101. "lon" : -71.12
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. --------------------------------------------------
  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,console]
  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. [float]
  136. ===== Lat Lon As String
  137. Format in `lat,lon`.
  138. [source,console]
  139. --------------------------------------------------
  140. GET my_locations/_search
  141. {
  142. "query": {
  143. "bool" : {
  144. "must" : {
  145. "match_all" : {}
  146. },
  147. "filter" : {
  148. "geo_bounding_box" : {
  149. "pin.location" : {
  150. "top_left" : "40.73, -74.1",
  151. "bottom_right" : "40.01, -71.12"
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. --------------------------------------------------
  159. [float]
  160. ===== Bounding Box as Well-Known Text (WKT)
  161. [source,console]
  162. --------------------------------------------------
  163. GET my_locations/_search
  164. {
  165. "query": {
  166. "bool" : {
  167. "must" : {
  168. "match_all" : {}
  169. },
  170. "filter" : {
  171. "geo_bounding_box" : {
  172. "pin.location" : {
  173. "wkt" : "BBOX (-74.1, -71.12, 40.73, 40.01)"
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. --------------------------------------------------
  181. [float]
  182. ===== Geohash
  183. [source,console]
  184. --------------------------------------------------
  185. GET my_locations/_search
  186. {
  187. "query": {
  188. "bool" : {
  189. "must" : {
  190. "match_all" : {}
  191. },
  192. "filter" : {
  193. "geo_bounding_box" : {
  194. "pin.location" : {
  195. "top_left" : "dr5r9ydj2y73",
  196. "bottom_right" : "drj7teegpus6"
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. --------------------------------------------------
  204. When geohashes are used to specify the bounding the edges of the
  205. bounding box, the geohashes are treated as rectangles. The bounding
  206. box is defined in such a way that its top left corresponds to the top
  207. left corner of the geohash specified in the `top_left` parameter and
  208. its bottom right is defined as the bottom right of the geohash
  209. specified in the `bottom_right` parameter.
  210. In order to specify a bounding box that would match entire area of a
  211. geohash the geohash can be specified in both `top_left` and
  212. `bottom_right` parameters:
  213. [source,console]
  214. --------------------------------------------------
  215. GET my_locations/_search
  216. {
  217. "query": {
  218. "geo_bounding_box" : {
  219. "pin.location" : {
  220. "top_left" : "dr",
  221. "bottom_right" : "dr"
  222. }
  223. }
  224. }
  225. }
  226. --------------------------------------------------
  227. In this example, the geohash `dr` will produce the bounding box
  228. query with the top left corner at `45.0,-78.75` and the bottom right
  229. corner at `39.375,-67.5`.
  230. [float]
  231. ==== Vertices
  232. The vertices of the bounding box can either be set by `top_left` and
  233. `bottom_right` or by `top_right` and `bottom_left` parameters. More
  234. over the names `topLeft`, `bottomRight`, `topRight` and `bottomLeft`
  235. are supported. Instead of setting the values pairwise, one can use
  236. the simple names `top`, `left`, `bottom` and `right` to set the
  237. values separately.
  238. [source,console]
  239. --------------------------------------------------
  240. GET my_locations/_search
  241. {
  242. "query": {
  243. "bool" : {
  244. "must" : {
  245. "match_all" : {}
  246. },
  247. "filter" : {
  248. "geo_bounding_box" : {
  249. "pin.location" : {
  250. "top" : 40.73,
  251. "left" : -74.1,
  252. "bottom" : 40.01,
  253. "right" : -71.12
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }
  260. --------------------------------------------------
  261. [float]
  262. ==== geo_point Type
  263. The filter *requires* the `geo_point` type to be set on the relevant
  264. field.
  265. [float]
  266. ==== Multi Location Per Document
  267. The filter can work with multiple locations / points per document. Once
  268. a single location / point matches the filter, the document will be
  269. included in the filter
  270. [float]
  271. [[geo-bbox-type]]
  272. ==== Type
  273. The type of the bounding box execution by default is set to `memory`,
  274. which means in memory checks if the doc falls within the bounding box
  275. range. In some cases, an `indexed` option will perform faster (but note
  276. that the `geo_point` type must have lat and lon indexed in this case).
  277. Note, when using the indexed option, multi locations per document field
  278. are not supported. Here is an example:
  279. [source,console]
  280. --------------------------------------------------
  281. GET my_locations/_search
  282. {
  283. "query": {
  284. "bool" : {
  285. "must" : {
  286. "match_all" : {}
  287. },
  288. "filter" : {
  289. "geo_bounding_box" : {
  290. "pin.location" : {
  291. "top_left" : {
  292. "lat" : 40.73,
  293. "lon" : -74.1
  294. },
  295. "bottom_right" : {
  296. "lat" : 40.10,
  297. "lon" : -71.12
  298. }
  299. },
  300. "type" : "indexed"
  301. }
  302. }
  303. }
  304. }
  305. }
  306. --------------------------------------------------
  307. [float]
  308. ==== Ignore Unmapped
  309. When set to `true` the `ignore_unmapped` option will ignore an unmapped field
  310. and will not match any documents for this query. This can be useful when
  311. querying multiple indexes which might have different mappings. When set to
  312. `false` (the default value) the query will throw an exception if the field
  313. is not mapped.
  314. [float]
  315. ==== Notes on Precision
  316. Geopoints have limited precision and are always rounded down during index time.
  317. During the query time, upper boundaries of the bounding boxes are rounded down,
  318. while lower boundaries are rounded up. As a result, the points along on the
  319. lower bounds (bottom and left edges of the bounding box) might not make it into
  320. the bounding box due to the rounding error. At the same time points alongside
  321. the upper bounds (top and right edges) might be selected by the query even if
  322. they are located slightly outside the edge. The rounding error should be less
  323. than 4.20e-8 degrees on the latitude and less than 8.39e-8 degrees on the
  324. longitude, which translates to less than 1cm error even at the equator.