Run a script Technical preview
Runs a script and returns a result. Use this API to build and test scripts, such as when defining a script for a runtime field. This API requires very few dependencies and is especially useful if you don't have permissions to write documents on a cluster.
The API uses several contexts, which control how scripts are run, what variables are available at runtime, and what the return type is.
Each context requires a script, but additional parameters depend on the context you're using for that script.
GET /_scripts/painless/_execute
Console
POST /_scripts/painless/_execute { "script": { "source": "params.count / params.total", "params": { "count": 100.0, "total": 1000.0 } } }
resp = client.scripts_painless_execute( script={ "source": "params.count / params.total", "params": { "count": 100, "total": 1000 } }, )
const response = await client.scriptsPainlessExecute({ script: { source: "params.count / params.total", params: { count: 100, total: 1000, }, }, });
response = client.scripts_painless_execute( body: { "script": { "source": "params.count / params.total", "params": { "count": 100, "total": 1000 } } } )
$resp = $client->scriptsPainlessExecute([ "body" => [ "script" => [ "source" => "params.count / params.total", "params" => [ "count" => 100, "total" => 1000, ], ], ], ]);
curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d '{"script":{"source":"params.count / params.total","params":{"count":100,"total":1000}}}' "$ELASTICSEARCH_URL/_scripts/painless/_execute"
Request examples
Test context
Run `POST /_scripts/painless/_execute`. The `painless_test` context is the default context. It runs scripts without additional parameters. The only variable that is available is `params`, which can be used to access user defined values. The result of the script is always converted to a string.
{ "script": { "source": "params.count / params.total", "params": { "count": 100.0, "total": 1000.0 } } }
Run `POST /_scripts/painless/_execute` with a `filter` context. It treats scripts as if they were run inside a script query. For testing purposes, a document must be provided so that it will be temporarily indexed in-memory and is accessible from the script. More precisely, the `_source`, stored fields, and doc values of such a document are available to the script being tested.
{ "script": { "source": "doc['field'].value.length() <= params.max_length", "params": { "max_length": 4 } }, "context": "filter", "context_setup": { "index": "my-index-000001", "document": { "field": "four" } } }
Run `POST /_scripts/painless/_execute` with a `score` context. It treats scripts as if they were run inside a `script_score` function in a `function_score` query.
{ "script": { "source": "doc['rank'].value / params.max_rank", "params": { "max_rank": 5.0 } }, "context": "score", "context_setup": { "index": "my-index-000001", "document": { "rank": 4 } } }
Response examples (200)
Test context
A successful response from `POST /_scripts/painless/_execute` with a `painless_test` context.
{ "result": "0.1" }
A successful response from `POST /_scripts/painless/_execute` with a `filter` context.
{ "result": true }
A successful response from `POST /_scripts/painless/_execute` with a `score` context.
{ "result": 0.8 }