geo-distance-query.asciidoc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "filtered" : {
  22. "query" : {
  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. "filtered" : {
  47. "query" : {
  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. "filtered" : {
  70. "query" : {
  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. "filtered" : {
  89. "query" : {
  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. "filtered" : {
  107. "query" : {
  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. [float]
  136. ==== geo_point Type
  137. The filter *requires* the `geo_point` type to be set on the relevant
  138. field.
  139. [float]
  140. ==== Multi Location Per Document
  141. The `geo_distance` filter can work with multiple locations / points per
  142. document. Once a single location / point matches the filter, the
  143. document will be included in the filter.