# Java API for multiple sub-aggregations

**URL:** <https://discuss.elastic.co/t/java-api-for-multiple-sub-aggregations/17323>\
**Category:** Elasticsearch\
**Created:** [May 2, 2014, 7:31pm UTC](https://discuss.elastic.co/t/java-api-for-multiple-sub-aggregations/17323 "2014-05-02T19:31:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saravanan\_Shanmugasu](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@Saravanan\_Shanmugasu](https://discuss.elastic.co/u/Saravanan_Shanmugasu)\
**Post date:** [May 2, 2014, 7:31pm UTC](https://discuss.elastic.co/t/java-api-for-multiple-sub-aggregations/17323/1 "2014-05-02T19:31:08Z")

</div>

Hi,

I am trying to find out the JAVA API that supports nested sub-aggregations in ES. However, the current API's doesn't provide the clear implementation.

Req: property1 , property 2 .... property n

Using TermBuilder, when i tried adding the subAggregation of subAggregation, its getting added into the TermsBuilder as list of sub-aggregrations rather than nested sub-aggregations.

Thanks,  
Saravanan.

---

<div class="post-metadata">

**Author:** ![Ha\_Lan\_Quyen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ha_lan_quyen/32/1585_2.png) [@Ha\_Lan\_Quyen](https://discuss.elastic.co/u/Ha_Lan_Quyen)\
**Post date:** [May 15, 2014, 1:40am UTC](https://discuss.elastic.co/t/java-api-for-multiple-sub-aggregations/17323/2 "2014-05-15T01:40:45Z")

</div>

I'm having the same problem and using sub-aggregation of sub-aggregation  
but it's difficult to parse response because will have to use many loop and  
I think it's not good way

On Saturday, May 3, 2014 2:31:08 AM UTC+7, Saravanan S wrote:

> Hi,
> 
> I am trying to find out the JAVA API that supports nested sub-aggregations  
> in ES. However, the current API's doesn't provide the clear  
> implementation.
> 
> Req: property1 , property 2 .... property n
> 
> Using TermBuilder, when i tried adding the subAggregation of  
> subAggregation,  
> its getting added into the TermsBuilder as list of sub-aggregrations  
> rather  
> than nested sub-aggregations.
> 
> Thanks,  
> Saravanan.
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Java-API-for-multiple-sub-aggregations-tp4055253.html](http://elasticsearch-users.115913.n3.nabble.com/Java-API-for-multiple-sub-aggregations-tp4055253.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/d1f96bb8-1020-4c02-ab4f-e24bf033a4a4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d1f96bb8-1020-4c02-ab4f-e24bf033a4a4%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bappr](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@bappr](https://discuss.elastic.co/u/bappr)\
**Post date:** [July 25, 2014, 9:03am UTC](https://discuss.elastic.co/t/java-api-for-multiple-sub-aggregations/17323/3 "2014-07-25T09:03:40Z")

</div>

Hi,

A possible solution is :

TermsBuilder termsBuilder = null;  
TermsBuilder root = null;  
if (!aggregs.isEmpty()) {  
for (Aggreg aggreg : aggregs) {  
if (aggreg.aggregOk()) {  
if (aggreg.isTerms()) {  
if (termsBuilder != null) {  
TermsBuilder temp = terms(aggreg.getKey()).field(aggreg.getField()).minDocCount(0).size(0);  
termsBuilder = termsBuilder.subAggregation(temp);  
termsBuilder = temp;  
} else {  
termsBuilder = terms(aggreg.getKey()).field(aggreg.getField()).minDocCount(0).size(0);  
root = termsBuilder;  
}  
}  
}  
}  
}  
termsBuilder = root;

Ben

---

<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:** [July 6, 2017, 1:13am UTC](https://discuss.elastic.co/t/java-api-for-multiple-sub-aggregations/17323/4 "2017-07-06T01:13:09Z")

</div>


