# Query Help: Grouping and then counting resulting buckets

**URL:** <https://discuss.elastic.co/t/query-help-grouping-and-then-counting-resulting-buckets/21543>\
**Category:** Elasticsearch\
**Created:** [January 8, 2015, 4:11pm UTC](https://discuss.elastic.co/t/query-help-grouping-and-then-counting-resulting-buckets/21543 "2015-01-08T16:11:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Stott](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_stott/32/1015_2.png) [@Nathan\_Stott](https://discuss.elastic.co/u/Nathan_Stott)\
**Post date:** [January 8, 2015, 4:11pm UTC](https://discuss.elastic.co/t/query-help-grouping-and-then-counting-resulting-buckets/21543/1 "2015-01-08T16:11:53Z")

</div>

I'm having an issue getting a query to provide the count that I want. It  
looks like something that may only be supported by "reducers" which are a  
coming feature; however, any insight would be appreciated even if it is  
just confirmation that this is not yet possible.

What I want to do is take a set of person documents, filter them, group the  
remaining persons by address, sort them and do a top hits to narrow it down  
to one person per household, and then count the number of "person types"  
that are left. Everything except the last part, which would normally be a  
terms aggregation on the person type attribute, is straight-forward. Can  
not do a subaggregation of top\_hits, so it does not seem possible currently  
to accomplish this.

The following is a gist describing the query that we're trying to do:

[https://gist.github.com/tcbeutler/1327e0a623bcdc52e89e](https://gist.github.com/tcbeutler/1327e0a623bcdc52e89e)

--  
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/55ef9fed-6653-4fd3-a0f0-ba5d25d694cd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/55ef9fed-6653-4fd3-a0f0-ba5d25d694cd%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:39am UTC](https://discuss.elastic.co/t/query-help-grouping-and-then-counting-resulting-buckets/21543/2 "2017-07-06T00:39:59Z")

</div>


