distance-feature-query.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. [[query-dsl-distance-feature-query]]
  2. === Distance feature query
  3. ++++
  4. <titleabbrev>Distance feature</titleabbrev>
  5. ++++
  6. Boosts the <<relevance-scores,relevance score>> of documents closer to a
  7. provided `origin` date or point. For example, you can use this query to give
  8. more weight to documents closer to a certain date or location.
  9. You can use the `distance_feature` query to find the nearest neighbors to a
  10. location. You can also use the query in a <<query-dsl-bool-query,`bool`>>
  11. search's `should` filter to add boosted relevance scores to the `bool` query's
  12. scores.
  13. [[distance-feature-query-ex-request]]
  14. ==== Example request
  15. [[distance-feature-index-setup]]
  16. ===== Index setup
  17. To use the `distance_feature` query, your index must include a <<date, `date`>>,
  18. <<date_nanos, `date_nanos`>> or <<geo-point,`geo_point`>> field.
  19. To see how you can set up an index for the `distance_feature` query, try the
  20. following example.
  21. . Create an `items` index with the following field mapping:
  22. +
  23. --
  24. * `name`, a <<keyword,`keyword`>> field
  25. * `production_date`, a <<date, `date`>> field
  26. * `location`, a <<geo-point,`geo_point`>> field
  27. [source,js]
  28. ----
  29. PUT /items
  30. {
  31. "mappings": {
  32. "properties": {
  33. "name": {
  34. "type": "keyword"
  35. },
  36. "production_date": {
  37. "type": "date"
  38. },
  39. "location": {
  40. "type": "geo_point"
  41. }
  42. }
  43. }
  44. }
  45. ----
  46. // CONSOLE
  47. // TESTSETUP
  48. --
  49. . Index several documents to this index.
  50. +
  51. --
  52. [source,js]
  53. ----
  54. PUT /items/_doc/1?refresh
  55. {
  56. "name" : "chocolate",
  57. "production_date": "2018-02-01",
  58. "location": [-71.34, 41.12]
  59. }
  60. PUT /items/_doc/2?refresh
  61. {
  62. "name" : "chocolate",
  63. "production_date": "2018-01-01",
  64. "location": [-71.3, 41.15]
  65. }
  66. PUT /items/_doc/3?refresh
  67. {
  68. "name" : "chocolate",
  69. "production_date": "2017-12-01",
  70. "location": [-71.3, 41.12]
  71. }
  72. ----
  73. // CONSOLE
  74. --
  75. [[distance-feature-query-ex-query]]
  76. ===== Example queries
  77. [[distance-feature-query-date-ex]]
  78. ====== Boost documents based on date
  79. The following `bool` search returns documents with a `name` value of
  80. `chocolate`. The search also uses the `distance_feature` query to increase the
  81. relevance score of documents with a `production_date` value closer to `now`.
  82. [source,js]
  83. ----
  84. GET /items/_search
  85. {
  86. "query": {
  87. "bool": {
  88. "must": {
  89. "match": {
  90. "name": "chocolate"
  91. }
  92. },
  93. "should": {
  94. "distance_feature": {
  95. "field": "production_date",
  96. "pivot": "7d",
  97. "origin": "now"
  98. }
  99. }
  100. }
  101. }
  102. }
  103. ----
  104. // CONSOLE
  105. [[distance-feature-query-distance-ex]]
  106. ====== Boost documents based on location
  107. The following `bool` search returns documents with a `name` value of
  108. `chocolate`. The search also uses the `distance_feature` query to increase the
  109. relevance score of documents with a `location` value closer to `[-71.3, 41.15]`.
  110. [source,js]
  111. ----
  112. GET /items/_search
  113. {
  114. "query": {
  115. "bool": {
  116. "must": {
  117. "match": {
  118. "name": "chocolate"
  119. }
  120. },
  121. "should": {
  122. "distance_feature": {
  123. "field": "location",
  124. "pivot": "1000m",
  125. "origin": [-71.3, 41.15]
  126. }
  127. }
  128. }
  129. }
  130. }
  131. ----
  132. // CONSOLE
  133. [[distance-feature-top-level-params]]
  134. ==== Top-level parameters for `distance_feature`
  135. `field`::
  136. (Required, string) Name of the field used to calculate distances. This field
  137. must meet the following criteria:
  138. * Be a <<date, `date`>>, <<date_nanos, `date_nanos`>> or
  139. <<geo-point,`geo_point`>> field
  140. * Have an <<mapping-index,`index`>> mapping parameter value of `true`, which is
  141. the default
  142. * Have an <<doc-values,`doc_values`>> mapping parameter value of `true`, which
  143. is the default
  144. `origin`::
  145. +
  146. --
  147. (Required, string) Date or point of origin used to calculate distances.
  148. If the `field` value is a <<date, `date`>> or <<date_nanos, `date_nanos`>>
  149. field, the `origin` value must be a <<date-format-pattern,date>>.
  150. <<date-math,Date Math>>, such as `now-1h`, is supported.
  151. If the `field` value is a <<geo-point,`geo_point`>> field, the `origin` value
  152. must be a geopoint.
  153. --
  154. `pivot`::
  155. +
  156. --
  157. (Required, <<time-units,time unit>> or <<distance-units,distance unit>>)
  158. Distance from the `origin` at which relevance scores receive half of the `boost`
  159. value.
  160. If the `field` value is a <<date, `date`>> or <<date_nanos, `date_nanos`>>
  161. field, the `pivot` value must be a <<time-units,time unit>>, such as `1h` or
  162. `10d`.
  163. If the `field` value is a <<geo-point,`geo_point`>> field, the `pivot` value
  164. must be a <<distance-units,distance unit>>, such as `1km` or `12m`.
  165. --
  166. `boost`::
  167. +
  168. --
  169. (Optional, float) Floating point number used to multiply the
  170. <<relevance-scores,relevance score>> of matching documents. This value
  171. cannot be negative. Defaults to `1.0`.
  172. --
  173. [[distance-feature-notes]]
  174. ==== Notes
  175. [[distance-feature-calculation]]
  176. ===== How the `distance_feature` query calculates relevance scores
  177. The `distance_feature` query dynamically calculates the distance between the
  178. `origin` value and a document's field values. It then uses this distance as a
  179. feature to boost the <<relevance-scores,relevance score>> of closer
  180. documents.
  181. The `distance_feature` query calculates a document's
  182. <<relevance-scores,relevance score>> as follows:
  183. ```
  184. relevance score = boost * pivot / (pivot + distance)
  185. ```
  186. The `distance` is the absolute difference between the `origin` value and a
  187. document's field value.
  188. [[distance-feature-skip-hits]]
  189. ===== Skip non-competitive hits
  190. Unlike the <<query-dsl-function-score-query,`function_score`>> query or other
  191. ways to change <<relevance-scores,relevance scores>>, the
  192. `distance_feature` query efficiently skips non-competitive hits when the
  193. <<search-uri-request,`track_total_hits`>> parameter is **not** `true`.