# Counting unique root objects on nested aggregations

**URL:** <https://discuss.elastic.co/t/counting-unique-root-objects-on-nested-aggregations/18719>\
**Category:** Elasticsearch\
**Created:** [July 17, 2014, 3:46pm UTC](https://discuss.elastic.co/t/counting-unique-root-objects-on-nested-aggregations/18719 "2014-07-17T15:46:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kallin\_Nagelberg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kallin_nagelberg/32/1007_2.png) [@Kallin\_Nagelberg](https://discuss.elastic.co/u/Kallin_Nagelberg)\
**Post date:** [July 17, 2014, 3:46pm UTC](https://discuss.elastic.co/t/counting-unique-root-objects-on-nested-aggregations/18719/1 "2014-07-17T15:46:29Z")

</div>

I'm trying to build a query to aggregate on some fields in a nested  
document, but instead of returning the count of the nested documents for  
each aggregation, I'd like to know the number of root objects.

IE.,

I have a mapping like (from the docs):

"product" : {  
"properties" : {  
"resellers" : {  
"type" : "nested"  
"properties" : {  
"name" : { "type" : "string" },  
"price" : { "type" : "double" }  
}  
}  
}  
}

Now let's say I want to know how many products have each reseller name.  
That's not straight forward as far as I can tell.

If I do an agg like:

"aggs": {  
"resellers": {  
"nested": {  
"path": "resellers"  
},  
"aggs": {  
"names": {  
"terms": {  
"field": "resellers.name"  
...

I'll get back something like ( assuming that each product has many  
resellers):

hits: 20,  
aggregations: {  
resellers: {  
doc\_count: 100,  
names : {  
buckets: [  
{  
key: 'name1'  
doc\_count: 50  
},  
{  
key: 'name2',  
doc\_count: 50  
}  
]  
}}}

So, its aggregating on the nested objects, ie there are 100 reseller nested  
docs, and in them 50 have name1, 50 have name2.

What I'm interested in though is how many products have resellers with name  
1 and name 2.

IE, it should say something like,

- 15 products have a reseller w/ name name1
- 10 products have a reseller w/ name name2

It looks like I can do this by putting a reverse nested aggregation below  
my names agg, then do a value\_count aggregation on the ID of the root  
object. This seems kind of round about and I wonder if I'm missing an  
easier way. Any suggestions would be appreciated !

Thanks,  
-Kal

--  
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/1e895c15-accc-4638-b19c-ec1d263ca53b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1e895c15-accc-4638-b19c-ec1d263ca53b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Kallin\_Nagelberg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kallin_nagelberg/32/1007_2.png) [@Kallin\_Nagelberg](https://discuss.elastic.co/u/Kallin_Nagelberg)\
**Post date:** [July 17, 2014, 6:17pm UTC](https://discuss.elastic.co/t/counting-unique-root-objects-on-nested-aggregations/18719/2 "2014-07-17T18:17:37Z")

</div>

I realized this could be simplified by simply leaving out the 'value\_count'  
aggregation within the reverse\_nested, as that information is already  
provided by the included 'doc\_count'. I guess it can't be simplified much  
beyond this.

Would it be worth including this information by default when doing a nested  
agg (doc\_count on the reverse\_nested?). It seems pretty useful, but not  
sure about performance implications of always doing it. An option  
nested\_aggs to return doc\_count of the parent would be a nice to have for  
sure!

On Thu, Jul 17, 2014 at 11:46 AM, Kallin Nagelberg \<  
[kallin.nagelberg@gmail.com](mailto:kallin.nagelberg@gmail.com)\> wrote:

> I'm trying to build a query to aggregate on some fields in a nested  
> document, but instead of returning the count of the nested documents for  
> each aggregation, I'd like to know the number of root objects.
> 
> IE.,
> 
> I have a mapping like (from the docs):
> 
> "product" : {  
> "properties" : {  
> "resellers" : {  
> "type" : "nested"  
> "properties" : {  
> "name" : { "type" : "string" },  
> "price" : { "type" : "double" }  
> }  
> }  
> }  
> }
> 
> Now let's say I want to know how many products have each reseller name.  
> That's not straight forward as far as I can tell.
> 
> If I do an agg like:
> 
> "aggs": {  
> "resellers": {  
> "nested": {  
> "path": "resellers"  
> },  
> "aggs": {  
> "names": {  
> "terms": {  
> "field": "resellers.name"  
> ...
> 
> I'll get back something like ( assuming that each product has many  
> resellers):
> 
> hits: 20,  
> aggregations: {  
> resellers: {  
> doc\_count: 100,  
> names : {  
> buckets: [  
> {  
> key: 'name1'  
> doc\_count: 50  
> },  
> {  
> key: 'name2',  
> doc\_count: 50  
> }  
> ]  
> }}}
> 
> So, its aggregating on the nested objects, ie there are 100 reseller  
> nested docs, and in them 50 have name1, 50 have name2.
> 
> What I'm interested in though is how many products have resellers with  
> name 1 and name 2.
> 
> IE, it should say something like,
> 
> - 15 products have a reseller w/ name name1
> - 10 products have a reseller w/ name name2
> 
> It looks like I can do this by putting a reverse nested aggregation below  
> my names agg, then do a value\_count aggregation on the ID of the root  
> object. This seems kind of round about and I wonder if I'm missing an  
> easier way. Any suggestions would be appreciated !
> 
> Thanks,  
> -Kal
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/5f9iHPo5-Ps/unsubscribe](https://groups.google.com/d/topic/elasticsearch/5f9iHPo5-Ps/unsubscribe).  
> To unsubscribe from this group and all its topics, 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/1e895c15-accc-4638-b19c-ec1d263ca53b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1e895c15-accc-4638-b19c-ec1d263ca53b%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/1e895c15-accc-4638-b19c-ec1d263ca53b%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1e895c15-accc-4638-b19c-ec1d263ca53b%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CAC7UURF8%3D\_1-J4LyCADUOnOQ82Bc5dt\_%3DYTAmAYV9isXXt6UJg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAC7UURF8%3D_1-J4LyCADUOnOQ82Bc5dt_%3DYTAmAYV9isXXt6UJg%40mail.gmail.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, 1:15am UTC](https://discuss.elastic.co/t/counting-unique-root-objects-on-nested-aggregations/18719/3 "2017-07-06T01:15:08Z")

</div>


