geo-bounding-box-query.asciidoc 6.7 KB

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