|
| 1 | +[[create-stored-script-api]] |
| 2 | +=== Create or update stored script API |
| 3 | +++++ |
| 4 | +<titleabbrev>Create or update stored script</titleabbrev> |
| 5 | +++++ |
| 6 | + |
| 7 | +Creates or updates a <<script-stored-scripts,stored script>> or |
| 8 | +<<search-template,search template>>. |
| 9 | + |
| 10 | +[source,console] |
| 11 | +---- |
| 12 | +PUT _scripts/my-stored-script |
| 13 | +{ |
| 14 | + "script": { |
| 15 | + "lang": "painless", |
| 16 | + "source": """ |
| 17 | + TimestampHour date = doc['@timestamp'].value; |
| 18 | + return date.getHour() |
| 19 | + """ |
| 20 | + } |
| 21 | +} |
| 22 | +---- |
| 23 | + |
| 24 | +[[create-stored-script-api-request]] |
| 25 | +==== {api-request-title} |
| 26 | + |
| 27 | +`PUT _search/<script-id>` |
| 28 | + |
| 29 | +`POST _scripts/<script-id>` |
| 30 | + |
| 31 | +`PUT _search/<script-id>/<context>` |
| 32 | + |
| 33 | +`POST _scripts/<script-id>/<context>` |
| 34 | + |
| 35 | +[[create-stored-script-api-prereqs]] |
| 36 | +==== {api-prereq-title} |
| 37 | + |
| 38 | +* If the {es} {security-features} are enabled, you must have the `manage` |
| 39 | +<<privileges-list-cluster,cluster privilege>> to use this API. |
| 40 | + |
| 41 | +[[create-stored-script-api-path-params]] |
| 42 | +==== {api-path-parms-title} |
| 43 | + |
| 44 | +`<script-id>`:: |
| 45 | +(Required, string) |
| 46 | +Name or identifier for the stored script or search template. Must be unique |
| 47 | +within the cluster. |
| 48 | + |
| 49 | +`<context>`:: |
| 50 | +(Optional, string) |
| 51 | +Context in which the script or search template should run. To prevent errors, |
| 52 | +the API immediately compiles the script or template in this context. |
| 53 | + |
| 54 | +[[create-stored-script-api-query-params]] |
| 55 | +==== {api-query-parms-title} |
| 56 | + |
| 57 | +`context`:: |
| 58 | +(Optional, string) |
| 59 | +Context in which the script or search template should run. To prevent errors, |
| 60 | +the API immediately compiles the script or template in this context. |
| 61 | ++ |
| 62 | +If you specify both this and the `<context>` request path parameter, the API |
| 63 | +uses the request path parameter. |
| 64 | + |
| 65 | +include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms] |
| 66 | + |
| 67 | +[role="child_attributes"] |
| 68 | +[[create-stored-script-api-request-body]] |
| 69 | +==== {api-request-body-title} |
| 70 | + |
| 71 | +`script`:: |
| 72 | +(Required, object) |
| 73 | +Contains the script or search template, its parameters, and its language. |
| 74 | ++ |
| 75 | +.Properties of `script` |
| 76 | +[%collapsible%open] |
| 77 | +==== |
| 78 | +`lang`:: |
| 79 | +(Required, string) |
| 80 | +<<scripting-available-languages,Script language>>. For search templates, use |
| 81 | +`mustache`. |
| 82 | +
|
| 83 | +`source`:: |
| 84 | +(Required, string) |
| 85 | +Script or search template. |
| 86 | +
|
| 87 | +`params`:: |
| 88 | +(Optional, object) |
| 89 | +Parameters for the script or search template. |
| 90 | +==== |
| 91 | + |
| 92 | +[[create-stored-script-api-example]] |
| 93 | +==== {api-examples-title} |
| 94 | + |
| 95 | +The following request stores a search template. Search templates must use a |
| 96 | +`lang` of `mustache`. |
| 97 | + |
| 98 | +[source,console] |
| 99 | +---- |
| 100 | +PUT _scripts/my-search-template |
| 101 | +{ |
| 102 | + "script": { |
| 103 | + "lang": "mustache", |
| 104 | + "source": { |
| 105 | + "from": "{{from}}{{^from}}0{{/from}}", |
| 106 | + "size": "{{size}}{{^size}}10{{/size}}", |
| 107 | + "query": { |
| 108 | + "match": { |
| 109 | + "content": "{{query_string}}" |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | +---- |
0 commit comments