# Multiple sub aggregation in java api

**URL:** <https://discuss.elastic.co/t/multiple-sub-aggregation-in-java-api/23055>\
**Category:** Elasticsearch\
**Created:** [April 2, 2015, 8:29am UTC](https://discuss.elastic.co/t/multiple-sub-aggregation-in-java-api/23055 "2015-04-02T08:29:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grigory\_Rubstein](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grigory_rubstein/32/793_2.png) [@Grigory\_Rubstein](https://discuss.elastic.co/u/Grigory_Rubstein)\
**Post date:** [April 2, 2015, 8:29am UTC](https://discuss.elastic.co/t/multiple-sub-aggregation-in-java-api/23055/1 "2015-04-02T08:29:06Z")

</div>

Hi

I wondering if the java client support multiple sub aggregation on same  
level :

for example how to translate this to java api :  
by\_city and by\_language is count aggregation on same level.

{  
"aggs": {  
"by\_gender": {  
"terms": {  
"field": "gender",  
"size": 10  
},  
"aggs": {  
"by\_city": {  
"terms": {  
"field": "city",  
"size": 3  
}  
},  
"by\_language": {  
"terms": {  
"field": "gender",  
"size": 1  
}  
}  
}  
}  
}  
}

--  
This message may contain confidential and/or privileged information.  
If you are not the addressee or authorized to receive this on behalf of the  
addressee you must not use, copy, disclose or take action based on this  
message or any information herein.  
If you have received this message in error, please advise the sender  
immediately by reply email and delete this message. Thank you.

--  
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/d74224cb-5bb9-4fa7-bc79-ada08dbd8cc8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d74224cb-5bb9-4fa7-bc79-ada08dbd8cc8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Grigory\_Rubstein](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grigory_rubstein/32/793_2.png) [@Grigory\_Rubstein](https://discuss.elastic.co/u/Grigory_Rubstein)\
**Post date:** [April 2, 2015, 9:15am UTC](https://discuss.elastic.co/t/multiple-sub-aggregation-in-java-api/23055/2 "2015-04-02T09:15:52Z")

</div>

[solved]

here is the solution:  
AggregationBuilders.terms("by\_gender").field("gende").size(10).

subAggregation(AggregationBuilders.terms("by\_city").field("city")).

subAggregation(AggregationBuilders.terms("by\_language").field("language"));

On Thursday, April 2, 2015 at 11:29:07 AM UTC+3, Grigory Rubstein wrote:

> Hi
> 
> I wondering if the java client support multiple sub aggregation on same  
> level :
> 
> for example how to translate this to java api :  
> by\_city and by\_language is count aggregation on same level.
> 
> {  
> "aggs": {  
> "by\_gender": {  
> "terms": {  
> "field": "gender",  
> "size": 10  
> },  
> "aggs": {  
> "by\_city": {  
> "terms": {  
> "field": "city",  
> "size": 3  
> }  
> },  
> "by\_language": {  
> "terms": {  
> "field": "gender",  
> "size": 1  
> }  
> }  
> }  
> }  
> }  
> }
> 
> This message may contain confidential and/or privileged information.  
> If you are not the addressee or authorized to receive this on behalf of  
> the addressee you must not use, copy, disclose or take action based on this  
> message or any information herein.  
> If you have received this message in error, please advise the sender  
> immediately by reply email and delete this message. Thank you.

--  
This message may contain confidential and/or privileged information.  
If you are not the addressee or authorized to receive this on behalf of the  
addressee you must not use, copy, disclose or take action based on this  
message or any information herein.  
If you have received this message in error, please advise the sender  
immediately by reply email and delete this message. Thank you.

--  
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/d30428a3-72da-4460-85dc-a1bb205b0d9a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d30428a3-72da-4460-85dc-a1bb205b0d9a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:22am UTC](https://discuss.elastic.co/t/multiple-sub-aggregation-in-java-api/23055/3 "2017-07-06T00:22:05Z")

</div>


