Skip to content
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

Add more contexts to painless execute api #30511

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 90 additions & 2 deletions docs/painless/painless-execute-script.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The only variable that is available is `params`, which can be used to access use
The result of the script is always converted to a string.
If no context is specified then this context is used by default.

==== Example
====== Example

Request:

Expand All @@ -52,4 +52,92 @@ Response:
"result": "0.1"
}
--------------------------------------------------
// TESTRESPONSE
// TESTRESPONSE

===== Filter script context

The `filter` context executes scripts as if they were executed inside a `script` query.
For testing purposes a document must be provided that will be indexed temporarily in-memory and
is accessible to the script being tested. Because of this the _source, stored fields and doc values
are available in the script being tested.

The following parameters are required inside a filter context:

document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
index:: The name of an index containing a mapping that is compatable with the document being indexed.

====== Example

[source,js]
----------------------------------------------------------------
POST /_scripts/painless/_execute
{
"script": {
"source": "doc['field'].value.length() <= params.max_length",
"params": {
"max_length": 4
}
"context": {
"index": "my-index",
"document": {
"field": "four"
}
}
}
}
----------------------------------------------------------------
// CONSOLE

Response:

[source,js]
--------------------------------------------------
{
"result": true
}
--------------------------------------------------
// TESTRESPONSE


===== Score script context

The `score` context executes scripts as if they were executed inside a `script_score` function in
`function_score` query.

Available parameters inside a score context:

document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
index:: The name of an index containing a mapping that is compatable with the document being indexed.
query:: If `_score` is used in the script then a query can specified that will be used the compute a score.

====== Example

[source,js]
----------------------------------------------------------------
POST /_scripts/painless/_execute
{
"script": {
"source": "doc['rank'].value / params.max_rank",
"params": {
"max_rank": 4
}
"context": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is quite confusing using "context" here, since for scripts, context is something different. It would be really good if we had a different term for this environment the script runs in...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I will rename it to execute_script_context. (after the ExecuteScriptContext class)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed it to execute_context (the script part sounded redundant to me): 539abde

"index": "my-index",
"document": {
"rank": 4
}
}
}
}
----------------------------------------------------------------
// CONSOLE

Response:

[source,js]
--------------------------------------------------
{
"result": 0.8
}
--------------------------------------------------
// TESTRESPONSE
Loading