mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-04-25 23:57:20 -04:00
Migrate scripted metric aggregation scripts to ScriptContext design (#30111)
* Migrate scripted metric aggregation scripts to ScriptContext design #29328 * Rename new script context container class and add clarifying comments to remaining references to params._agg(s) * Misc cleanup: make mock metric agg script inner classes static * Move _score to an accessor rather than an arg for scripted metric agg scripts This causes the score to be evaluated only when it's used. * Documentation changes for params._agg -> agg * Migration doc addition for scripted metric aggs _agg object change * Rename "agg" Scripted Metric Aggregation script context variable to "state" * Rename a private base class from ...Agg to ...State that I missed in my last commit * Clean up imports after merge
This commit is contained in:
parent
8b698f0bce
commit
8e4768890a
13 changed files with 619 additions and 111 deletions
|
@ -13,8 +13,8 @@ Here is an example on how to create the aggregation request:
|
|||
--------------------------------------------------
|
||||
ScriptedMetricAggregationBuilder aggregation = AggregationBuilders
|
||||
.scriptedMetric("agg")
|
||||
.initScript(new Script("params._agg.heights = []"))
|
||||
.mapScript(new Script("params._agg.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"));
|
||||
.initScript(new Script("state.heights = []"))
|
||||
.mapScript(new Script("state.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"));
|
||||
--------------------------------------------------
|
||||
|
||||
You can also specify a `combine` script which will be executed on each shard:
|
||||
|
@ -23,9 +23,9 @@ You can also specify a `combine` script which will be executed on each shard:
|
|||
--------------------------------------------------
|
||||
ScriptedMetricAggregationBuilder aggregation = AggregationBuilders
|
||||
.scriptedMetric("agg")
|
||||
.initScript(new Script("params._agg.heights = []"))
|
||||
.mapScript(new Script("params._agg.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"))
|
||||
.combineScript(new Script("double heights_sum = 0.0; for (t in params._agg.heights) { heights_sum += t } return heights_sum"));
|
||||
.initScript(new Script("state.heights = []"))
|
||||
.mapScript(new Script("state.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"))
|
||||
.combineScript(new Script("double heights_sum = 0.0; for (t in state.heights) { heights_sum += t } return heights_sum"));
|
||||
--------------------------------------------------
|
||||
|
||||
You can also specify a `reduce` script which will be executed on the node which gets the request:
|
||||
|
@ -34,10 +34,10 @@ You can also specify a `reduce` script which will be executed on the node which
|
|||
--------------------------------------------------
|
||||
ScriptedMetricAggregationBuilder aggregation = AggregationBuilders
|
||||
.scriptedMetric("agg")
|
||||
.initScript(new Script("params._agg.heights = []"))
|
||||
.mapScript(new Script("params._agg.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"))
|
||||
.combineScript(new Script("double heights_sum = 0.0; for (t in params._agg.heights) { heights_sum += t } return heights_sum"))
|
||||
.reduceScript(new Script("double heights_sum = 0.0; for (a in params._aggs) { heights_sum += a } return heights_sum"));
|
||||
.initScript(new Script("state.heights = []"))
|
||||
.mapScript(new Script("state.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"))
|
||||
.combineScript(new Script("double heights_sum = 0.0; for (t in state.heights) { heights_sum += t } return heights_sum"))
|
||||
.reduceScript(new Script("double heights_sum = 0.0; for (a in states) { heights_sum += a } return heights_sum"));
|
||||
--------------------------------------------------
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue