[SOLVED] Painless parameters passing from Java code


(Christophe Bismuth) #1

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


(Ryan Ernst) #2

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"));"

(Christophe Bismuth) #3

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


(Christophe Bismuth) #4

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


(Ryan Ernst) #5

@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);

(Christophe Bismuth) #6

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


(system) #7

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