geo-bounding-box-query.asciidoc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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` |Set to `true` to
  49. accept geo points with invalid latitude or longitude (default is `false`).
  50. |`type` |Set to one of `indexed` or `memory` to defines whether this filter will
  51. be executed in memory or indexed. See <<geo-bbox-type,Type>> below for further details
  52. Default is `memory`.
  53. |=======================================================================
  54. [float]
  55. ==== Accepted Formats
  56. In much the same way the geo_point type can accept different
  57. representation of the geo point, the filter can accept it as well:
  58. [float]
  59. ===== Lat Lon As Properties
  60. [source,js]
  61. --------------------------------------------------
  62. {
  63. "bool" : {
  64. "must" : {
  65. "match_all" : {}
  66. },
  67. "filter" : {
  68. "geo_bounding_box" : {
  69. "pin.location" : {
  70. "top_left" : {
  71. "lat" : 40.73,
  72. "lon" : -74.1
  73. },
  74. "bottom_right" : {
  75. "lat" : 40.01,
  76. "lon" : -71.12
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. --------------------------------------------------
  84. [float]
  85. ===== Lat Lon As Array
  86. Format in `[lon, lat]`, note, the order of lon/lat here in order to
  87. conform with http://geojson.org/[GeoJSON].
  88. [source,js]
  89. --------------------------------------------------
  90. {
  91. "bool" : {
  92. "must" : {
  93. "match_all" : {}
  94. },
  95. "filter" : {
  96. "geo_bounding_box" : {
  97. "pin.location" : {
  98. "top_left" : [-74.1, 40.73],
  99. "bottom_right" : [-71.12, 40.01]
  100. }
  101. }
  102. }
  103. }
  104. }
  105. --------------------------------------------------
  106. [float]
  107. ===== Lat Lon As String
  108. Format in `lat,lon`.
  109. [source,js]
  110. --------------------------------------------------
  111. {
  112. "bool" : {
  113. "must" : {
  114. "match_all" : {}
  115. },
  116. "filter" : {
  117. "geo_bounding_box" : {
  118. "pin.location" : {
  119. "top_left" : "40.73, -74.1",
  120. "bottom_right" : "40.01, -71.12"
  121. }
  122. }
  123. }
  124. }
  125. }
  126. --------------------------------------------------
  127. [float]
  128. ===== Geohash
  129. [source,js]
  130. --------------------------------------------------
  131. {
  132. "bool" : {
  133. "must" : {
  134. "match_all" : {}
  135. },
  136. "filter" : {
  137. "geo_bounding_box" : {
  138. "pin.location" : {
  139. "top_left" : "dr5r9ydj2y73",
  140. "bottom_right" : "drj7teegpus6"
  141. }
  142. }
  143. }
  144. }
  145. }
  146. --------------------------------------------------
  147. [float]
  148. ==== Vertices
  149. The vertices of the bounding box can either be set by `top_left` and
  150. `bottom_right` or by `top_right` and `bottom_left` parameters. More
  151. over the names `topLeft`, `bottomRight`, `topRight` and `bottomLeft`
  152. are supported. Instead of setting the values pairwise, one can use
  153. the simple names `top`, `left`, `bottom` and `right` to set the
  154. values separately.
  155. [source,js]
  156. --------------------------------------------------
  157. {
  158. "bool" : {
  159. "must" : {
  160. "match_all" : {}
  161. },
  162. "filter" : {
  163. "geo_bounding_box" : {
  164. "pin.location" : {
  165. "top" : 40.73,
  166. "left" : -74.1,
  167. "bottom" : 40.01,
  168. "right" : -71.12
  169. }
  170. }
  171. }
  172. }
  173. }
  174. --------------------------------------------------
  175. [float]
  176. ==== geo_point Type
  177. The filter *requires* the `geo_point` type to be set on the relevant
  178. field.
  179. [float]
  180. ==== Multi Location Per Document
  181. The filter can work with multiple locations / points per document. Once
  182. a single location / point matches the filter, the document will be
  183. included in the filter
  184. [float]
  185. [[geo-bbox-type]]
  186. ==== Type
  187. The type of the bounding box execution by default is set to `memory`,
  188. which means in memory checks if the doc falls within the bounding box
  189. range. In some cases, an `indexed` option will perform faster (but note
  190. that the `geo_point` type must have lat and lon indexed in this case).
  191. Note, when using the indexed option, multi locations per document field
  192. are not supported. Here is an example:
  193. [source,js]
  194. --------------------------------------------------
  195. {
  196. "bool" : {
  197. "must" : {
  198. "match_all" : {}
  199. },
  200. "filter" : {
  201. "geo_bounding_box" : {
  202. "pin.location" : {
  203. "top_left" : {
  204. "lat" : 40.73,
  205. "lon" : -74.1
  206. },
  207. "bottom_right" : {
  208. "lat" : 40.10,
  209. "lon" : -71.12
  210. }
  211. },
  212. "type" : "indexed"
  213. }
  214. }
  215. }
  216. }
  217. --------------------------------------------------