Run a script
Technical preview; Added in 6.3.0
All methods and paths for this operation:
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.
Body
-
The context that the script should run in. NOTE: Result ordering in the field contexts is not guaranteed.
Supported values include:
painless_test
: The default context if no other context is specified.filter
: Treats scripts as if they were run inside a script query.score
: Treats scripts as if they were run inside ascript_score
function in afunction_score
query.boolean_field
: The context for boolean fields. The script returns atrue
orfalse
response.date_field
: The context for date fields.emit
takes a long value and the script returns a sorted list of dates.double_field
: The context for double numeric fields. The script returns a sorted list of double values.geo_point_field
: The context for geo-point fields.emit
takes two double parameters, the latitude and longitude values, and the script returns an object in GeoJSON format containing the coordinates for the geo point.ip_field
: The context forip
fields. The script returns a sorted list of IP addresses.keyword_field
: The context for keyword fields. The script returns a sorted list of string values.long_field
: The context for long numeric fields. The script returns a sorted list of long values.composite_field
: The context for composite runtime fields. The script returns a map of values.
Values are
painless_test
,filter
,score
,boolean_field
,date_field
,double_field
,geo_point_field
,ip_field
,keyword_field
,long_field
, orcomposite_field
. -
Additional parameters for the
context
. NOTE: This parameter is required for all contexts exceptpainless_test
, which is the default if no value is provided forcontext
. -
The Painless script to run.
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"
client.scriptsPainlessExecute(s -> s
.script(sc -> sc
.source(so -> so
.scriptString("params.count / params.total")
)
.params(Map.of("total", JsonData.fromJson("1000"),"count", JsonData.fromJson("100")))
)
);
{
"script": {
"source": "params.count / params.total",
"params": {
"count": 100.0,
"total": 1000.0
}
}
}
{
"script": {
"source": "doc['field'].value.length() <= params.max_length",
"params": {
"max_length": 4
}
},
"context": "filter",
"context_setup": {
"index": "my-index-000001",
"document": {
"field": "four"
}
}
}
{
"script": {
"source": "doc['rank'].value / params.max_rank",
"params": {
"max_rank": 5.0
}
},
"context": "score",
"context_setup": {
"index": "my-index-000001",
"document": {
"rank": 4
}
}
}
{
"result": "0.1"
}
{
"result": true
}
{
"result": 0.8
}