geo-distance-query.asciidoc 4.7 KB

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