script-score-query.asciidoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. [[query-dsl-script-score-query]]
  2. === Script Score Query
  3. The `script_score` allows you to modify the score of documents that are
  4. retrieved by a query. This can be useful if, for example, a score
  5. function is computationally expensive and it is sufficient to compute
  6. the score on a filtered set of documents.
  7. To use `script_score`, you have to define a query and a script -
  8. a function to be used to compute a new score for each document returned
  9. by the query. For more information on scripting see
  10. <<modules-scripting, scripting documentation>>.
  11. Here is an example of using `script_score` to assign each matched document
  12. a score equal to the number of likes divided by 10:
  13. [source,js]
  14. --------------------------------------------------
  15. GET /_search
  16. {
  17. "query" : {
  18. "script_score" : {
  19. "query" : {
  20. "match": { "message": "elasticsearch" }
  21. },
  22. "script" : {
  23. "source" : "doc['likes'].value / 10 "
  24. }
  25. }
  26. }
  27. }
  28. --------------------------------------------------
  29. // CONSOLE
  30. NOTE: The values returned from `script_score` cannot be negative. In general,
  31. Lucene requires the scores produced by queries to be non-negative in order to
  32. support certain search optimizations.
  33. ==== Accessing the score of a document within a script
  34. Within a script, you can
  35. {ref}/modules-scripting-fields.html#scripting-score[access]
  36. the `_score` variable which represents the current relevance score of a
  37. document.
  38. ==== Predefined functions within a Painless script
  39. You can use any of the available
  40. <<painless-api-reference, painless functions>> in the painless script.
  41. Besides these functions, there are a number of predefined functions
  42. that can help you with scoring. We suggest you to use them instead of
  43. rewriting equivalent functions of your own, as these functions try
  44. to be the most efficient by using the internal mechanisms.
  45. ===== saturation
  46. `saturation(value,k) = value/(k + value)`
  47. [source,js]
  48. --------------------------------------------------
  49. "script" : {
  50. "source" : "saturation(doc['likes'].value, 1)"
  51. }
  52. --------------------------------------------------
  53. // NOTCONSOLE
  54. ===== sigmoid
  55. `sigmoid(value, k, a) = value^a/ (k^a + value^a)`
  56. [source,js]
  57. --------------------------------------------------
  58. "script" : {
  59. "source" : "sigmoid(doc['likes'].value, 2, 1)"
  60. }
  61. --------------------------------------------------
  62. // NOTCONSOLE
  63. [[random-score-function]]
  64. ===== Random score function
  65. `random_score` function generates scores that are uniformly distributed
  66. from 0 up to but not including 1.
  67. `randomScore` function has the following syntax:
  68. `randomScore(<seed>, <fieldName>)`.
  69. It has a required parameter - `seed` as an integer value,
  70. and an optional parameter - `fieldName` as a string value.
  71. [source,js]
  72. --------------------------------------------------
  73. "script" : {
  74. "source" : "randomScore(100, '_seq_no')"
  75. }
  76. --------------------------------------------------
  77. // NOTCONSOLE
  78. If the `fieldName` parameter is omitted, the internal Lucene
  79. document ids will be used as a source of randomness. This is very efficient,
  80. but unfortunately not reproducible since documents might be renumbered
  81. by merges.
  82. [source,js]
  83. --------------------------------------------------
  84. "script" : {
  85. "source" : "randomScore(100)"
  86. }
  87. --------------------------------------------------
  88. // NOTCONSOLE
  89. Note that documents that are within the same shard and have the
  90. same value for field will get the same score, so it is usually desirable
  91. to use a field that has unique values for all documents across a shard.
  92. A good default choice might be to use the `_seq_no`
  93. field, whose only drawback is that scores will change if the document is
  94. updated since update operations also update the value of the `_seq_no` field.
  95. [[decay-functions-numeric-fields]]
  96. ===== Decay functions for numeric fields
  97. You can read more about decay functions
  98. {ref}/query-dsl-function-score-query.html#function-decay[here].
  99. * `double decayNumericLinear(double origin, double scale, double offset, double decay, double docValue)`
  100. * `double decayNumericExp(double origin, double scale, double offset, double decay, double docValue)`
  101. * `double decayNumericGauss(double origin, double scale, double offset, double decay, double docValue)`
  102. [source,js]
  103. --------------------------------------------------
  104. "script" : {
  105. "source" : "decayNumericLinear(params.origin, params.scale, params.offset, params.decay, doc['dval'].value)",
  106. "params": { <1>
  107. "origin": 20,
  108. "scale": 10,
  109. "decay" : 0.5,
  110. "offset" : 0
  111. }
  112. }
  113. --------------------------------------------------
  114. // NOTCONSOLE
  115. <1> Using `params` allows to compile the script only once, even if params change.
  116. ===== Decay functions for geo fields
  117. * `double decayGeoLinear(String originStr, String scaleStr, String offsetStr, double decay, GeoPoint docValue)`
  118. * `double decayGeoExp(String originStr, String scaleStr, String offsetStr, double decay, GeoPoint docValue)`
  119. * `double decayGeoGauss(String originStr, String scaleStr, String offsetStr, double decay, GeoPoint docValue)`
  120. [source,js]
  121. --------------------------------------------------
  122. "script" : {
  123. "source" : "decayGeoExp(params.origin, params.scale, params.offset, params.decay, doc['location'].value)",
  124. "params": {
  125. "origin": "40, -70.12",
  126. "scale": "200km",
  127. "offset": "0km",
  128. "decay" : 0.2
  129. }
  130. }
  131. --------------------------------------------------
  132. // NOTCONSOLE
  133. ===== Decay functions for date fields
  134. * `double decayDateLinear(String originStr, String scaleStr, String offsetStr, double decay, JodaCompatibleZonedDateTime docValueDate)`
  135. * `double decayDateExp(String originStr, String scaleStr, String offsetStr, double decay, JodaCompatibleZonedDateTime docValueDate)`
  136. * `double decayDateGauss(String originStr, String scaleStr, String offsetStr, double decay, JodaCompatibleZonedDateTime docValueDate)`
  137. [source,js]
  138. --------------------------------------------------
  139. "script" : {
  140. "source" : "decayDateGauss(params.origin, params.scale, params.offset, params.decay, doc['date'].value)",
  141. "params": {
  142. "origin": "2008-01-01T01:00:00Z",
  143. "scale": "1h",
  144. "offset" : "0",
  145. "decay" : 0.5
  146. }
  147. }
  148. --------------------------------------------------
  149. // NOTCONSOLE
  150. NOTE: Decay functions on dates are limited to dates in the default format
  151. and default time zone. Also calculations with `now` are not supported.
  152. ===== Functions for vector fields
  153. <<vector-functions, Functions for vector fields>> are accessible through
  154. `script_score` query.
  155. ==== Faster alternatives
  156. Script Score Query calculates the score for every hit (matching document).
  157. There are faster alternative query types that can efficiently skip
  158. non-competitive hits:
  159. * If you want to boost documents on some static fields, use
  160. <<query-dsl-rank-feature-query, Rank Feature Query>>.
  161. ==== Transition from Function Score Query
  162. We are deprecating <<query-dsl-function-score-query, Function Score>>, and
  163. Script Score Query will be a substitute for it.
  164. Here we describe how Function Score Query's functions can be
  165. equivalently implemented in Script Score Query:
  166. [[script-score]]
  167. ===== `script_score`
  168. What you used in `script_score` of the Function Score query, you
  169. can copy into the Script Score query. No changes here.
  170. [[weight]]
  171. ===== `weight`
  172. `weight` function can be implemented in the Script Score query through
  173. the following script:
  174. [source,js]
  175. --------------------------------------------------
  176. "script" : {
  177. "source" : "params.weight * _score",
  178. "params": {
  179. "weight": 2
  180. }
  181. }
  182. --------------------------------------------------
  183. // NOTCONSOLE
  184. [[random-score]]
  185. ===== `random_score`
  186. Use `randomScore` function
  187. as described in <<random-score-function, random score function>>.
  188. [[field-value-factor]]
  189. ===== `field_value_factor`
  190. `field_value_factor` function can be easily implemented through script:
  191. [source,js]
  192. --------------------------------------------------
  193. "script" : {
  194. "source" : "Math.log10(doc['field'].value * params.factor)",
  195. params" : {
  196. "factor" : 5
  197. }
  198. }
  199. --------------------------------------------------
  200. // NOTCONSOLE
  201. For checking if a document has a missing value, you can use
  202. `doc['field'].size() == 0`. For example, this script will use
  203. a value `1` if a document doesn't have a field `field`:
  204. [source,js]
  205. --------------------------------------------------
  206. "script" : {
  207. "source" : "Math.log10((doc['field'].size() == 0 ? 1 : doc['field'].value()) * params.factor)",
  208. params" : {
  209. "factor" : 5
  210. }
  211. }
  212. --------------------------------------------------
  213. // NOTCONSOLE
  214. This table lists how `field_value_factor` modifiers can be implemented
  215. through a script:
  216. [cols="<,<",options="header",]
  217. |=======================================================================
  218. | Modifier | Implementation in Script Score
  219. | `none` | -
  220. | `log` | `Math.log10(doc['f'].value)`
  221. | `log1p` | `Math.log10(doc['f'].value + 1)`
  222. | `log2p` | `Math.log10(doc['f'].value + 2)`
  223. | `ln` | `Math.log(doc['f'].value)`
  224. | `ln1p` | `Math.log(doc['f'].value + 1)`
  225. | `ln2p` | `Math.log(doc['f'].value + 2)`
  226. | `square` | `Math.pow(doc['f'].value, 2)`
  227. | `sqrt` | `Math.sqrt(doc['f'].value)`
  228. | `reciprocal` | `1.0 / doc['f'].value`
  229. |=======================================================================
  230. [[decay-functions]]
  231. ===== `decay functions`
  232. Script Score query has equivalent <<decay-functions, decay functions>>
  233. that can be used in script.
  234. include::{es-repo-dir}/vectors/vector-functions.asciidoc[]