# Accessing Aggregation buckets to get the \`key\` value and \`\_doc\` values

**URL:** <https://discuss.elastic.co/t/accessing-aggregation-buckets-to-get-the-key-value-and-doc-values/346391>\
**Category:** Elasticsearch\
**Created:** [November 3, 2023, 5:12pm UTC](https://discuss.elastic.co/t/accessing-aggregation-buckets-to-get-the-key-value-and-doc-values/346391 "2023-11-03T17:12:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Santosh\_mandyajayara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/santosh_mandyajayara/32/127208_2.png) [@Santosh\_mandyajayara](https://discuss.elastic.co/u/Santosh_mandyajayara)\
**Post date:** [November 3, 2023, 5:12pm UTC](https://discuss.elastic.co/t/accessing-aggregation-buckets-to-get-the-key-value-and-doc-values/346391/1 "2023-11-03T17:12:50Z")

</div>

We were using the Rest High Level Client before and below was the usage to access the aggregation buckets from the SearchResponse

```auto
 	ParsedStringTerms aggregation1 = searchResponse.getAggregations().get(AGGREGATION1.name);
    for (Terms.Bucket bucket : aggregation1.getBuckets()) {
      for (Aggregation aggregation2 : bucket.getAggregations()) {
        List<? extends Terms.Bucket> buckets = ((ParsedStringTerms) aggregation2).getBuckets();
        // do something with the subaggregations
      }
    }

```

How do we go about accessing it in the new version of Java Client API ?

> **[Variant types | Elasticsearch Java API Client \[8.10\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.10/variant-types.html)**

I do see these mention to use \_custom to access these buckets ? Is there any other way to access these buckets ?

---

<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:** [December 1, 2023, 5:13pm UTC](https://discuss.elastic.co/t/accessing-aggregation-buckets-to-get-the-key-value-and-doc-values/346391/2 "2023-12-01T17:13:38Z")

</div>

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