Type to search.

Store and retrieve scripts

Serverless

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

You can store and retrieve scripts from the cluster state using the stored script APIs. Stored scripts allow you to reference shared scripts for operations like scoring, aggregating, filtering, and reindexing. Instead of embedding scripts inline in each query, you can reference these shared operations.

Stored scripts can also reduce request payload size. Depending on script size and request frequency, this can help lower latency and data transfer costs.

Note

Unlike regular scripts, stored scripts require that you specify a script language using the lang parameter.

To create a script, use the create stored script API. For example, the following request creates a stored script named calculate-score.

JSON
POST _scripts/calculate-score
{
  "script": {
    "lang": "painless",
    "source": "Math.log(_score * 2) + params['my_modifier']"
  }
}

You can retrieve that script by using the get stored script API.

JSON
GET _scripts/calculate-score

To use the stored script in a query, include the script id in the script declaration:

JSON
GET my-index-000001/_search
{
  "query": {
    "script_score": {
      "query": {
        "match": {
            "message": "some message"
        }
      },
      "script": {
        "id": "calculate-score",
        "params": {
          "my_modifier": 2
        }
      }
    }
  }
}
  1. id of the stored script

To delete a stored script, submit a delete stored script API request.

JSON
DELETE _scripts/calculate-score