script-score-query.asciidoc 12 KB

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