2022-09-02 18:09:13 -04:00
---
layout: default
2022-10-27 12:50:39 -04:00
title: Execute Painless stored script
2022-09-02 18:09:13 -04:00
parent: Script APIs
nav_order: 2
---
## Execute Painless stored script
Runs a stored script written in the Painless language.
OpenSearch provides several ways to run a script; the following sections show how to run a script by passing script information in the request body of a `GET <index>/_search` request.
### Request fields
2022-11-18 15:25:06 -05:00
| Field | Data type | Description |
2022-09-02 18:09:13 -04:00
:--- | :--- | :---
| query | Object | A filter that specifies documents to process. |
| script_fields | Object | Fields to include in output. |
| script | Object | ID of the script that produces a value for a field. |
2023-02-20 11:34:20 -05:00
#### Example request
2022-09-02 18:09:13 -04:00
2022-10-27 12:50:39 -04:00
The following request runs the stored script that was created in [Create or update stored script ]({{site.url}}{{site.baseurl}}/api-reference/script-apis/create-stored-script/ ). The script sums the ratings for each book and displays the sum in the `total_ratings` field in the output.
2022-09-02 18:09:13 -04:00
* The script's target is the `books` index.
* The `"match_all": {}` property value is an empty object indicating to process each document in the index.
2022-10-27 12:50:39 -04:00
* The `total_ratings` field value is the result of the `my-first-script` execution. See [Create or update stored script ]({{site.url}}{{site.baseurl}}/api-reference/script-apis/create-stored-script/ ).
2022-09-02 18:09:13 -04:00
````json
GET books/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"total_ratings": {
"script": {
"id": "my-first-script"
}
}
}
}
````
2023-01-30 17:09:38 -05:00
{% include copy-curl.html %}
2022-09-02 18:09:13 -04:00
2023-02-20 11:34:20 -05:00
#### Example response
2022-09-02 18:09:13 -04:00
The `GET books/_search` request returns the following fields:
````json
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "books",
"_id" : "1",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
12
]
}
},
{
"_index" : "books",
"_id" : "2",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
15
]
}
},
{
"_index" : "books",
"_id" : "3",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
8
]
}
}
]
}
}
````
### Response fields
2022-11-18 15:25:06 -05:00
| Field | Data type | Description |
2022-09-02 18:09:13 -04:00
:--- | :--- | :---
| took | Integer | How long the operation took in milliseconds. |
| timed_out | Boolean | Whether the operation timed out. |
| _shards | Object | Total number of shards processed and also the total number of successful, skipped, and not processed. |
| hits | Object | Contains high-level information about the documents processed and an array of `hits` objects. See [Hits object ](#hits-object ). |
#### Hits object
2022-11-18 15:25:06 -05:00
| Field | Data type | Description |
2022-09-02 18:09:13 -04:00
:--- | :--- | :---
| total | Object | Total number of documents processed and their relationship to the `match` request field. |
| max_score | Double | Highest relevance score returned from all the hits. |
| hits | Array | Information about each document that was processed. See [Document object ](#Document-object ). |
#### Document object
2022-11-18 15:25:06 -05:00
| Field | Data type | Description |
2022-09-02 18:09:13 -04:00
:--- | :--- | :---
| _index | String | Index that contains the document. |
| _id | String | Document ID. |
| _score | Float | Document's relevance score. |
| fields | Object | Fields and their value returned from the script. |