Skip to content

Commit 374de63

Browse files
authored
Augment painless execute API (#3589)
1 parent 8311946 commit 374de63

12 files changed

+268
-29
lines changed

output/openapi/elasticsearch-openapi.json

Lines changed: 20 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

output/openapi/elasticsearch-serverless-openapi.json

Lines changed: 20 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

output/schema/schema.json

Lines changed: 67 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

output/typescript/types.ts

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

specification/_global/scripts_painless_execute/ExecutePainlessScriptRequest.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@
1919

2020
import { RequestBase } from '@_types/Base'
2121
import { Script } from '@_types/Scripting'
22-
import { PainlessContextSetup } from './types'
22+
import { PainlessContext, PainlessContextSetup } from './types'
2323

2424
/**
2525
* Run a script.
26+
*
2627
* Runs a script and returns a result.
28+
* Use this API to build and test scripts, such as when defining a script for a runtime field.
29+
* This API requires very few dependencies and is especially useful if you don't have permissions to write documents on a cluster.
30+
*
31+
* The API uses several _contexts_, which control how scripts are run, what variables are available at runtime, and what the return type is.
32+
*
33+
* Each context requires a script, but additional parameters depend on the context you're using for that script.
2734
* @rest_spec_name scripts_painless_execute
2835
* @availability stack since=6.3.0 stability=experimental
2936
* @availability serverless stability=experimental visibility=public
3037
* @doc_tag script
38+
* @doc_id painless-execute-api
3139
*/
3240
export interface Request extends RequestBase {
3341
urls: [
@@ -39,15 +47,17 @@ export interface Request extends RequestBase {
3947
body: {
4048
/**
4149
* The context that the script should run in.
50+
* NOTE: Result ordering in the field contexts is not guaranteed.
4251
* @server_default painless_test
4352
*/
44-
context?: string
53+
context?: PainlessContext
4554
/**
4655
* Additional parameters for the `context`.
56+
* NOTE: This parameter is required for all contexts except `painless_test`, which is the default if no value is provided for `context`.
4757
*/
4858
context_setup?: PainlessContextSetup
4959
/**
50-
* The Painless script to execute.
60+
* The Painless script to run.
5161
*/
5262
script?: Script
5363
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
summary: Test context
2+
# method_request: POST /_scripts/painless/_execute
3+
description: >
4+
Run `POST /_scripts/painless/_execute`.
5+
The `painless_test` context is the default context.
6+
It runs scripts without additional parameters.
7+
The only variable that is available is `params`, which can be used to access user defined values.
8+
The result of the script is always converted to a string.
9+
# type: request
10+
value: |-
11+
{
12+
"script": {
13+
"source": "params.count / params.total",
14+
"params": {
15+
"count": 100.0,
16+
"total": 1000.0
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
summary: Filter context
2+
# method_request: POST /_scripts/painless/_execute
3+
description: >
4+
Run `POST /_scripts/painless/_execute` with a `filter` context.
5+
It treats scripts as if they were run inside a script query.
6+
For testing purposes, a document must be provided so that it will be temporarily indexed in-memory and is accessible from the script.
7+
More precisely, the `_source`, stored fields, and doc values of such a document are available to the script being tested.
8+
# type: request
9+
value: |-
10+
{
11+
"script": {
12+
"source": "doc['field'].value.length() <= params.max_length",
13+
"params": {
14+
"max_length": 4
15+
}
16+
},
17+
"context": "filter",
18+
"context_setup": {
19+
"index": "my-index-000001",
20+
"document": {
21+
"field": "four"
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
summary: Score context
2+
# method_request: POST /_scripts/painless/_execute
3+
description: >
4+
Run `POST /_scripts/painless/_execute` with a `score` context.
5+
It treats scripts as if they were run inside a `script_score` function in a `function_score` query.
6+
# type: request
7+
value: |-
8+
{
9+
"script": {
10+
"source": "doc['rank'].value / params.max_rank",
11+
"params": {
12+
"max_rank": 5.0
13+
}
14+
},
15+
"context": "score",
16+
"context_setup": {
17+
"index": "my-index-000001",
18+
"document": {
19+
"rank": 4
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)