geo-distance-query.asciidoc 5.1 KB

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