BucketPath to ScriptedMetric not working?

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?


The scripted metric can return values of different type:
In your example you return a number so if you want to access the value you need to specify the full path. In your example it is Urgency-Level.value (value refers to the metric computed by your reduce script).

You've got to be kidding me :S

I've tried everything, including this, but when I tried this I probably did something else wrong (like using groovy instead of painless)...

But it finally works now, thanks you!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.