# Java API: how to access sub aggregations from FilterAggregate objects

**URL:** <https://discuss.elastic.co/t/java-api-how-to-access-sub-aggregations-from-filteraggregate-objects/301936>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 8, 2022, 8:39am UTC](https://discuss.elastic.co/t/java-api-how-to-access-sub-aggregations-from-filteraggregate-objects/301936 "2022-04-08T08:39:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Harbers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tim_harbers/32/85031_2.png) [@Tim\_Harbers](https://discuss.elastic.co/u/Tim_Harbers)\
**Post date:** [April 8, 2022, 8:39am UTC](https://discuss.elastic.co/t/java-api-how-to-access-sub-aggregations-from-filteraggregate-objects/301936/1 "2022-04-08T08:39:33Z")

</div>

I am in the process of migrating from the High Level REST API to the Java API. Although it is going ok despite not having many examples in the docs, there is one problem I can't figure out.

When you send a search request that contains a filter aggregation that contains one or more sub aggregations, I don't know how to access those sub aggregations from the search response.

The search response object gives access to the top level aggregations (Aggregate objects). I can also access the FilterAggregate objects with the filter() method, but those only allow me to get the docCount() from the response. I would expect an aggregations() method that returns Map\<String,Aggregate\>, in order to access the Aggregate objects under the filter. In the HLRC this used to be possible.

Many thanks for helping me out.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 8, 2022, 3:14pm UTC](https://discuss.elastic.co/t/java-api-how-to-access-sub-aggregations-from-filteraggregate-objects/301936/2 "2022-04-08T15:14:22Z")

</div>

I also don't think I have a map, but I can put all the results of subaggs in my own map.

```auto
    Terms aggregation = response.getAggregations().get("root-aggs");
    var mapResult = new HashMap<String, Long>();
    for (var aggs : aggregation.getBuckets()) {
      Terms subAggs = aggs.getAggregations().get("key-sub-aggs");
      for (var bucket : subAggs.getBuckets()) {
        mapResult.put(bucket.getKeyAsString(), bucket.getDocCount());
      }
    }

```

---

<div class="post-metadata">

**Author:** ![Tim\_Harbers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tim_harbers/32/85031_2.png) [@Tim\_Harbers](https://discuss.elastic.co/u/Tim_Harbers)\
**Post date:** [April 8, 2022, 3:29pm UTC](https://discuss.elastic.co/t/java-api-how-to-access-sub-aggregations-from-filteraggregate-objects/301936/3 "2022-04-08T15:29:13Z")

</div>

@RabBit_BR , thanks for your reply. For StringTerm aggregations, it also works for me. Just not for Filter aggregations. Your code snippet looks like it's using the High Level Rest API (e.g. with `getAggregations()` instead of `aggregations()`), but for me the problem only happens for the Java API. Here is a code snippet, using your example:

```auto
co.elastic.clients.elasticsearch.core.SearchResponse<HashMap> searchResponse = doSearchCall();
Aggregate aggregate = searchResponse.aggregations().get("root-aggs"); //This works
if(aggregate.isFilter()) {
	FilterAggregate filterAggregate = aggregate.filter();
	Aggregate subAggregate = filterAggregate.aggregations().get("key-sub-aggs"); //This does not work.
}
```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 9, 2022, 7:32pm UTC](https://discuss.elastic.co/t/java-api-how-to-access-sub-aggregations-from-filteraggregate-objects/301936/4 "2022-04-09T19:32:34Z")

</div>

Why do you not use High Level Rest API ?

---

<div class="post-metadata">

**Author:** ![Tim\_Harbers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tim_harbers/32/85031_2.png) [@Tim\_Harbers](https://discuss.elastic.co/u/Tim_Harbers)\
**Post date:** [April 11, 2022, 8:04am UTC](https://discuss.elastic.co/t/java-api-how-to-access-sub-aggregations-from-filteraggregate-objects/301936/5 "2022-04-11T08:04:19Z")

</div>

Because the High Level REST Client is deprecated.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 11, 2022, 1:53pm UTC](https://discuss.elastic.co/t/java-api-how-to-access-sub-aggregations-from-filteraggregate-objects/301936/6 "2022-04-11T13:53:43Z")

</div>

Sorry,

Maybe this:

```auto
    var mapResult = new HashMap<String, Long>();
    for (var rootBucket : aggs.get("root-aggs").sterms().buckets().array()){
     var subAggsBucket = rootBucket.aggregations().get("key-sub-aggs").sterms().buckets().array();
      for (var subBucket : subAggsBucket) {
        mapResult.put(subBucket.key(), subBucket.docCount());
      }
    }

```

---

<div class="post-metadata">

**Author:** ![Tim\_Harbers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tim_harbers/32/85031_2.png) [@Tim\_Harbers](https://discuss.elastic.co/u/Tim_Harbers)\
**Post date:** [April 12, 2022, 7:17am UTC](https://discuss.elastic.co/t/java-api-how-to-access-sub-aggregations-from-filteraggregate-objects/301936/7 "2022-04-12T07:17:28Z")

</div>

Thanks for thinking along @RabBit_BR .

So for String Term Aggregations I can indeed access the buckets and their sub aggregations via the Java API. It's different however for Filter Aggregations.

---

<div class="post-metadata">

**Author:** ![Tim\_Harbers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tim_harbers/32/85031_2.png) [@Tim\_Harbers](https://discuss.elastic.co/u/Tim_Harbers)\
**Post date:** [April 15, 2022, 4:00pm UTC](https://discuss.elastic.co/t/java-api-how-to-access-sub-aggregations-from-filteraggregate-objects/301936/8 "2022-04-15T16:00:25Z")

</div>

I see that this is fixed now since 7.17, very nice.

---

<div class="post-metadata">

**Author:** ![seenusvuc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seenusvuc/32/105617_2.png) [@seenusvuc](https://discuss.elastic.co/u/seenusvuc)\
**Post date:** [May 12, 2022, 8:46pm UTC](https://discuss.elastic.co/t/java-api-how-to-access-sub-aggregations-from-filteraggregate-objects/301936/9 "2022-05-12T20:46:34Z")

</div>

Hi, how are you building subaggregations with filter aggregation request.

---

<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:** [June 9, 2022, 8:47pm UTC](https://discuss.elastic.co/t/java-api-how-to-access-sub-aggregations-from-filteraggregate-objects/301936/10 "2022-06-09T20:47:07Z")

</div>

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