geo-distance-query.asciidoc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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,console]
  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. // TESTSETUP
  36. Then the following simple query can be executed with a `geo_distance`
  37. filter:
  38. [source,console]
  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. [float]
  61. ==== Accepted Formats
  62. In much the same way the `geo_point` type can accept different
  63. representations of the geo point, the filter can accept it as well:
  64. [float]
  65. ===== Lat Lon As Properties
  66. [source,console]
  67. --------------------------------------------------
  68. GET /my_locations/_search
  69. {
  70. "query": {
  71. "bool" : {
  72. "must" : {
  73. "match_all" : {}
  74. },
  75. "filter" : {
  76. "geo_distance" : {
  77. "distance" : "12km",
  78. "pin.location" : {
  79. "lat" : 40,
  80. "lon" : -70
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. --------------------------------------------------
  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,console]
  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. [float]
  112. ===== Lat Lon As String
  113. Format in `lat,lon`.
  114. [source,console]
  115. --------------------------------------------------
  116. GET /my_locations/_search
  117. {
  118. "query": {
  119. "bool" : {
  120. "must" : {
  121. "match_all" : {}
  122. },
  123. "filter" : {
  124. "geo_distance" : {
  125. "distance" : "12km",
  126. "pin.location" : "40,-70"
  127. }
  128. }
  129. }
  130. }
  131. }
  132. --------------------------------------------------
  133. [float]
  134. ===== Geohash
  135. [source,console]
  136. --------------------------------------------------
  137. GET /my_locations/_search
  138. {
  139. "query": {
  140. "bool" : {
  141. "must" : {
  142. "match_all" : {}
  143. },
  144. "filter" : {
  145. "geo_distance" : {
  146. "distance" : "12km",
  147. "pin.location" : "drm3btev3e86"
  148. }
  149. }
  150. }
  151. }
  152. }
  153. --------------------------------------------------
  154. [float]
  155. ==== Options
  156. The following are options allowed on the filter:
  157. [horizontal]
  158. `distance`::
  159. The radius of the circle centred on the specified location. Points which
  160. fall into this circle are considered to be matches. The `distance` can be
  161. specified in various units. See <<distance-units>>.
  162. `distance_type`::
  163. How to compute the distance. Can either be `arc` (default), or `plane` (faster, but inaccurate on long distances and close to the poles).
  164. `_name`::
  165. Optional name field to identify the query
  166. `validation_method`::
  167. Set to `IGNORE_MALFORMED` to accept geo points with invalid latitude or
  168. longitude, set to `COERCE` to additionally try and infer correct
  169. coordinates (default is `STRICT`).
  170. [float]
  171. ==== geo_point Type
  172. The filter *requires* the `geo_point` type to be set on the relevant
  173. field.
  174. [float]
  175. ==== Multi Location Per Document
  176. The `geo_distance` filter can work with multiple locations / points per
  177. document. Once a single location / point matches the filter, the
  178. document will be included in the filter.
  179. [float]
  180. ==== Ignore Unmapped
  181. When set to `true` the `ignore_unmapped` option will ignore an unmapped field
  182. and will not match any documents for this query. This can be useful when
  183. querying multiple indexes which might have different mappings. When set to
  184. `false` (the default value) the query will throw an exception if the field
  185. is not mapped.