Java API scriptedMetric param _agg

(Xavier Facq) #1

Hi all,

I have a problem, and I don't understand how to solve it, I need your help :wink:

I use the Java client API, and I have to use an AggregationBuilders.scriptedMetric with initScript, mapScript and reduceScript. I can run all very well when I use a JSON in my Head plugin, but there is something I can find in Java:

     "params": {
      "paramRightBasket": [
      "paramLeftBasket": [
      "_agg": {}

I don't understand how to initialize the _agg param ?

	Map<String, Object> scriptParams = new HashMap<String, Object>();
        scriptParams.put("paramLeftBasket", fromIds);
        scriptParams.put("paramRightBasket", targetContactId);
        scriptParams.put("_agg", >>>> HERE ??? <<<<);

Because in the initscript I must have the following code:

_agg.transactions = [];

Any advice ?


(Xavier Facq) #2

Ok here is the solution !

In the Java code

        scriptParams.put("_agg", new HashMap<String, ArrayList<Object>>());

In the initScript

        _agg.put("transactions", new ArrayList<Object>());

In the mapScript


In the reduceScript

        for(transac in  _aggs) {
          for(String key : transac.get("transactions")) {
            // do your job

and then return what ever you want unless it the same Objet as provided in Param

  HashMap<String, ArrayList<Object>> results = new HashMap<String, ArrayList<Object>>();

  ArrayList<String> values = new ArrayList<String>();
  results.put("transactions", values);

  return results;

(system) #3

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