using.asciidoc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_rate`.
  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. ==== Request Examples
  93. The following are examples of using a stored script that lives at
  94. `/_scripts/{id}`.
  95. First, create the script called `calculate-score` in the cluster state:
  96. [source,js]
  97. -----------------------------------
  98. POST _scripts/calculate-score
  99. {
  100. "script": {
  101. "lang": "painless",
  102. "source": "Math.log(_score * 2) + params.my_modifier"
  103. }
  104. }
  105. -----------------------------------
  106. // CONSOLE
  107. This same script can be retrieved with:
  108. [source,js]
  109. -----------------------------------
  110. GET _scripts/calculate-score
  111. -----------------------------------
  112. // CONSOLE
  113. // TEST[continued]
  114. Stored scripts can be used by specifying the `id` parameters as follows:
  115. [source,js]
  116. --------------------------------------------------
  117. GET _search
  118. {
  119. "query": {
  120. "script": {
  121. "script": {
  122. "id": "calculate-score",
  123. "params": {
  124. "my_modifier": 2
  125. }
  126. }
  127. }
  128. }
  129. }
  130. --------------------------------------------------
  131. // CONSOLE
  132. // TEST[continued]
  133. And deleted with:
  134. [source,js]
  135. -----------------------------------
  136. DELETE _scripts/calculate-score
  137. -----------------------------------
  138. // CONSOLE
  139. // TEST[continued]
  140. [float]
  141. [[modules-scripting-using-caching]]
  142. === Script Caching
  143. All scripts are cached by default so that they only need to be recompiled
  144. when updates occur. By default, scripts do not have a time-based expiration, but
  145. you can change this behavior by using the `script.cache.expire` setting.
  146. You can configure the size of this cache by using the `script.cache.max_size` setting.
  147. By default, the cache size is `100`.
  148. NOTE: The size of stored scripts is limited to 65,535 bytes. This can be
  149. changed by setting `script.max_size_in_bytes` setting to increase that soft
  150. limit, but if scripts are really large then a
  151. <<modules-scripting-engine,native script engine>> should be considered.