# \[SOLVED\] Painless parameters passing from Java code

**URL:** <https://discuss.elastic.co/t/solved-painless-parameters-passing-from-java-code/117079>\
**Category:** Elasticsearch\
**Created:** [January 25, 2018, 4:34pm UTC](https://discuss.elastic.co/t/solved-painless-parameters-passing-from-java-code/117079 "2018-01-25T16:34:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbismuth](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cbismuth](https://discuss.elastic.co/u/cbismuth)\
**Post date:** [January 25, 2018, 4:34pm UTC](https://discuss.elastic.co/t/solved-painless-parameters-passing-from-java-code/117079/1 "2018-01-25T16:34:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [January 28, 2018, 4:53am UTC](https://discuss.elastic.co/t/solved-painless-parameters-passing-from-java-code/117079/2 "2018-01-28T04:53:59Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![cbismuth](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cbismuth](https://discuss.elastic.co/u/cbismuth)\
**Post date:** [January 29, 2018, 3:47pm UTC](https://discuss.elastic.co/t/solved-painless-parameters-passing-from-java-code/117079/3 "2018-01-29T15:47:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cbismuth](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cbismuth](https://discuss.elastic.co/u/cbismuth)\
**Post date:** [February 1, 2018, 10:15am UTC](https://discuss.elastic.co/t/solved-painless-parameters-passing-from-java-code/117079/4 "2018-02-01T10:15:25Z")

</div>

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

> <https://github.com/elastic/elasticsearch/blob/99f88f15c5febbca2d13b5b5fda27b844153bf1a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricIT.java#L363>

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [February 25, 2018, 9:39pm UTC](https://discuss.elastic.co/t/solved-painless-parameters-passing-from-java-code/117079/5 "2018-02-25T21:39:07Z")

</div>

@cbismuth I finally got around to debugging this and I was able to reproduce and [it is a bug](https://github.com/elastic/elasticsearch/issues/28819). There is fortunately a workaround. Instead of specifying the params directly on the map script, specify them on the metric agg builder:

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![cbismuth](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cbismuth](https://discuss.elastic.co/u/cbismuth)\
**Post date:** [March 3, 2018, 12:28pm UTC](https://discuss.elastic.co/t/solved-painless-parameters-passing-from-java-code/117079/6 "2018-03-03T12:28:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 31, 2018, 12:28pm UTC](https://discuss.elastic.co/t/solved-painless-parameters-passing-from-java-code/117079/7 "2018-03-31T12:28:52Z")

</div>

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