Map Object in scripted metric aggregation


(Varun Somani) #1

I have the following Reduce Script, which is working fine when I do the query. When I am trying to call the same via Java I get am not able to call "getScriptedMetricAggregation", which is giving me Error when it is trying to convert the object to double. (Which I expect) but then how can I get the response map?

Reduce Script:

{
  "script" : {
    "lang": "painless",
    "source" : "Map result = new HashMap(); double words = 0, error = 0; for (a in params._aggs) { words += a['words'] ; error += a['error']; } result.put('words', words); result.put('error', error); return result"
  }
}

Java Code (Since it is nested on top of Term aggregation only putting the significant code):

    for (TermsAggregation.Entry entry:termsAggregation.getBuckets()) {
        log.info("key = {},{}", entry.getKey(), entry);
        ScriptedMetricAggregation scriptedResult = entry.getAggregation("scripted_error", ScriptedMetricAggregation.class);
        System.out.println(scriptedResult);
    }

Error On Screen:

Caused by: java.lang.UnsupportedOperationException: JsonObject
	at com.google.gson.JsonElement.getAsDouble(JsonElement.java:204)
	at io.searchbox.core.search.aggregation.SingleValueAggregation.<init>(SingleValueAggregation.java:19)
	at io.searchbox.core.search.aggregation.ScriptedMetricAggregation.<init>(ScriptedMetricAggregation.java:15)
	... 3 more

How to proceed ?
To me this seems like a limitation in Java-API.


(system) #2

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