# Nested Aggregations - Java API

**URL:** <https://discuss.elastic.co/t/nested-aggregations-java-api/17007>\
**Category:** Elasticsearch\
**Created:** [April 14, 2014, 6:21pm UTC](https://discuss.elastic.co/t/nested-aggregations-java-api/17007 "2014-04-14T18:21:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Douglas](https://avatars.discourse-cdn.com/v4/letter/r/f08c70/32.png) [@Robert\_Douglas](https://discuss.elastic.co/u/Robert_Douglas)\
**Post date:** [April 14, 2014, 6:21pm UTC](https://discuss.elastic.co/t/nested-aggregations-java-api/17007/1 "2014-04-14T18:21:21Z")

</div>

Hi all,

I'm working on putting together a query that requires nested aggregations  
in the form:

"aggs": {  
"steve\_unique": {  
"filter": {  
"term": {  
"user.screen\_name": "steve"  
}  
},  
"aggs": {  
"unique": {  
"cardinality": {  
"field": "entities.hashtags.text"  
}  
}  
}  
}  
}

Looking through the API, I'm not noticing an obvious way to go about  
forming this part of the query. Is there something built in that would  
facilitate this?

Thanks,  
Robert

--  
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/c9d2bd76-9a4b-44e7-a40a-708894c63cc0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c9d2bd76-9a4b-44e7-a40a-708894c63cc0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [April 14, 2014, 9:39pm UTC](https://discuss.elastic.co/t/nested-aggregations-java-api/17007/2 "2014-04-14T21:39:31Z")

</div>

Hi,

You should be able to do it with the Java API with something like  
(untested):

AggregationBuildersfilter("steve\_unique").filter(FilterBuilders.termFilter("user.screen\_name",  
"steve")).subAggregation(AggregationBuilders.cardinality("unique").field("entities.hashtags.text"))

On Mon, Apr 14, 2014 at 8:21 PM, Robert Douglas [robertd89@gmail.com](mailto:robertd89@gmail.com) wrote:

> Hi all,
> 
> I'm working on putting together a query that requires nested aggregations  
> in the form:
> 
> "aggs": {  
> "steve\_unique": {  
> "filter": {  
> "term": {  
> "user.screen\_name": "steve"  
> }  
> },  
> "aggs": {  
> "unique": {  
> "cardinality": {  
> "field": "entities.hashtags.text"  
> }  
> }  
> }  
> }  
> }
> 
> Looking through the API, I'm not noticing an obvious way to go about  
> forming this part of the query. Is there something built in that would  
> facilitate this?
> 
> Thanks,  
> Robert
> 
> --  
> 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/c9d2bd76-9a4b-44e7-a40a-708894c63cc0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c9d2bd76-9a4b-44e7-a40a-708894c63cc0%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/c9d2bd76-9a4b-44e7-a40a-708894c63cc0%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/c9d2bd76-9a4b-44e7-a40a-708894c63cc0%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
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/CAL6Z4j6ufTa6Mb%3Dq3EnuCVx4g6-0jzT7d64j-ODxWVnWHGG5sQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6ufTa6Mb%3Dq3EnuCVx4g6-0jzT7d64j-ODxWVnWHGG5sQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![sumit\_gupta\_sgt](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@sumit\_gupta\_sgt](https://discuss.elastic.co/u/sumit_gupta_sgt)\
**Post date:** [April 15, 2014, 5:55pm UTC](https://discuss.elastic.co/t/nested-aggregations-java-api/17007/3 "2014-04-15T17:55:55Z")

</div>

Hi,

I am searching the same thing but there is no java api for ES aggregation so you can use generate json using XContentBuilder and perform Java Search api in that json.I do it in the same way.

Thanks,  
Sumit

---

<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, 1:47am UTC](https://discuss.elastic.co/t/nested-aggregations-java-api/17007/4 "2014-05-02T01:47:29Z")

</div>

Hi,

I am trying to create nested aggregations on list of strings.

for e.g  
{aggs : {property1 : aggs : { property2 : aggs : property 3 } } }

I dont think, there are current functionalities to do so.

Regards,  
Saravanan.

---

<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:32am UTC](https://discuss.elastic.co/t/nested-aggregations-java-api/17007/5 "2017-07-06T01:32:12Z")

</div>


