-
Notifications
You must be signed in to change notification settings - Fork 504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
script api documentation #1094
script api documentation #1094
Changes from 4 commits
0a643e4
e240391
d68552c
d7f2c06
14e1aad
bd332eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
layout: default | ||
title: Create or Update Stored Script | ||
parent: Script APIs | ||
grand_parent: REST API reference | ||
nav_order: 1 | ||
--- | ||
|
||
## Create or update stored script | ||
|
||
Creates or updates a stored script or search template. | ||
|
||
For additonal information about Painless scripting, see: | ||
|
||
* [k-NN Painless Scripting extensions]({{site.url}}{{site.baseurl}}/search-plugins/knn/painless-functions/). | ||
|
||
* [k-NN]({{site.url}}{{site.baseurl}}/search-plugins/knn/inswz/). | ||
|
||
|
||
### Path parameters | ||
|
||
| Parameter | Data Type | Description | | ||
:--- | :--- | :--- | ||
| script-id | String | Stored script or search template ID. Must be unique across the cluster. Required. | | ||
|
||
### Query parameters | ||
|
||
All parameters are optional. | ||
|
||
| Parameter | Data Type | Description | | ||
:--- | :--- | :--- | ||
| context | String | Context in which the script or search template is to run. To prevent errors, the API immediately compiles the script or template in this context. | | ||
| cluster_manager_timeout | Time | Amount of time to wait for a connection to the master node. Defaults to 30 seconds. | | ||
| timeout | Time | The period of time to wait for a response. If a response is not received before the timeout value, the request fails and returns an error. Defaults to 30 seconds.| | ||
|
||
### Request fields | ||
|
||
| Field | Data Type | Description | | ||
:--- | :--- | :--- | ||
| script | Object | Defines the script or search template, its parameters, and its language. See *Script object* below. | | ||
|
||
*Script object* | ||
|
||
| Field | Data Type | Description | | ||
:--- | :--- | :--- | ||
| lang | String | Scripting language. Required. | | ||
| source | String or Object | Required. <br /> <br /> For scripts, a string with the contents of the script. <br /> <br /> For search templates, an object that defines the search template. Supports the same parameters as the [Search API]({{site.url}}{{site.baseurl}}/opensearch/rest-api/search)'s request body. request body. Search templates also support Mustache variables. | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "request body" repeated in this line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| params | Object | The script's or search template's parameters. | | ||
|
||
#### Sample request | ||
|
||
The sample uses an index called `books` with the following documents: | ||
|
||
````json | ||
{"index":{"_id":1}} | ||
{"name":"book1","author":"Faustine","ratings":[4,3,5]} | ||
{"index":{"_id":2}} | ||
{"name":"book2","author":"Amit","ratings":[5,5,5]} | ||
{"index":{"_id":3}} | ||
{"name":"book3","author":"Gilroy","ratings":[2,1,5]} | ||
```` | ||
|
||
The following request creates the Painless script `my-first-script`. It sums the ratings for each book and displays the sum in the output. | ||
|
||
````json | ||
PUT _scripts/my-first-script | ||
{ | ||
"script": { | ||
"lang": "painless", | ||
"source": """ | ||
int total = 0; | ||
for (int i = 0; i < doc['ratings'].length; ++i) { | ||
total += doc['ratings'][i]; | ||
} | ||
return total; | ||
""" | ||
} | ||
} | ||
```` | ||
The example above uses the syntax of the Dev Tools console in OpenSearch Dashboards. You can also use curl for this request as follows: | ||
{: .note } | ||
|
||
````json | ||
curl -XPUT "http://opensearch:9200/_scripts/my-first-script" -H 'Content-Type: application/json' -d' | ||
{ | ||
"script": { | ||
"lang": "painless", | ||
"source": "\n int total = 0;\n for (int i = 0; i < doc['\''ratings'\''].length; ++i) {\n total += doc['\''ratings'\''][i];\n }\n return total;\n " | ||
} | ||
}' | ||
```` | ||
|
||
See [Execute Painless stored script]({{site.url}}{{site.baseurl}}/opensearch/rest-api/_script-apis/exec-stored-script) for information about running the script. | ||
|
||
#### Sample response | ||
|
||
The `PUT _scripts/my-first-script` request returns the following fields: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a single field or plural? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. singular. corrected |
||
|
||
````json | ||
{ | ||
"acknowledged" : true | ||
} | ||
```` | ||
|
||
To determine whether the script was successfully created, use the [Get stored script]({{site.url}}{{site.baseurl}}/opensearch/rest-api/_script-apis/get-stored-script/) API, passing the script name as the `script` path parameter. | ||
{: .note} | ||
|
||
### Response fields | ||
|
||
| Field | Data Type | Description | | ||
:--- | :--- | :--- | ||
| acknowledged | Boolean | whether the request was received. | |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
layout: default | ||
title: Delete Script | ||
parent: Script APIs | ||
grand_parent: REST API reference | ||
nav_order: 4 | ||
--- | ||
|
||
## Delete script | ||
|
||
Deletes a stored script | ||
|
||
### Path parameters | ||
|
||
Path parameters are optional. | ||
|
||
| Parameter | Data Type | Description | | ||
:--- | :--- | :--- | ||
| script-id | String | ID of script to delete. | | ||
|
||
### Query parameters | ||
|
||
| Parameter | Data Type | Description | | ||
:--- | :--- | :--- | ||
| cluster_manager_timeout | Time | Amount of time to wait for a connection to the master node. Optional, defaults to `30s`. | | ||
| timeout | Time | The period of time to wait for a response. If a response is not received before the timeout value, the requ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this last sentence clipped? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
||
#### Sample request | ||
|
||
The following request deletes the `my-first-script` script: | ||
|
||
````json | ||
DELETE _scripts/my-script | ||
```` | ||
|
||
#### Sample response | ||
|
||
The `DELETE _scripts/my-first-script` request returns the following field: | ||
|
||
````json | ||
{ | ||
"acknowledged" : true | ||
} | ||
```` | ||
|
||
To determine whether the stored script was successfully deleted, use the [Get stored script]({{site.url}}{{site.baseurl}}/opensearch/rest-api/_script-apis/get-stored-script/) API, passing the script name as the `script` path parameter. | ||
|
||
### Response fields | ||
|
||
The <HTTP METHOD> <endpoint> request returns the following response fields: | ||
|
||
| Field | Data Type | Description | | ||
:--- | :--- | :--- | ||
| acknowledged | Boolean | Whether the delete script request was received. | |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
--- | ||
layout: default | ||
title: Execute Painless Stored Script | ||
parent: Script APIs | ||
grand_parent: REST API reference | ||
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 | ||
|
||
| Field | Data Type | Description | | ||
:--- | :--- | :--- | ||
| 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. | | ||
|
||
#### Sample request | ||
|
||
The following request runs the stored script that was created in [Create or Update Stored Script]({{site.url}}{{site.baseurl}}/opensearch/rest-api/_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. | ||
|
||
* 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. | ||
|
||
* The `total_ratings` field value is the result of the `my-first-script` execution. (See [Create or Update Stored Script]({{site.url}}{{site.baseurl}}/opensearch/rest-api/_script-apis/create-stored-script/).) | ||
|
||
````json | ||
GET books/_search | ||
{ | ||
"query": { | ||
"match_all": {} | ||
}, | ||
"script_fields": { | ||
"total_ratings": { | ||
"script": { | ||
"id": "my-first-script" | ||
} | ||
} | ||
} | ||
} | ||
```` | ||
|
||
#### Sample response | ||
|
||
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 | ||
|
||
| Field | Data Type | Description | | ||
:--- | :--- | :--- | ||
| 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 | ||
|
||
| Field | Data Type | Description | | ||
:--- | :--- | :--- | ||
| 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 | ||
|
||
| Field | Data Type | Description | | ||
:--- | :--- | :--- | ||
| _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. | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to decide what term to use to replace "master node" (in running text). Primary node? We use "primary" and secondary shard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, actually the new node name for previous "master" node is "cluster manager" node now.
see https://opensearch.org/docs/latest/opensearch/cluster/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah primary and secondary are best I think. I'll go ahead and update