create-stored-script-api.asciidoc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. [[create-stored-script-api]]
  2. === Create or update stored script API
  3. ++++
  4. <titleabbrev>Create or update stored script</titleabbrev>
  5. ++++
  6. Creates or updates a <<script-stored-scripts,stored script>> or
  7. <<search-template,search template>>.
  8. [source,console]
  9. ----
  10. PUT _scripts/my-stored-script
  11. {
  12. "script": {
  13. "lang": "painless",
  14. "source": """
  15. TimestampHour date = doc['@timestamp'].value;
  16. return date.getHour()
  17. """
  18. }
  19. }
  20. ----
  21. [[create-stored-script-api-request]]
  22. ==== {api-request-title}
  23. `PUT _scripts/<script-id>`
  24. `POST _scripts/<script-id>`
  25. `PUT _scripts/<script-id>/<context>`
  26. `POST _scripts/<script-id>/<context>`
  27. [[create-stored-script-api-prereqs]]
  28. ==== {api-prereq-title}
  29. * If the {es} {security-features} are enabled, you must have the `manage`
  30. <<privileges-list-cluster,cluster privilege>> to use this API.
  31. [[create-stored-script-api-path-params]]
  32. ==== {api-path-parms-title}
  33. `<script-id>`::
  34. (Required, string)
  35. Identifier for the stored script or search template. Must be unique within the
  36. cluster.
  37. `<context>`::
  38. (Optional, string)
  39. Context in which the script or search template should run. To prevent errors,
  40. the API immediately compiles the script or template in this context.
  41. [[create-stored-script-api-query-params]]
  42. ==== {api-query-parms-title}
  43. `context`::
  44. (Optional, string)
  45. Context in which the script or search template should run. To prevent errors,
  46. the API immediately compiles the script or template in this context.
  47. +
  48. If you specify both this and the `<context>` request path parameter, the API
  49. uses the request path parameter.
  50. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  51. [role="child_attributes"]
  52. [[create-stored-script-api-request-body]]
  53. ==== {api-request-body-title}
  54. `script`::
  55. (Required, object)
  56. Contains the script or search template, its parameters, and its language.
  57. +
  58. .Properties of `script`
  59. [%collapsible%open]
  60. ====
  61. `lang`::
  62. (Required, string)
  63. <<scripting-available-languages,Script language>>. For search templates, use
  64. `mustache`.
  65. `source`::
  66. (Required, string or object)
  67. For scripts, a string containing the script.
  68. +
  69. For search templates, an object containing the search template. The object
  70. supports the same parameters as the <<search-search,search API>>'s request body.
  71. Also supports https://mustache.github.io/[Mustache] variables. See
  72. <<search-template>>.
  73. `params`::
  74. (Optional, object)
  75. Parameters for the script or search template.
  76. ====