script-score-query.asciidoc 9.4 KB

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