I'm trying to get a ScriptedMetric in a BucketScript, that should be possible right?
This is my query (in NEST)
.ScriptedMetric("Urgency-Level", sm => sm
.InitScript(i => i.Inline("params._agg.data = []").Lang("painless"))
.MapScript(i => i.Inline("params._agg.data.add(doc.urgency.value)").Lang("painless"))
.CombineScript(i => i.Inline("int urgency = 0; for (u in params._agg.data) { urgency += u } return urgency").Lang("painless"))
.ReduceScript(i =>i.Inline("int urgency = 0; for (a in params._aggs) { urgency += a } return urgency").Lang("painless"))
)
.BucketScript("finalScore", scb => scb
.BucketsPath(bp => bp
.Add("avgScore", "Avg-Score")
.Add("urgencyLevel", "Urgency-Level")
.Add("maxDateTime", "Max-DateTime"))
.Script(i => i.Inline("params.avgScore").Lang("painless"))
)
BTW, I do realize that the ScriptedMetric does exactly do what the Sum aggregation does, but that's going to change of course... The result of this metric is a nice single value 11...
But I'm getting the error: "aggregation_execution_exception Reason: "buckets_path must reference either a number value or a single value numeric metric aggregation, got: org.elasticsearch.search.aggregations.metrics.scripted.InternalScriptedMetric"
As if the ScriptedMetric hasn't ran yet?