1
0

using.asciidoc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. [[modules-scripting-using]]
  2. == How to use scripts
  3. Wherever scripting is supported in the Elasticsearch API, the syntax follows
  4. the same pattern:
  5. [source,js]
  6. -------------------------------------
  7. "script": {
  8. "lang": "...", <1>
  9. "source" | "id": "...", <2>
  10. "params": { ... } <3>
  11. }
  12. -------------------------------------
  13. // NOTCONSOLE
  14. <1> The language the script is written in, which defaults to `painless`.
  15. <2> The script itself which may be specified as `source` for an inline script or `id` for a stored script.
  16. <3> Any named parameters that should be passed into the script.
  17. For example, the following script is used in a search request to return a
  18. <<request-body-search-script-fields, scripted field>>:
  19. [source,console]
  20. -------------------------------------
  21. PUT my_index/_doc/1
  22. {
  23. "my_field": 5
  24. }
  25. GET my_index/_search
  26. {
  27. "script_fields": {
  28. "my_doubled_field": {
  29. "script": {
  30. "lang": "expression",
  31. "source": "doc['my_field'] * multiplier",
  32. "params": {
  33. "multiplier": 2
  34. }
  35. }
  36. }
  37. }
  38. }
  39. -------------------------------------
  40. [float]
  41. === Script parameters
  42. `lang`::
  43. Specifies the language the script is written in. Defaults to `painless`.
  44. `source`, `id`::
  45. Specifies the source of the script. An `inline` script is specified
  46. `source` as in the example above. A `stored` script is specified `id`
  47. and is retrieved from the cluster state (see <<modules-scripting-stored-scripts,Stored Scripts>>).
  48. `params`::
  49. Specifies any named parameters that are passed into the script as
  50. variables.
  51. [IMPORTANT]
  52. [[prefer-params]]
  53. .Prefer parameters
  54. ========================================
  55. The first time Elasticsearch sees a new script, it compiles it and stores the
  56. compiled version in a cache. Compilation can be a heavy process.
  57. If you need to pass variables into the script, you should pass them in as
  58. named `params` instead of hard-coding values into the script itself. For
  59. example, if you want to be able to multiply a field value by different
  60. multipliers, don't hard-code the multiplier into the script:
  61. [source,js]
  62. ----------------------
  63. "source": "doc['my_field'] * 2"
  64. ----------------------
  65. // NOTCONSOLE
  66. Instead, pass it in as a named parameter:
  67. [source,js]
  68. ----------------------
  69. "source": "doc['my_field'] * multiplier",
  70. "params": {
  71. "multiplier": 2
  72. }
  73. ----------------------
  74. // NOTCONSOLE
  75. The first version has to be recompiled every time the multiplier changes. The
  76. second version is only compiled once.
  77. If you compile too many unique scripts within a small amount of time,
  78. Elasticsearch will reject the new dynamic scripts with a
  79. `circuit_breaking_exception` error. By default, up to 15 inline scripts per
  80. minute will be compiled. You can change this setting dynamically by setting
  81. `script.max_compilations_rate`.
  82. ========================================
  83. [float]
  84. [[modules-scripting-short-script-form]]
  85. === Short script form
  86. A short script form can be used for brevity. In the short form, `script` is represented
  87. by a string instead of an object. This string contains the source of the script.
  88. Short form:
  89. [source,js]
  90. ----------------------
  91. "script": "ctx._source.likes++"
  92. ----------------------
  93. // NOTCONSOLE
  94. The same script in the normal form:
  95. [source,js]
  96. ----------------------
  97. "script": {
  98. "source": "ctx._source.likes++"
  99. }
  100. ----------------------
  101. // NOTCONSOLE
  102. [float]
  103. [[modules-scripting-stored-scripts]]
  104. === Stored scripts
  105. Scripts may be stored in and retrieved from the cluster state using the
  106. `_scripts` end-point.
  107. [float]
  108. ==== Request examples
  109. The following are examples of using a stored script that lives at
  110. `/_scripts/{id}`.
  111. First, create the script called `calculate-score` in the cluster state:
  112. [source,console]
  113. -----------------------------------
  114. POST _scripts/calculate-score
  115. {
  116. "script": {
  117. "lang": "painless",
  118. "source": "Math.log(_score * 2) + params.my_modifier"
  119. }
  120. }
  121. -----------------------------------
  122. // TEST[setup:twitter]
  123. You may also specify a context as part of the url path to compile a
  124. stored script against that specific context in the form of
  125. `/_scripts/{id}/{context}`:
  126. [source,console]
  127. -----------------------------------
  128. POST _scripts/calculate-score/score
  129. {
  130. "script": {
  131. "lang": "painless",
  132. "source": "Math.log(_score * 2) + params.my_modifier"
  133. }
  134. }
  135. -----------------------------------
  136. // TEST[setup:twitter]
  137. This same script can be retrieved with:
  138. [source,console]
  139. -----------------------------------
  140. GET _scripts/calculate-score
  141. -----------------------------------
  142. // TEST[continued]
  143. Stored scripts can be used by specifying the `id` parameters as follows:
  144. [source,console]
  145. --------------------------------------------------
  146. GET twitter/_search
  147. {
  148. "query": {
  149. "script_score": {
  150. "query": {
  151. "match": {
  152. "message": "some message"
  153. }
  154. },
  155. "script": {
  156. "id": "calculate-score",
  157. "params": {
  158. "my_modifier": 2
  159. }
  160. }
  161. }
  162. }
  163. }
  164. --------------------------------------------------
  165. // TEST[continued]
  166. And deleted with:
  167. [source,console]
  168. -----------------------------------
  169. DELETE _scripts/calculate-score
  170. -----------------------------------
  171. // TEST[continued]
  172. [float]
  173. [[modules-scripting-search-templates]]
  174. === Search templates
  175. You can also use the `_scripts` API to store **search templates**. Search
  176. templates save specific <<search-search,search requests>> with placeholder
  177. values, called template parameters.
  178. You can use stored search templates to run searches without writing out the
  179. entire query. Just provide the stored template's ID and the template parameters.
  180. This is useful when you want to run a commonly used query quickly and without
  181. mistakes.
  182. Search templates use the http://mustache.github.io/mustache.5.html[mustache
  183. templating language]. See <<search-template>> for more information and examples.
  184. [float]
  185. [[modules-scripting-using-caching]]
  186. === Script caching
  187. All scripts are cached by default so that they only need to be recompiled
  188. when updates occur. By default, scripts do not have a time-based expiration, but
  189. you can change this behavior by using the `script.cache.expire` setting.
  190. You can configure the size of this cache by using the `script.cache.max_size` setting.
  191. By default, the cache size is `100`.
  192. NOTE: The size of scripts is limited to 65,535 bytes. This can be
  193. changed by setting `script.max_size_in_bytes` setting to increase that soft
  194. limit, but if scripts are really large then a
  195. <<modules-scripting-engine,native script engine>> should be considered.
  196. [float]
  197. [[modules-scripting-errors]]
  198. === Script errors
  199. Elasticsearch returns error details when there is a compliation or runtime
  200. exception. The contents of this response are useful for tracking down the
  201. problem.
  202. experimental[]
  203. The contents of `position` are experimental and subject to change.