geo-distance-query.asciidoc 5.0 KB

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