Skip to content

Commit 54322a4

Browse files
committed
Add more contexts to painless execute api (#30511)
This change adds two contexts the execute scripts against: * SEARCH_SCRIPT: Allows to run scripts in a search script context. This context is used in `function_score` query's script function, script fields, script sorting and `terms_set` query. * FILTER_SCRIPT: Allows to run scripts in a filter script context. This context is used in the `script` query. In both contexts a index name needs to be specified and a sample document. The document is needed to create an in-memory index that the script can access via the `doc[...]` and other notations. The index name is needed because a mapping is needed to index the document. Examples: ``` POST /_scripts/painless/_execute { "script": { "source": "doc['field'].value.length()" }, "context" : { "search_script": { "document": { "field": "four" }, "index": "my-index" } } } ``` Returns: ``` { "result": 4 } ``` POST /_scripts/painless/_execute { "script": { "source": "doc['field'].value.length() <= params.max_length", "params": { "max_length": 4 } }, "context" : { "filter_script": { "document": { "field": "four" }, "index": "my-index" } } } Returns: ``` { "result": true } ``` Also changed PainlessExecuteAction.TransportAction to use TransportSingleShardAction instead of HandledAction, because now in case score or filter contexts are used the request needs to be redirected to a node that has an active IndexService for the index being referenced (a node with a shard copy for that index).
1 parent cfae31d commit 54322a4

File tree

5 files changed

+691
-76
lines changed

5 files changed

+691
-76
lines changed

docs/painless/painless-execute-script.asciidoc

Lines changed: 127 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@ The Painless execute API allows an arbitrary script to be executed and a result
99
.Parameters
1010
[options="header"]
1111
|======
12-
| Name | Required | Default | Description
13-
| `script` | yes | - | The script to execute
14-
| `context` | no | `painless_test` | The context the script should be executed in.
12+
| Name | Required | Default | Description
13+
| `script` | yes | - | The script to execute
14+
| `context` | no | `painless_test` | The context the script should be executed in.
15+
| `context_setup` | no | - | Additional parameters to the context.
1516
|======
1617

1718
==== Contexts
1819

1920
Contexts control how scripts are executed, what variables are available at runtime and what the return type is.
2021

21-
===== Painless test script context
22+
===== Painless test context
2223

2324
The `painless_test` context executes scripts as is and do not add any special parameters.
2425
The only variable that is available is `params`, which can be used to access user defined values.
2526
The result of the script is always converted to a string.
2627
If no context is specified then this context is used by default.
2728

28-
==== Example
29+
====== Example
2930

3031
Request:
3132

@@ -52,4 +53,124 @@ Response:
5253
"result": "0.1"
5354
}
5455
--------------------------------------------------
55-
// TESTRESPONSE
56+
// TESTRESPONSE
57+
58+
===== Filter context
59+
60+
The `filter` context executes scripts as if they were executed inside a `script` query.
61+
For testing purposes a document must be provided that will be indexed temporarily in-memory and
62+
is accessible to the script being tested. Because of this the _source, stored fields and doc values
63+
are available in the script being tested.
64+
65+
The following parameters may be specified in `context_setup` for a filter context:
66+
67+
document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
68+
index:: The name of an index containing a mapping that is compatable with the document being indexed.
69+
70+
====== Example
71+
72+
[source,js]
73+
----------------------------------------------------------------
74+
PUT /my-index
75+
{
76+
"mappings": {
77+
"_doc": {
78+
"properties": {
79+
"field": {
80+
"type": "keyword"
81+
}
82+
}
83+
}
84+
}
85+
}
86+
87+
POST /_scripts/painless/_execute
88+
{
89+
"script": {
90+
"source": "doc['field'].value.length() <= params.max_length",
91+
"params": {
92+
"max_length": 4
93+
}
94+
},
95+
"context": "filter",
96+
"context_setup": {
97+
"index": "my-index",
98+
"document": {
99+
"field": "four"
100+
}
101+
}
102+
}
103+
----------------------------------------------------------------
104+
// CONSOLE
105+
106+
Response:
107+
108+
[source,js]
109+
--------------------------------------------------
110+
{
111+
"result": true
112+
}
113+
--------------------------------------------------
114+
// TESTRESPONSE
115+
116+
117+
===== Score context
118+
119+
The `score` context executes scripts as if they were executed inside a `script_score` function in
120+
`function_score` query.
121+
122+
The following parameters may be specified in `context_setup` for a score context:
123+
124+
document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
125+
index:: The name of an index containing a mapping that is compatable with the document being indexed.
126+
query:: If `_score` is used in the script then a query can specified that will be used to compute a score.
127+
128+
====== Example
129+
130+
[source,js]
131+
----------------------------------------------------------------
132+
PUT /my-index
133+
{
134+
"mappings": {
135+
"_doc": {
136+
"properties": {
137+
"field": {
138+
"type": "keyword"
139+
},
140+
"rank": {
141+
"type": "long"
142+
}
143+
}
144+
}
145+
}
146+
}
147+
148+
149+
POST /_scripts/painless/_execute
150+
{
151+
"script": {
152+
"source": "doc['rank'].value / params.max_rank",
153+
"params": {
154+
"max_rank": 5.0
155+
}
156+
},
157+
"context": "score",
158+
"context_setup": {
159+
"index": "my-index",
160+
"document": {
161+
"rank": 4
162+
}
163+
}
164+
}
165+
----------------------------------------------------------------
166+
// CONSOLE
167+
168+
Response:
169+
170+
[source,js]
171+
--------------------------------------------------
172+
{
173+
"result": 0.8
174+
}
175+
--------------------------------------------------
176+
// TESTRESPONSE

0 commit comments

Comments
 (0)