Store and retrieve scripts
Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud. Availability This functionality is generally available and ready for production usage. Serverless interfaces and procedures might differ from classic Elastic Stack deployments. The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats. Availability 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.
Serverless
Generally available
Stack
Generally available
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.
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.
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.
GET _scripts/calculate-score
To use the stored script in a query, include the script id in the script declaration:
GET my-index-000001/_search{ "query": { "script_score": { "query": { "match": { "message": "some message" } }, "script": { "id": "calculate-score", "params": { "my_modifier": 2 } } } } }
idof the stored script
To delete a stored script, submit a delete stored script API request.
DELETE _scripts/calculate-score