34_Geo_distance.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. [[geo-distance]]
  2. === `geo_distance` filter
  3. The `geo_distance` filter draws a circle around the specified location and
  4. finds all documents that have a geo-point within that circle:
  5. [source,json]
  6. ---------------------
  7. GET /attractions/restaurant/_search
  8. {
  9. "query": {
  10. "filtered": {
  11. "filter": {
  12. "geo_distance": {
  13. "distance": "1km", <1>
  14. "location": { <2>
  15. "lat": 40.715,
  16. "lon": -73.988
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }
  23. ---------------------
  24. <1> Find all `location` fields within `1km` of the specified point.
  25. See {ref}common-options.html#distance-units[Distance Units] for
  26. a list of the accepted units.
  27. <2> The central point can be specified as a string, an array, or (as in this
  28. example) as an object. See <<lat-lon-formats>>.
  29. A geo-distance calculation is expensive. To optimize performance,
  30. Elasticsearch draws a box around the circle and first uses the less expensive
  31. bounding-box calculation to exclude as many documents as it can. It only runs
  32. the geo-distance calculation on those points that fall within the bounding
  33. box.
  34. TIP: Do your users really require an accurate circular filter to be applied to
  35. their results? Using a rectangular <<geo-bounding-box,bounding box>> is much
  36. more efficient than geo-distance and will usually serve their purposes just as
  37. well.
  38. ==== Faster geo-distance calculations
  39. The distance between two points can be calculated using different algorithms,
  40. which trade performance for accuracy.
  41. `arc`::
  42. The slowest but most accurate is the `arc` calculation, which treats the world
  43. as a sphere. Accuracy is still limited because the world isn't really a sphere.
  44. `plane`::
  45. The `plane` calculation, which treats the world as if it were flat, is faster
  46. but less accurate. It is most accurate at the equator and becomes less
  47. accurate towards the poles.
  48. `sloppy_arc`::
  49. So called because it uses the `SloppyMath` Lucene class to trade accuracy for speed,
  50. the `sloppy_arc` calculation uses the
  51. http://en.wikipedia.org/wiki/Haversine_formula[Haversine formula] to calculate
  52. distance. It is four to five times as fast as `arc`, and distances are 99.9% accurate.
  53. This is the default calculation.
  54. You can specify a different calculation as follows:
  55. [source,json]
  56. ---------------------
  57. GET /attractions/restaurant/_search
  58. {
  59. "query": {
  60. "filtered": {
  61. "filter": {
  62. "geo_distance": {
  63. "distance": "1km",
  64. "distance_type": "plane", <1>
  65. "location": {
  66. "lat": 40.715,
  67. "lon": -73.988
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. ---------------------
  75. <1> Use the faster but less accurate `plane` calculation.
  76. TIP: Will your users really care if a restaurant is a few metres outside of
  77. their specified radius? While some geo applications require great accuracy,
  78. less accurate but faster calculations will suit the majority of use cases just
  79. fine.
  80. [[geo-distance-range]]
  81. ==== `geo_distance_range` filter
  82. The only difference between the `geo_distance` and `geo_distance_range`
  83. filters is that the latter has a doughnut shape and excludes documents within
  84. the central hole.
  85. Instead of specifying a single `distance` from the centre, you specify a
  86. minimum distance (with `gt` or `gte`) and maximum distance (with `lt` or
  87. `lte`), just like a `range` filter:
  88. [source,json]
  89. ---------------------
  90. GET /attractions/restaurant/_search
  91. {
  92. "query": {
  93. "filtered": {
  94. "filter": {
  95. "geo_distance_range": {
  96. "gte": "1km", <1>
  97. "lt": "2km", <1>
  98. "location": {
  99. "lat": 40.715,
  100. "lon": -73.988
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. ---------------------
  108. <1> Matches locations which are at least `1km` from the centre, and less than
  109. `2km` from the centre.