geo-distance-query.asciidoc 5.1 KB

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