-
Notifications
You must be signed in to change notification settings - Fork 25.2k
[Docs] Add painless context details for bucket_script #35142
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
Merged
polyfractal
merged 2 commits into
elastic:master
from
polyfractal:painless_bucket_context
Nov 6, 2018
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
21 changes: 0 additions & 21 deletions
21
docs/painless/painless-contexts/painless-bucket-agg-context.asciidoc
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
docs/painless/painless-contexts/painless-bucket-script-agg-context.asciidoc
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
[[painless-bucket-script-agg-context]] | ||
=== Bucket Script aggregation context | ||
|
||
Use a Painless script in an | ||
{ref}/search-aggregations-pipeline-bucket-script-aggregation.html[`bucket_script` pipeline aggregation] | ||
to calculate a value as a result in a bucket. | ||
|
||
==== Variables | ||
|
||
`params` (`Map`, read-only):: | ||
User-defined parameters passed in as part of the query. The parameters | ||
include values defined as part of the `buckets_path`. | ||
|
||
==== Return | ||
|
||
numeric:: | ||
The calculated value as the result. | ||
|
||
==== API | ||
|
||
The standard <<painless-api-reference, Painless API>> is available. | ||
|
||
==== Example | ||
|
||
To run this example, first follow the steps in <<painless-context-examples, context examples>>. | ||
|
||
The painless context in a `bucket_script` aggregation provides a `params` map. This map contains both | ||
user-specified custom values, as well as the values from other aggregations specified in the `buckets_path` | ||
property. | ||
|
||
For our example, we are going to take the values from a `min` and `max` aggregation, subtract them to find | ||
the difference, then add in a "base cost" from a user-specified value. The script looks like this: | ||
|
||
polyfractal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[source,Painless] | ||
-------------------------------------------------- | ||
(params.max - params.min) + params.base_cost | ||
-------------------------------------------------- | ||
|
||
Note how all the values are being pulled from the `params` map. In context, the aggregation looks like this: | ||
|
||
polyfractal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[source,js] | ||
-------------------------------------------------- | ||
GET /seats/_search | ||
{ | ||
"size": 0, | ||
"aggs": { | ||
"theatres": { | ||
"terms": { | ||
"field": "theatre", | ||
"size": 10 | ||
}, | ||
"aggs": { | ||
"min_cost": { | ||
"min": { | ||
"field": "cost" | ||
} | ||
}, | ||
"max_cost": { | ||
"max": { | ||
"field": "cost" | ||
} | ||
}, | ||
"spread_plus_base": { | ||
"bucket_script": { | ||
"buckets_path": { <1> | ||
"min": "min_cost", | ||
"max": "max_cost" | ||
}, | ||
"script": { | ||
"params": { | ||
"base_cost": 5 <2> | ||
}, | ||
"source": "(params.max - params.min) + params.base_cost" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
-------------------------------------------------- | ||
// CONSOLE | ||
// TEST[setup:seats] | ||
<1> the `buckets_path` points to two aggregations (`min_cost`, `max_cost`) and adds `min`/`max` variables | ||
to the `params` map | ||
<2> the user-specified `base_cost` is added in the script's `params` section, which is also added to the | ||
`params` variable for use in the script. | ||
polyfractal marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
Uh oh!
There was an error while loading. Please reload this page.