geo-distance-query.asciidoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. [[query-dsl-geo-distance-query]]
  2. === Geo Distance Query
  3. Filters documents that include only hits that exists within a specific
  4. distance from a geo point. Assuming the following mapping:
  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. --------------------------------------------------
  24. // CONSOLE
  25. ... and indexed document:
  26. [source,js]
  27. --------------------------------------------------
  28. PUT /my_locations/location/1
  29. {
  30. "pin" : {
  31. "location" : {
  32. "lat" : 40.12,
  33. "lon" : -71.34
  34. }
  35. }
  36. }
  37. --------------------------------------------------
  38. // CONSOLE
  39. // TEST[continued]
  40. Then the following simple query can be executed with a `geo_distance`
  41. filter:
  42. [source,js]
  43. --------------------------------------------------
  44. GET /my_locations/location/_search
  45. {
  46. "query": {
  47. "bool" : {
  48. "must" : {
  49. "match_all" : {}
  50. },
  51. "filter" : {
  52. "geo_distance" : {
  53. "distance" : "200km",
  54. "pin.location" : {
  55. "lat" : 40,
  56. "lon" : -70
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. --------------------------------------------------
  64. // CONSOLE
  65. // TEST[continued]
  66. [float]
  67. ==== Accepted Formats
  68. In much the same way the `geo_point` type can accept different
  69. representation of the geo point, the filter can accept it as well:
  70. [float]
  71. ===== Lat Lon As Properties
  72. [source,js]
  73. --------------------------------------------------
  74. GET /my_locations/location/_search
  75. {
  76. "query": {
  77. "bool" : {
  78. "must" : {
  79. "match_all" : {}
  80. },
  81. "filter" : {
  82. "geo_distance" : {
  83. "distance" : "12km",
  84. "pin.location" : {
  85. "lat" : 40,
  86. "lon" : -70
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. --------------------------------------------------
  94. // CONSOLE
  95. // TEST[continued]
  96. [float]
  97. ===== Lat Lon As Array
  98. Format in `[lon, lat]`, note, the order of lon/lat here in order to
  99. conform with http://geojson.org/[GeoJSON].
  100. [source,js]
  101. --------------------------------------------------
  102. GET /my_locations/location/_search
  103. {
  104. "query": {
  105. "bool" : {
  106. "must" : {
  107. "match_all" : {}
  108. },
  109. "filter" : {
  110. "geo_distance" : {
  111. "distance" : "12km",
  112. "pin.location" : [-70, 40]
  113. }
  114. }
  115. }
  116. }
  117. }
  118. --------------------------------------------------
  119. // CONSOLE
  120. // TEST[continued]
  121. [float]
  122. ===== Lat Lon As String
  123. Format in `lat,lon`.
  124. [source,js]
  125. --------------------------------------------------
  126. GET /my_locations/location/_search
  127. {
  128. "query": {
  129. "bool" : {
  130. "must" : {
  131. "match_all" : {}
  132. },
  133. "filter" : {
  134. "geo_distance" : {
  135. "distance" : "12km",
  136. "pin.location" : "40,-70"
  137. }
  138. }
  139. }
  140. }
  141. }
  142. --------------------------------------------------
  143. // CONSOLE
  144. // TEST[continued]
  145. [float]
  146. ===== Geohash
  147. [source,js]
  148. --------------------------------------------------
  149. GET /my_locations/location/_search
  150. {
  151. "query": {
  152. "bool" : {
  153. "must" : {
  154. "match_all" : {}
  155. },
  156. "filter" : {
  157. "geo_distance" : {
  158. "distance" : "12km",
  159. "pin.location" : "drm3btev3e86"
  160. }
  161. }
  162. }
  163. }
  164. }
  165. --------------------------------------------------
  166. // CONSOLE
  167. // TEST[continued]
  168. [float]
  169. ==== Options
  170. The following are options allowed on the filter:
  171. [horizontal]
  172. `distance`::
  173. The radius of the circle centred on the specified location. Points which
  174. fall into this circle are considered to be matches. The `distance` can be
  175. specified in various units. See <<distance-units>>.
  176. `distance_type`::
  177. How to compute the distance. Can either be `sloppy_arc` (default), `arc` (slightly more precise but significantly slower) or `plane` (faster, but inaccurate on long distances and close to the poles).
  178. `optimize_bbox`::
  179. Whether to use the optimization of first running a bounding box check
  180. before the distance check. Defaults to `memory` which will do in memory
  181. checks. Can also have values of `indexed` to use indexed value check (make
  182. sure the `geo_point` type index lat lon in this case), or `none` which
  183. disables bounding box optimization.
  184. `_name`::
  185. Optional name field to identify the query
  186. `ignore_malformed`::
  187. deprecated[5.0.0,Use `validation_method` instead] Set to `true` to accept geo points with invalid latitude or
  188. longitude (default is `false`).
  189. `validation_method`::
  190. Set to `IGNORE_MALFORMED` to accept geo points with invalid latitude or
  191. longitude, set to `COERCE` to additionally try and infer correct
  192. coordinates (default is `STRICT`).
  193. [float]
  194. ==== geo_point Type
  195. The filter *requires* the `geo_point` type to be set on the relevant
  196. field.
  197. [float]
  198. ==== Multi Location Per Document
  199. The `geo_distance` filter can work with multiple locations / points per
  200. document. Once a single location / point matches the filter, the
  201. document will be included in the filter.
  202. [float]
  203. ==== Ignore Unmapped
  204. When set to `true` the `ignore_unmapped` option will ignore an unmapped field
  205. and will not match any documents for this query. This can be useful when
  206. querying multiple indexes which might have different mappings. When set to
  207. `false` (the default value) the query will throw an exception if the field
  208. is not mapped.