1
0

using.asciidoc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. <<search-request-script-fields, scripted field>>:
  19. [source,js]
  20. -------------------------------------
  21. PUT my_index/my_type/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. // CONSOLE
  41. [float]
  42. === Script Parameters
  43. `lang`::
  44. Specifies the language the script is written in. Defaults to `painless` but
  45. may be set to any of languages listed in <<modules-scripting>>. The
  46. default language may be changed in the `elasticsearch.yml` config file by
  47. setting `script.default_lang` to the appropriate language.
  48. `source`, `id`::
  49. Specifies the source of the script. An `inline` script is specified
  50. `source` as in the example above. A `stored` script is specified `id`
  51. and is retrieved from the cluster state (see <<modules-scripting-stored-scripts,Stored Scripts>>).
  52. `params`::
  53. Specifies any named parameters that are passed into the script as
  54. variables.
  55. [IMPORTANT]
  56. [[prefer-params]]
  57. .Prefer parameters
  58. ========================================
  59. The first time Elasticsearch sees a new script, it compiles it and stores the
  60. compiled version in a cache. Compilation can be a heavy process.
  61. If you need to pass variables into the script, you should pass them in as
  62. named `params` instead of hard-coding values into the script itself. For
  63. example, if you want to be able to multiply a field value by different
  64. multipliers, don't hard-code the multiplier into the script:
  65. [source,js]
  66. ----------------------
  67. "source": "doc['my_field'] * 2"
  68. ----------------------
  69. // NOTCONSOLE
  70. Instead, pass it in as a named parameter:
  71. [source,js]
  72. ----------------------
  73. "source": "doc['my_field'] * multiplier",
  74. "params": {
  75. "multiplier": 2
  76. }
  77. ----------------------
  78. // NOTCONSOLE
  79. The first version has to be recompiled every time the multiplier changes. The
  80. second version is only compiled once.
  81. If you compile too many unique scripts within a small amount of time,
  82. Elasticsearch will reject the new dynamic scripts with a
  83. `circuit_breaking_exception` error. By default, up to 15 inline scripts per
  84. minute will be compiled. You can change this setting dynamically by setting
  85. `script.max_compilations_per_minute`.
  86. ========================================
  87. [float]
  88. [[modules-scripting-stored-scripts]]
  89. === Stored Scripts
  90. Scripts may be stored in and retrieved from the cluster state using the
  91. `_scripts` end-point.
  92. ==== Deprecated Namespace
  93. The namespace for stored scripts using both `lang` and `id` as a unique
  94. identifier has been deprecated. The new namespace for stored scripts will
  95. only use `id`. Stored scripts with the same `id`, but different `lang`'s
  96. will no longer be allowed in 6.0. To comply with the new namespace for
  97. stored scripts, existing stored scripts should be deleted and put again.
  98. Any scripts that share an `id` but have different `lang`s will need to
  99. be re-named. For example, take the following:
  100. "id": "example", "lang": "painless"
  101. "id": "example", "lang": "expressions"
  102. The above scripts will conflict under the new namespace since the id's are
  103. the same. At least one will have to be re-named to comply with the new
  104. namespace of only `id`.
  105. As a final caveat, stored search templates and stored scripts share
  106. the same namespace, so if a search template has the same `id` as a
  107. stored script, one of the two will have to be re-named as well using
  108. delete and put requests.
  109. ==== Request Examples
  110. The following are examples of using a stored script that lives at
  111. `/_scripts/{id}`.
  112. First, create the script called `calculate-score` in the cluster state:
  113. [source,js]
  114. -----------------------------------
  115. POST _scripts/calculate-score
  116. {
  117. "script": {
  118. "lang": "painless",
  119. "source": "Math.log(_score * 2) + params.my_modifier"
  120. }
  121. }
  122. -----------------------------------
  123. // CONSOLE
  124. This same script can be retrieved with:
  125. [source,js]
  126. -----------------------------------
  127. GET _scripts/calculate-score
  128. -----------------------------------
  129. // CONSOLE
  130. // TEST[continued]
  131. Stored scripts can be used by specifying the `id` parameters as follows:
  132. [source,js]
  133. --------------------------------------------------
  134. GET _search
  135. {
  136. "query": {
  137. "script": {
  138. "script": {
  139. "id": "calculate-score",
  140. "params": {
  141. "my_modifier": 2
  142. }
  143. }
  144. }
  145. }
  146. }
  147. --------------------------------------------------
  148. // CONSOLE
  149. // TEST[continued]
  150. And deleted with:
  151. [source,js]
  152. -----------------------------------
  153. DELETE _scripts/calculate-score
  154. -----------------------------------
  155. // CONSOLE
  156. // TEST[continued]
  157. [float]
  158. [[modules-scripting-using-caching]]
  159. === Script Caching
  160. All scripts are cached by default so that they only need to be recompiled
  161. when updates occur. File scripts keep a static cache and will always reside
  162. in memory. Both inline and stored scripts are stored in a cache that can evict
  163. residing scripts. By default, scripts do not have a time-based expiration, but
  164. you can change this behavior by using the `script.cache.expire` setting.
  165. You can configure the size of this cache by using the `script.cache.max_size` setting.
  166. By default, the cache size is `100`.
  167. NOTE: The size of stored scripts is limited to 65,535 bytes. This can be
  168. changed by setting `script.max_size_in_bytes` setting to increase that soft
  169. limit, but if scripts are really large then alternatives like
  170. <<modules-scripting-native,native>> scripts should be considered instead.