mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-04-25 07:37:19 -04:00
115 lines
2.6 KiB
Text
115 lines
2.6 KiB
Text
[[create-stored-script-api]]
|
|
=== Create or update stored script API
|
|
++++
|
|
<titleabbrev>Create or update stored script</titleabbrev>
|
|
++++
|
|
|
|
Creates or updates a <<script-stored-scripts,stored script>> or
|
|
<<search-template,search template>>.
|
|
|
|
[source,console]
|
|
----
|
|
PUT _scripts/my-stored-script
|
|
{
|
|
"script": {
|
|
"lang": "painless",
|
|
"source": """
|
|
TimestampHour date = doc['@timestamp'].value;
|
|
return date.getHour()
|
|
"""
|
|
}
|
|
}
|
|
----
|
|
|
|
[[create-stored-script-api-request]]
|
|
==== {api-request-title}
|
|
|
|
`PUT _scripts/<script-id>`
|
|
|
|
`POST _scripts/<script-id>`
|
|
|
|
`PUT _scripts/<script-id>/<context>`
|
|
|
|
`POST _scripts/<script-id>/<context>`
|
|
|
|
[[create-stored-script-api-prereqs]]
|
|
==== {api-prereq-title}
|
|
|
|
* If the {es} {security-features} are enabled, you must have the `manage`
|
|
<<privileges-list-cluster,cluster privilege>> to use this API.
|
|
|
|
[[create-stored-script-api-path-params]]
|
|
==== {api-path-parms-title}
|
|
|
|
`<script-id>`::
|
|
(Required, string)
|
|
Identifier for the stored script or search template. Must be unique within the
|
|
cluster.
|
|
|
|
`<context>`::
|
|
(Optional, string)
|
|
Context in which the script or search template should run. To prevent errors,
|
|
the API immediately compiles the script or template in this context.
|
|
|
|
[[create-stored-script-api-query-params]]
|
|
==== {api-query-parms-title}
|
|
|
|
`context`::
|
|
(Optional, string)
|
|
Context in which the script or search template should run. To prevent errors,
|
|
the API immediately compiles the script or template in this context.
|
|
+
|
|
If you specify both this and the `<context>` request path parameter, the API
|
|
uses the request path parameter.
|
|
|
|
include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
|
|
|
|
[role="child_attributes"]
|
|
[[create-stored-script-api-request-body]]
|
|
==== {api-request-body-title}
|
|
|
|
`script`::
|
|
(Required, object)
|
|
Contains the script or search template, its parameters, and its language.
|
|
+
|
|
.Properties of `script`
|
|
[%collapsible%open]
|
|
====
|
|
`lang`::
|
|
(Required, string)
|
|
<<scripting-available-languages,Script language>>. For search templates, use
|
|
`mustache`.
|
|
|
|
`source`::
|
|
(Required, string or object)
|
|
Script or search template.
|
|
|
|
`params`::
|
|
(Optional, object)
|
|
Parameters for the script or search template.
|
|
====
|
|
|
|
[[create-stored-script-api-example]]
|
|
==== {api-examples-title}
|
|
|
|
The following request stores a search template. Search templates must use a
|
|
`lang` of `mustache`.
|
|
|
|
[source,console]
|
|
----
|
|
PUT _scripts/my-search-template
|
|
{
|
|
"script": {
|
|
"lang": "mustache",
|
|
"source": {
|
|
"from": "{{from}}{{^from}}0{{/from}}",
|
|
"size": "{{size}}{{^size}}10{{/size}}",
|
|
"query": {
|
|
"match": {
|
|
"content": "{{query_string}}"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
----
|