2022-11-09 11:01:52 -05:00
---
layout: default
title: Execute Painless script
parent: Script APIs
nav_order: 7
---
2023-10-23 19:53:52 +05:00
# Execute Painless script
**Introduced 1.0**
{: .label .label-purple }
2022-11-09 11:01:52 -05:00
The Execute Painless script API allows you to run a script that is not stored.
2023-10-23 19:53:52 +05:00
## Path and HTTP methods
2022-11-09 11:01:52 -05:00
``` json
G E T / _ s c r i p t s / p a i n l e s s / _ e x e c u t e
P O S T / _ s c r i p t s / p a i n l e s s / _ e x e c u t e
```
2023-10-23 19:53:52 +05:00
## Request fields
2022-11-09 11:01:52 -05:00
| Field | Description |
:--- | :---
| script | The script to run. Required|
| context | A context for the script. Optional. Default is `painless_test` . |
| context_setup | Specifies additional parameters for the context. Optional.|
2023-02-20 11:34:20 -05:00
#### Example request
2022-11-09 11:01:52 -05:00
The following request uses the default `painless_context` for the script:
``` json
G E T / _ s c r i p t s / p a i n l e s s / _ e x e c u t e
{
"script" : {
"source" : "(params.x + params.y)/ 2" ,
"params" : {
"x" : 80 ,
"y" : 100
}
}
}
```
2023-01-30 17:09:38 -05:00
{% include copy-curl.html %}
2022-11-09 11:01:52 -05:00
2023-02-20 11:34:20 -05:00
#### Example response
2022-11-09 11:01:52 -05:00
The response contains the average of two script parameters:
``` json
{
"result" : "90"
}
```
2023-10-23 19:53:52 +05:00
## Response fields
2022-11-09 11:01:52 -05:00
| Field | Description |
:--- | :---
| result | The script result.|
## Script contexts
Choose different contexts to control the variables that are available to the script and the result's return type. The default context is `painless_test` .
2023-10-23 19:53:52 +05:00
## Painless test context
2022-11-09 11:01:52 -05:00
2023-02-20 11:34:20 -05:00
The `painless_test` context is the default script context that provides only the `params` variable to the script. The returned result is always converted to a string. See the preceding example request for a usage example.
2022-11-09 11:01:52 -05:00
2023-10-23 19:53:52 +05:00
## Filter context
2022-11-09 11:01:52 -05:00
The `filter` context runs the script as if the script were inside a script query. You must provide a test document in the context. The `_source` , stored fields, and `_doc` variables will be available to the script.
You can specify the following parameters for the filter context in the `context_setup` .
Parameter | Description
:--- | :---
document | The document that is indexed in memory temporarily and available to the script.
index | The name of the index that contains a mapping for the document.
For example, first create an index with a mapping for a test document:
``` json
P U T / t e s t i n d e x 1
{
"mappings" : {
"properties" : {
"grad" : {
"type" : "boolean"
} ,
"gpa" : {
"type" : "float"
}
}
}
}
```
2023-01-30 17:09:38 -05:00
{% include copy-curl.html %}
2022-11-09 11:01:52 -05:00
Run a script to determine if a student is eligible to graduate with honors:
``` json
P O S T / _ s c r i p t s / p a i n l e s s / _ e x e c u t e
{
"script" : {
"source" : "doc['grad'].value == true && doc['gpa'].value >= params.min_honors_gpa" ,
"params" : {
"min_honors_gpa" : 3.5
}
} ,
"context" : "filter" ,
"context_setup" : {
"index" : "testindex1" ,
"document" : {
"grad" : true ,
"gpa" : 3.79
}
}
}
```
2023-01-30 17:09:38 -05:00
{% include copy-curl.html %}
2022-11-09 11:01:52 -05:00
The response contains the result:
``` json
{
"result" : true
}
```
2023-10-23 19:53:52 +05:00
## Score context
2022-11-09 11:01:52 -05:00
The `score` context runs a script as if the script were in a `script_score` function in a `function_score` query.
You can specify the following parameters for the score context in the `context_setup` .
Parameter | Description
:--- | :---
document | The document that is indexed in memory temporarily and available to the script.
index | The name of the index that contains a mapping for the document.
query | If the script uses the `_score` parameter, the query can specify to use the `_score` field to compute the score.
For example, first create an index with a mapping for a test document:
``` json
P U T / t e s t i n d e x 1
{
"mappings" : {
"properties" : {
"gpa_4_0" : {
"type" : "float"
}
}
}
}
```
2023-01-30 17:09:38 -05:00
{% include copy-curl.html %}
2022-11-09 11:01:52 -05:00
Run a script that converts a GPA on a 4.0 scale into a different scale that is provided as a parameter:
``` json
P O S T / _ s c r i p t s / p a i n l e s s / _ e x e c u t e
{
"script" : {
"source" : "doc['gpa_4_0'].value * params.max_gpa / 4.0" ,
"params" : {
"max_gpa" : 5.0
}
} ,
"context" : "score" ,
"context_setup" : {
"index" : "testindex1" ,
"document" : {
"gpa_4_0" : 3.5
}
}
}
```
2023-01-30 17:09:38 -05:00
{% include copy-curl.html %}
2022-11-09 11:01:52 -05:00
The response contains the result:
``` json
{
"result" : 4.375
}
```