# Java API scriptedMetric param \_agg

**URL:** <https://discuss.elastic.co/t/java-api-scriptedmetric-param--agg/73709>\
**Category:** Elasticsearch\
**Created:** [February 2, 2017, 3:30pm UTC](https://discuss.elastic.co/t/java-api-scriptedmetric-param--agg/73709 "2017-02-02T15:30:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [February 2, 2017, 3:30pm UTC](https://discuss.elastic.co/t/java-api-scriptedmetric-param--agg/73709/1 "2017-02-02T15:30:44Z")

</div>

Hi all,

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

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

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [February 2, 2017, 4:25pm UTC](https://discuss.elastic.co/t/java-api-scriptedmetric-param--agg/73709/2 "2017-02-02T16:25:48Z")

</div>

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

---

<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 2, 2017, 4:25pm UTC](https://discuss.elastic.co/t/java-api-scriptedmetric-param--agg/73709/3 "2017-03-02T16:25:54Z")

</div>

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