[SOLVED] Painless parameters passing from Java code

Hi,

We can't manage to pass parameters from Java to Painless runtime (Elasticsearch 6.1.2 - client and server).

The error we got is:

{"error":{"root_cause":[{"type":"script_exception","reason":"runtime error","to_string":"null","script_stack":["Debug.explain(params.get(\"enabled\"));","lang":"painless","caused_by":{"type":"painless_explain_error","reason":null}}}]},"status":500}

Here is below a code snippet of our own. Can anyone spot an error?

final Map<String, Object> enabled = new HashMap<>();
enabled.put("enabled", true);

final ScriptedMetricAggregationBuilder builder = AggregationBuilders.scriptedMetric("metricName");
builder.mapScript(new Script(ScriptType.INLINE, "painless", "Debug.explain(params.get("enabled"));", enabled));


final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(myQuery)
                                                                         .size(0)
                                                                         .timeout(TimeValue.timeValueSeconds(1))
                                                                         .aggregation(builder);

final SearchRequest searchRequest = new SearchRequest().indices("myIndex")
                                                       .types("myIndexType")
                                                       .source(searchSourceBuilder);

final SearchResponse response = restClientFactory.getRestHighLevelClient().search(searchRequest);

final ScriptedMetric agg = response.getAggregations().get("metricName");

return agg.aggregation();

Thanks,
Christophe

1 Like

Are you sure the above code is exactly what you are compiling and running? I don't think it will compile, since there are missing quote escapes in the script:

"Debug.explain(params.get("enabled"));"

Hi Ryan,

Thanks for your reply, I've tweak the snippet to remove all business logic (everything compile in our real life aggregation).

Should we access parameters from the params map object (in the "map" Painless phase) ?

Thank you,
Christophe

Here is the best example I can find in Elasticsearch source repository.

@cbismuth I finally got around to debugging this and I was able to reproduce and it is a bug. There is fortunately a workaround. Instead of specifying the params directly on the map script, specify them on the metric agg builder:

final ScriptedMetricAggregationBuilder builder = AggregationBuilders.scriptedMetric("metricName");
builder.mapScript(new Script(ScriptType.INLINE, "painless", "Debug.explain(params.get('enabled'));", Collections.emptyMap() /* note this map will be ignored*/));
builder.params(enabled);

Wow sorry for the delay and thank you very very much Ryan!

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