Create or update stored script APIedit

Creates or updates a stored script or search template.

PUT _scripts/my-stored-script
{
  "script": {
    "lang": "painless",
    "source": """
      TimestampHour date =  doc['@timestamp'].value;
      return date.getHour()
    """
  }
}

Requestedit

PUT _scripts/<script-id>

POST _scripts/<script-id>

PUT _scripts/<script-id>/<context>

POST _scripts/<script-id>/<context>

Prerequisitesedit

  • If the Elasticsearch security features are enabled, you must have the manage cluster privilege to use this API.

Path parametersedit

<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.

Query parametersedit

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.

master_timeout
(Optional, time units) Period to wait for a connection to the master node. If no response is received before the timeout expires, the request fails and returns an error. Defaults to 30s.
timeout
(Optional, time units) Period to wait for a response. If no response is received before the timeout expires, the request fails and returns an error. Defaults to 30s.

Request bodyedit

script

(Required, object) Contains the script or search template, its parameters, and its language.

Properties of script
lang
(Required, string) 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.

Examplesedit

The following request stores a search template. Search templates must use a lang of mustache.

PUT _scripts/my-search-template
{
  "script": {
    "lang": "mustache",
    "source": {
      "from": "{{from}}{{^from}}0{{/from}}",
      "size": "{{size}}{{^size}}10{{/size}}",
      "query": {
        "match": {
          "content": "{{query_string}}"
        }
      }
    }
  }
}