# Java API client 7.16.3 : handling for loops in terms aggregations

**URL:** <https://discuss.elastic.co/t/java-api-client-7-16-3-handling-for-loops-in-terms-aggregations/297616>\
**Category:** Elasticsearch\
**Created:** [February 18, 2022, 2:03pm UTC](https://discuss.elastic.co/t/java-api-client-7-16-3-handling-for-loops-in-terms-aggregations/297616 "2022-02-18T14:03:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scylanth](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Scylanth](https://discuss.elastic.co/u/Scylanth)\
**Post date:** [February 18, 2022, 2:03pm UTC](https://discuss.elastic.co/t/java-api-client-7-16-3-handling-for-loops-in-terms-aggregations/297616/1 "2022-02-18T14:03:11Z")

</div>

Hello.

I struggle to do aggregations on multiple terms using the new Java API, whenever using loops.

I have a map of terms I need to aggregate on with their renamed value that I need to loop on.

I have tried the following code, but I can't return the builder instead of the aggregations.

```auto
co.elastic.clients.elasticsearch.core.SearchRequest.Builder searchBuilder = co.elastic.clients.elasticsearch.core.SearchRequest.of(b1 -> b1
            .aggregations("agg", (b2, c2) -> {
                    for (Map.Entry<String, String> fieldEntry : fieldNames.entrySet()) {
                        String esField = fieldEntry.getKey();
                        String renamedValue : fieldNames.get(esField);
                        b2.terms(b3 ->
                            b3.field(esField)
                                .missingBucket(true)
                                .name(renamedValue)
                        );
                    }
                    return b2;
                }
            )
            .size(0)
        );

```

How should I change the code to solve this issue ?

I am using the version 7.16.3 of the new Java API.

Thanks

---

<div class="post-metadata">

**Author:** ![Scylanth](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Scylanth](https://discuss.elastic.co/u/Scylanth)\
**Post date:** [February 18, 2022, 5:09pm UTC](https://discuss.elastic.co/t/java-api-client-7-16-3-handling-for-loops-in-terms-aggregations/297616/2 "2022-02-18T17:09:52Z")

</div>

I actually found out my issue : I used a terms aggregation instead of a composite aggregation, which is what I intended to run at the beginning.  
Here is the updated code.

```auto
searchBuilder.aggregations("agg", b2 ->
                b2.composite( c ->
                {
                    CompositeAggregationSource.Builder builderComposite = new CompositeAggregationSource.Builder();
                    Map<String, CompositeAggregationSource> sourceMap= new HashMap<>();
                    for (Map.Entry<String, List<String>> fieldEntry : fieldNames.entrySet()) {
                        String esField = fieldEntry.getKey();
                        String renamedValue = fieldNames.getValue();
                        builderComposite.terms(t -> t
                                .field(esField)
                                .missingBucket(true)
                        );
                        sourceMap.put(renamedValue, builderComposite.build());
                    }
                    c.sources(sourceMap);
                    c.size(10000);
                    return c;
                })
            )
            .size(0);

```

---

<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 18, 2022, 5:10pm UTC](https://discuss.elastic.co/t/java-api-client-7-16-3-handling-for-loops-in-terms-aggregations/297616/3 "2022-03-18T17:10:52Z")

</div>

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