-
Notifications
You must be signed in to change notification settings - Fork 24.7k
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
martijnvg
merged 16 commits into
elastic:master
from
martijnvg:painless_execute_api_add_more_contexts
Jul 18, 2018
Merged
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3e78138
[Painless] Add more contexts to painless execute api
martijnvg e86764a
added tests and docs, changed the parsing code to make more use of Ob…
martijnvg d965597
changed PainlessExecuteAction.TransportAction to use TransportSingleS…
martijnvg 636ad34
Merge remote-tracking branch 'es/master' into painless_execute_api_ad…
martijnvg 539abde
rename
martijnvg 7bc755c
fixed typo
martijnvg 65bbaf8
added unit like tests and made the code easier to test
martijnvg f82c307
Merge remote-tracking branch 'es/master' into painless_execute_api_ad…
martijnvg fd3fb41
Use ScoreScript instead of SearchScript (see #30816)
martijnvg caf6f26
fixed docs
martijnvg 6da0dc7
fixed docs for real
martijnvg e3437cf
Merge remote-tracking branch 'es/master' into painless_execute_api_ad…
martijnvg 95300fe
iter
martijnvg f3eab3f
introduced context_setup parameter
martijnvg 3afa5d0
made context setup fields final
martijnvg 9b3b67e
Merge remote-tracking branch 'es/master' into painless_execute_api_ad…
martijnvg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,23 +9,23 @@ The Painless execute API allows an arbitrary script to be executed and a result | |
.Parameters | ||
[options="header"] | ||
|====== | ||
| Name | Required | Default | Description | ||
| `script` | yes | - | The script to execute | ||
| `context` | no | `painless_test` | The context the script should be executed in. | ||
| Name | Required | Default | Description | ||
| `script` | yes | - | The script to execute | ||
| `execute_context` | no | `painless_test` | The context the script should be executed in. | ||
|====== | ||
|
||
==== Contexts | ||
==== Execute contexts | ||
|
||
Contexts control how scripts are executed, what variables are available at runtime and what the return type is. | ||
Execute contexts control how scripts are executed, what variables are available at runtime and what the return type is. | ||
|
||
===== Painless test script context | ||
===== Painless test execute context | ||
|
||
The `painless_test` context executes scripts as is and do not add any special parameters. | ||
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. | ||
|
||
==== Example | ||
====== Example | ||
|
||
Request: | ||
|
||
|
@@ -52,4 +52,126 @@ Response: | |
"result": "0.1" | ||
} | ||
-------------------------------------------------- | ||
// TESTRESPONSE | ||
// TESTRESPONSE | ||
|
||
===== Filter execute context | ||
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. I would still call this section "Filter context". Ditto for the rest of this file on naming. |
||
|
||
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] | ||
---------------------------------------------------------------- | ||
PUT /my-index | ||
{ | ||
"mappings": { | ||
"_doc": { | ||
"properties": { | ||
"field": { | ||
"type": "keyword" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
POST /_scripts/painless/_execute | ||
{ | ||
"script": { | ||
"source": "doc['field'].value.length() <= params.max_length", | ||
"params": { | ||
"max_length": 4 | ||
} | ||
}, | ||
"execute_context": { | ||
"filter": { | ||
"index": "my-index", | ||
"document": { | ||
"field": "four" | ||
} | ||
} | ||
} | ||
} | ||
---------------------------------------------------------------- | ||
// CONSOLE | ||
|
||
Response: | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
{ | ||
"result": true | ||
} | ||
-------------------------------------------------- | ||
// TESTRESPONSE | ||
|
||
|
||
===== Score execute 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 to compute a score. | ||
|
||
====== Example | ||
|
||
[source,js] | ||
---------------------------------------------------------------- | ||
PUT /my-index | ||
{ | ||
"mappings": { | ||
"_doc": { | ||
"properties": { | ||
"field": { | ||
"type": "keyword" | ||
}, | ||
"rank": { | ||
"type": "long" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
POST /_scripts/painless/_execute | ||
{ | ||
"script": { | ||
"source": "doc['rank'].value / params.max_rank", | ||
"params": { | ||
"max_rank": 5.0 | ||
} | ||
}, | ||
"execute_context": { | ||
"score": { | ||
"index": "my-index", | ||
"document": { | ||
"rank": 4 | ||
} | ||
} | ||
} | ||
} | ||
---------------------------------------------------------------- | ||
// CONSOLE | ||
|
||
Response: | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
{ | ||
"result": 0.8 | ||
} | ||
-------------------------------------------------- | ||
// TESTRESPONSE |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this should still be called
Contexts
? While the parameter isexecute_context
, the concept is still just "contexts"