geo-bounding-box-query.asciidoc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. "location": {
  11. "properties": {
  12. "pin": {
  13. "properties": {
  14. "location": {
  15. "type": "geo_point"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }
  23. PUT /my_locations/location/1
  24. {
  25. "pin" : {
  26. "location" : {
  27. "lat" : 40.12,
  28. "lon" : -71.34
  29. }
  30. }
  31. }
  32. --------------------------------------------------
  33. // CONSOLE
  34. // TESTSETUP
  35. Then the following simple query can be executed with a
  36. `geo_bounding_box` filter:
  37. [source,js]
  38. --------------------------------------------------
  39. GET /_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. // CONSOLE
  65. [float]
  66. ==== Query Options
  67. [cols="<,<",options="header",]
  68. |=======================================================================
  69. |Option |Description
  70. |`_name` |Optional name field to identify the filter
  71. |`ignore_malformed` |deprecated[5.0.0,Use `validation_method` instead] Set to `true` to
  72. accept geo points with invalid latitude or longitude (default is `false`).
  73. |`validation_method` |Set to `IGNORE_MALFORMED` to
  74. accept geo points with invalid latitude or longitude, set to
  75. `COERCE` to also try to infer correct latitude or longitude. (default is `STRICT`).
  76. |`type` |Set to one of `indexed` or `memory` to defines whether this filter will
  77. be executed in memory or indexed. See <<geo-bbox-type,Type>> below for further details
  78. Default is `memory`.
  79. |=======================================================================
  80. [float]
  81. ==== Accepted Formats
  82. In much the same way the geo_point type can accept different
  83. representation of the geo point, the filter can accept it as well:
  84. [float]
  85. ===== Lat Lon As Properties
  86. [source,js]
  87. --------------------------------------------------
  88. GET /_search
  89. {
  90. "query": {
  91. "bool" : {
  92. "must" : {
  93. "match_all" : {}
  94. },
  95. "filter" : {
  96. "geo_bounding_box" : {
  97. "pin.location" : {
  98. "top_left" : {
  99. "lat" : 40.73,
  100. "lon" : -74.1
  101. },
  102. "bottom_right" : {
  103. "lat" : 40.01,
  104. "lon" : -71.12
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. --------------------------------------------------
  113. // CONSOLE
  114. [float]
  115. ===== Lat Lon As Array
  116. Format in `[lon, lat]`, note, the order of lon/lat here in order to
  117. conform with http://geojson.org/[GeoJSON].
  118. [source,js]
  119. --------------------------------------------------
  120. GET /_search
  121. {
  122. "query": {
  123. "bool" : {
  124. "must" : {
  125. "match_all" : {}
  126. },
  127. "filter" : {
  128. "geo_bounding_box" : {
  129. "pin.location" : {
  130. "top_left" : [-74.1, 40.73],
  131. "bottom_right" : [-71.12, 40.01]
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. --------------------------------------------------
  139. // CONSOLE
  140. [float]
  141. ===== Lat Lon As String
  142. Format in `lat,lon`.
  143. [source,js]
  144. --------------------------------------------------
  145. GET /_search
  146. {
  147. "query": {
  148. "bool" : {
  149. "must" : {
  150. "match_all" : {}
  151. },
  152. "filter" : {
  153. "geo_bounding_box" : {
  154. "pin.location" : {
  155. "top_left" : "40.73, -74.1",
  156. "bottom_right" : "40.01, -71.12"
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. --------------------------------------------------
  164. // CONSOLE
  165. [float]
  166. ===== Geohash
  167. [source,js]
  168. --------------------------------------------------
  169. GET /_search
  170. {
  171. "query": {
  172. "bool" : {
  173. "must" : {
  174. "match_all" : {}
  175. },
  176. "filter" : {
  177. "geo_bounding_box" : {
  178. "pin.location" : {
  179. "top_left" : "dr5r9ydj2y73",
  180. "bottom_right" : "drj7teegpus6"
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }
  187. --------------------------------------------------
  188. // CONSOLE
  189. [float]
  190. ==== Vertices
  191. The vertices of the bounding box can either be set by `top_left` and
  192. `bottom_right` or by `top_right` and `bottom_left` parameters. More
  193. over the names `topLeft`, `bottomRight`, `topRight` and `bottomLeft`
  194. are supported. Instead of setting the values pairwise, one can use
  195. the simple names `top`, `left`, `bottom` and `right` to set the
  196. values separately.
  197. [source,js]
  198. --------------------------------------------------
  199. GET /_search
  200. {
  201. "query": {
  202. "bool" : {
  203. "must" : {
  204. "match_all" : {}
  205. },
  206. "filter" : {
  207. "geo_bounding_box" : {
  208. "pin.location" : {
  209. "top" : 40.73,
  210. "left" : -74.1,
  211. "bottom" : 40.01,
  212. "right" : -71.12
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. --------------------------------------------------
  220. // CONSOLE
  221. [float]
  222. ==== geo_point Type
  223. The filter *requires* the `geo_point` type to be set on the relevant
  224. field.
  225. [float]
  226. ==== Multi Location Per Document
  227. The filter can work with multiple locations / points per document. Once
  228. a single location / point matches the filter, the document will be
  229. included in the filter
  230. [float]
  231. [[geo-bbox-type]]
  232. ==== Type
  233. The type of the bounding box execution by default is set to `memory`,
  234. which means in memory checks if the doc falls within the bounding box
  235. range. In some cases, an `indexed` option will perform faster (but note
  236. that the `geo_point` type must have lat and lon indexed in this case).
  237. Note, when using the indexed option, multi locations per document field
  238. are not supported. Here is an example:
  239. [source,js]
  240. --------------------------------------------------
  241. GET /_search
  242. {
  243. "query": {
  244. "bool" : {
  245. "must" : {
  246. "match_all" : {}
  247. },
  248. "filter" : {
  249. "geo_bounding_box" : {
  250. "pin.location" : {
  251. "top_left" : {
  252. "lat" : 40.73,
  253. "lon" : -74.1
  254. },
  255. "bottom_right" : {
  256. "lat" : 40.10,
  257. "lon" : -71.12
  258. }
  259. },
  260. "type" : "indexed"
  261. }
  262. }
  263. }
  264. }
  265. }
  266. --------------------------------------------------
  267. // CONSOLE
  268. [float]
  269. ==== Ignore Unmapped
  270. When set to `true` the `ignore_unmapped` option will ignore an unmapped field
  271. and will not match any documents for this query. This can be useful when
  272. querying multiple indexes which might have different mappings. When set to
  273. `false` (the default value) the query will throw an exception if the field
  274. is not mapped.