create-stored-script-api.asciidoc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 _search/<script-id>`
  24. `POST _scripts/<script-id>`
  25. `PUT _search/<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. Name or identifier for the stored script or search template. Must be unique
  36. within the 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)
  67. Script or search template.
  68. `params`::
  69. (Optional, object)
  70. Parameters for the script or search template.
  71. ====
  72. [[create-stored-script-api-example]]
  73. ==== {api-examples-title}
  74. The following request stores a search template. Search templates must use a
  75. `lang` of `mustache`.
  76. [source,console]
  77. ----
  78. PUT _scripts/my-search-template
  79. {
  80. "script": {
  81. "lang": "mustache",
  82. "source": {
  83. "from": "{{from}}{{^from}}0{{/from}}",
  84. "size": "{{size}}{{^size}}10{{/size}}",
  85. "query": {
  86. "match": {
  87. "content": "{{query_string}}"
  88. }
  89. }
  90. }
  91. }
  92. }
  93. ----