2018-04-19 03:33:34 -04:00
|
|
|
[[painless-execute-api]]
|
|
|
|
=== Painless execute API
|
|
|
|
|
2018-05-21 03:49:25 -04:00
|
|
|
experimental[The painless execute api is new and the request / response format may change in a breaking way in the future]
|
|
|
|
|
2018-04-19 03:33:34 -04:00
|
|
|
The Painless execute API allows an arbitrary script to be executed and a result to be returned.
|
|
|
|
|
|
|
|
[[painless-execute-api-parameters]]
|
|
|
|
.Parameters
|
|
|
|
[options="header"]
|
|
|
|
|======
|
2018-07-18 06:42:07 -04:00
|
|
|
| Name | Required | Default | Description
|
2019-10-18 10:30:34 -04:00
|
|
|
| `script` | yes | - | The script to execute.
|
2018-07-18 06:42:07 -04:00
|
|
|
| `context` | no | `painless_test` | The context the script should be executed in.
|
|
|
|
| `context_setup` | no | - | Additional parameters to the context.
|
2018-04-19 03:33:34 -04:00
|
|
|
|======
|
|
|
|
|
|
|
|
==== Contexts
|
|
|
|
|
|
|
|
Contexts control how scripts are executed, what variables are available at runtime and what the return type is.
|
|
|
|
|
2018-07-18 06:42:07 -04:00
|
|
|
===== Painless test context
|
2018-04-19 03:33:34 -04:00
|
|
|
|
2019-10-18 10:30:34 -04:00
|
|
|
The `painless_test` context executes scripts as is and does not add any special parameters.
|
2018-04-19 03:33:34 -04:00
|
|
|
The only variable that is available is `params`, which can be used to access user defined values.
|
|
|
|
The result of the script is always converted to a string.
|
|
|
|
If no context is specified then this context is used by default.
|
|
|
|
|
2018-08-21 16:13:07 -04:00
|
|
|
*Example*
|
2018-04-19 03:33:34 -04:00
|
|
|
|
|
|
|
Request:
|
|
|
|
|
2019-09-04 15:19:20 -04:00
|
|
|
[source,console]
|
2018-04-19 03:33:34 -04:00
|
|
|
----------------------------------------------------------------
|
|
|
|
POST /_scripts/painless/_execute
|
|
|
|
{
|
|
|
|
"script": {
|
|
|
|
"source": "params.count / params.total",
|
|
|
|
"params": {
|
|
|
|
"count": 100.0,
|
|
|
|
"total": 1000.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
Response:
|
|
|
|
|
2019-09-04 15:19:20 -04:00
|
|
|
[source,console-result]
|
2018-04-19 03:33:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"result": "0.1"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2018-07-18 06:42:07 -04:00
|
|
|
|
|
|
|
===== Filter context
|
|
|
|
|
|
|
|
The `filter` context executes scripts as if they were executed inside a `script` query.
|
2019-10-18 10:30:34 -04:00
|
|
|
For testing purposes, a document must be provided so that it will be temporarily indexed in-memory and
|
|
|
|
is accessible from the script. More precisely, the _source, stored fields and doc values of such a
|
|
|
|
document are available to the script being tested.
|
2018-07-18 06:42:07 -04:00
|
|
|
|
|
|
|
The following parameters may be specified in `context_setup` for a filter context:
|
|
|
|
|
|
|
|
document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
|
2019-01-07 08:44:12 -05:00
|
|
|
index:: The name of an index containing a mapping that is compatible with the document being indexed.
|
2018-07-18 06:42:07 -04:00
|
|
|
|
2018-08-21 16:13:07 -04:00
|
|
|
*Example*
|
2018-07-18 06:42:07 -04:00
|
|
|
|
2019-09-04 15:19:20 -04:00
|
|
|
[source,console]
|
2018-07-18 06:42:07 -04:00
|
|
|
----------------------------------------------------------------
|
2019-01-18 08:11:18 -05:00
|
|
|
PUT /my-index
|
2018-07-18 06:42:07 -04:00
|
|
|
{
|
|
|
|
"mappings": {
|
2019-01-18 08:11:18 -05:00
|
|
|
"properties": {
|
|
|
|
"field": {
|
|
|
|
"type": "keyword"
|
2018-07-18 06:42:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
POST /_scripts/painless/_execute
|
|
|
|
{
|
|
|
|
"script": {
|
|
|
|
"source": "doc['field'].value.length() <= params.max_length",
|
|
|
|
"params": {
|
|
|
|
"max_length": 4
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"context": "filter",
|
|
|
|
"context_setup": {
|
|
|
|
"index": "my-index",
|
|
|
|
"document": {
|
|
|
|
"field": "four"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
Response:
|
|
|
|
|
2019-09-04 15:19:20 -04:00
|
|
|
[source,console-result]
|
2018-07-18 06:42:07 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"result": true
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
===== Score context
|
|
|
|
|
|
|
|
The `score` context executes scripts as if they were executed inside a `script_score` function in
|
|
|
|
`function_score` query.
|
|
|
|
|
|
|
|
The following parameters may be specified in `context_setup` for a score context:
|
|
|
|
|
|
|
|
document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
|
2019-01-07 08:44:12 -05:00
|
|
|
index:: The name of an index containing a mapping that is compatible with the document being indexed.
|
2019-10-18 10:30:34 -04:00
|
|
|
query:: If `_score` is used in the script then a query can specify that it will be used to compute a score.
|
2018-07-18 06:42:07 -04:00
|
|
|
|
2018-08-21 16:13:07 -04:00
|
|
|
*Example*
|
2018-07-18 06:42:07 -04:00
|
|
|
|
2019-09-04 15:19:20 -04:00
|
|
|
[source,console]
|
2018-07-18 06:42:07 -04:00
|
|
|
----------------------------------------------------------------
|
2019-01-18 08:11:18 -05:00
|
|
|
PUT /my-index
|
2018-07-18 06:42:07 -04:00
|
|
|
{
|
|
|
|
"mappings": {
|
2019-01-18 08:11:18 -05:00
|
|
|
"properties": {
|
|
|
|
"field": {
|
|
|
|
"type": "keyword"
|
|
|
|
},
|
|
|
|
"rank": {
|
|
|
|
"type": "long"
|
2018-07-18 06:42:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
POST /_scripts/painless/_execute
|
|
|
|
{
|
|
|
|
"script": {
|
|
|
|
"source": "doc['rank'].value / params.max_rank",
|
|
|
|
"params": {
|
|
|
|
"max_rank": 5.0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"context": "score",
|
|
|
|
"context_setup": {
|
|
|
|
"index": "my-index",
|
|
|
|
"document": {
|
|
|
|
"rank": 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
Response:
|
|
|
|
|
2019-09-04 15:19:20 -04:00
|
|
|
[source,console-result]
|
2018-07-18 06:42:07 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"result": 0.8
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|