# Converting 7x. into 8.x Java client code. Getting the counts of individual buckets

**URL:** <https://discuss.elastic.co/t/converting-7x-into-8-x-java-client-code-getting-the-counts-of-individual-buckets/373223>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [January 15, 2025, 9:48am UTC](https://discuss.elastic.co/t/converting-7x-into-8-x-java-client-code-getting-the-counts-of-individual-buckets/373223 "2025-01-15T09:48:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hans\_Kruse](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hans_kruse/32/54739_2.png) [@Hans\_Kruse](https://discuss.elastic.co/u/Hans_Kruse)\
**Post date:** [January 15, 2025, 9:48am UTC](https://discuss.elastic.co/t/converting-7x-into-8-x-java-client-code-getting-the-counts-of-individual-buckets/373223/1 "2025-01-15T09:48:35Z")

</div>

In our journey of converting 7.x client code into 8.x code we ran into one more challenge.

Observe the following code that converts aggregation data retrieved from the client into a list of our `Agg` dto objects.

```Java
public static @NotNull List<Agg> toAggList(final @NotNull Map<String, Aggregate> aggregations) {
        if (aggregations.isEmpty()) {
            return Collections.emptyList();
        }
        final var results = new ArrayList<Agg>();
        for (final var aggItem : aggregations.entrySet()) {
            final var aggregationName = aggItem.getKey();
            final var aggBuilder = Agg.builder()
                    .name(aggregationName);
            final var aggregation = aggregations.get(aggregationName);
            // REMARK: map to aggBuckets to get the count of each aggregation bucket.
            // The original 7.x code iterated over the buckets of a terms aggregation here.

            final var aggBuckets= new ArrayList<AggBucket>();
            aggBuilder.detail(aggBuckets);
            results.add(aggBuilder.build());
        }
        return results;
    }

```

The Orginal 7.X code had the following:

```Java
...
for (final Aggregation aggItem : aggregations.asList()) {
            final var aggregationName = aggItem.getName();
            final var aggObject = Agg.builder()
                    .aggName(aggregationName)
                    .build();
            final Terms aggregation = aggregations.get(aggregationName);
            final var details = new ArrayList<AggBucket>();
            for (final var entry : aggregation.getBuckets()) {
                AggBucket aggBucket = new AggBucket();
                aggBucket.setValue((String) entry.getKey()); // Term
                aggBucket.setCount((int) entry.getDocCount()); // Doc count
                details.add(aggBucket);
            }
...

```

The problem is how to get to the Terms aggregation to get to the list of buckets to get the count.

In the 8.x code I can check `aggregation.isMultiTerms()` and then do `aggregation.multiTerms()` and try to continue from there or do similar with dozens of other flavours terms aggregations.

What is the right approach?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 15, 2025, 10:07am UTC](https://discuss.elastic.co/t/converting-7x-into-8-x-java-client-code-getting-the-counts-of-individual-buckets/373223/2 "2025-01-15T10:07:56Z")

</div>

Hey @Hans_Kruse

Does this help?

> <https://github.com/dadoonet/elasticsearch-java-client-demo/blob/235f9dd59c2c133744b6b5ef496b4ef67e9390f9/src/test/java/fr/pilato/test/elasticsearch/hlclient/EsClientIT.java#L370-L388>

---

<div class="post-metadata">

**Author:** ![Hans\_Kruse](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hans_kruse/32/54739_2.png) [@Hans\_Kruse](https://discuss.elastic.co/u/Hans_Kruse)\
**Post date:** [January 16, 2025, 8:52am UTC](https://discuss.elastic.co/t/converting-7x-into-8-x-java-client-code-getting-the-counts-of-individual-buckets/373223/3 "2025-01-16T08:52:24Z")

</div>

Bonjour,

Oui. So simple, `sterms` option as in string terms.

That solves it. Merci beaucoup.

```Java
  public static @NotNull List<Agg> toAggList(final @NotNull Map<String, Aggregate> aggregations) {
        if (aggregations.isEmpty()) {
            return Collections.emptyList();
        }
        return aggregations.entrySet().stream()
                // call me paranoid for filtering a bit much on null or empty
                .filter(Objects::nonNull)
                .filter(item -> item.getValue() != null)
                .filter(item -> item.getValue().sterms() != null)
                .filter(item -> item.getValue().sterms().buckets() != null)
                .filter(item -> item.getValue().sterms().buckets().isArray())
                .filter(item -> !item.getValue().sterms().buckets().array().isEmpty())
                .map(item -> Agg.builder()
                        .name(item.getKey())
                        .detail(item
                                .getValue()
                                .sterms()
                                .buckets()
                                .array()
                                .stream()
                                .filter(Objects::nonNull)
                                .map(bucket -> AggBucket.builder()
                                        .value(bucket.key().stringValue())
                                        .count(bucket.docCount())
                                        .build())
                                .toList())
                        .build()).toList();
    }

```
