Java API scriptedMetric param _agg

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": [
        51,
        4206475,
        4065960
      ],
      "paramLeftBasket": [
        2588950
      ],
      "_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 ?

Thanks,
Xavier

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

        _agg.get("transactions").add(key);

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>();
  values.addAll(ids);
  results.put("transactions", values);

  return results;

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