Create or update stored script

Creates or updates a stored script or search template.

For additional information about Painless scripting, see:

Path parameters

ParameterData typeDescription
script-idStringStored script or search template ID. Must be unique across the cluster. Required.

Query parameters

All parameters are optional.

ParameterData typeDescription
contextStringContext in which the script or search template is to run. To prevent errors, the API immediately compiles the script or template in this context.
cluster_manager_timeoutTimeAmount of time to wait for a connection to the cluster manager. Defaults to 30 seconds.
timeoutTimeThe period of time to wait for a response. If a response is not received before the timeout value, the request fails and returns an error. Defaults to 30 seconds.

Request fields

FieldData typeDescription
scriptObjectDefines the script or search template, its parameters, and its language. See Script object below.

Script object

FieldData typeDescription
langStringScripting language. Required.
sourceString or ObjectRequired.

For scripts, a string with the contents of the script.

For search templates, an object that defines the search template. Supports the same parameters as the Search API request body. Search templates also support Mustache variables.

Example request

The sample uses an index called books with the following documents:

  1. {"index":{"_id":1}}
  2. {"name":"book1","author":"Faustine","ratings":[4,3,5]}
  3. {"index":{"_id":2}}
  4. {"name":"book2","author":"Amit","ratings":[5,5,5]}
  5. {"index":{"_id":3}}
  6. {"name":"book3","author":"Gilroy","ratings":[2,1,5]}

The following request creates the Painless script my-first-script. It sums the ratings for each book and displays the sum in the output.

  1. PUT _scripts/my-first-script
  2. {
  3. "script": {
  4. "lang": "painless",
  5. "source": """
  6. int total = 0;
  7. for (int i = 0; i < doc['ratings'].length; ++i) {
  8. total += doc['ratings'][i];
  9. }
  10. return total;
  11. """
  12. }
  13. }

copy

The example above uses the syntax of the Dev Tools console in OpenSearch Dashboards. You can also use a curl request.

The following curl request is equivalent to the previous Dashboards console example:

  1. curl -XPUT "http://opensearch:9200/_scripts/my-first-script" -H 'Content-Type: application/json' -d'
  2. {
  3. "script": {
  4. "lang": "painless",
  5. "source": "\n int total = 0;\n for (int i = 0; i < doc['\''ratings'\''].length; ++i) {\n total += doc['\''ratings'\''][i];\n }\n return total;\n "
  6. }
  7. }'

copy

See Execute Painless stored script for information about running the script.

Example response

The PUT _scripts/my-first-script request returns the following field:

  1. {
  2. "acknowledged" : true
  3. }

To determine whether the script was successfully created, use the Get stored script API, passing the script name as the script path parameter.

Response fields

FieldData typeDescription
acknowledgedBooleanwhether the request was received.