# Count nested documents only?

**URL:** https://discuss.elastic.co/t/count-nested-documents-only/113248
**Category:** Elasticsearch
**Created:** [December 26, 2017, 10:11pm UTC](https://discuss.elastic.co/t/count-nested-documents-only/113248 "2017-12-26T22:11:24Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![bkazez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bkazez/32/25991_2.png) [@bkazez](https://discuss.elastic.co/u/bkazez)
#### Post date: [December 26, 2017, 10:11pm UTC](https://discuss.elastic.co/t/count-nested-documents-only/113248/1 "2017-12-26T22:11:24Z")

</div>

Hi, in my index, each document represents a Record Group, which includes an array of Records -  
type: nested. I need the count of Records only, not Record Groups. I've seen several people looking for this kind of thing over time and wondering if we all missed a way to do this?

[https://stackoverflow.com/questions/45143183/count-nested-documents-along-with-parent-documents-in-size-parameter-of-query](https://stackoverflow.com/questions/45143183/count-nested-documents-along-with-parent-documents-in-size-parameter-of-query)  
[https://stackoverflow.com/questions/44576150/count-nested-documents-in-elasticsearch](https://stackoverflow.com/questions/44576150/count-nested-documents-in-elasticsearch)  
[https://stackoverflow.com/questions/45143183/count-nested-documents-along-with-parent-documents-in-size-parameter-of-query?rq=1](https://stackoverflow.com/questions/45143183/count-nested-documents-along-with-parent-documents-in-size-parameter-of-query?rq=1)

> [@Get count of parent documents only](https://discuss.elastic.co/t/get-count-of-parent-documents-only/92494):
>
> Hi all, I have a document which has some nested documents. When I do something like the following, the count that comes back includes the nested document: GET loandetails2/loandetail/\_count { "query": { "bool": { "should": [{ "match": { "visibility": { "query": "3" } } }] } } } The above query is just a simplified version of what I'm actually doing. Sometimes I'm really trying to get a count based on fields in the parent document, and fields in the nested documents. But I …

Thank you,  
Ben

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [December 27, 2017, 5:27am UTC](https://discuss.elastic.co/t/count-nested-documents-only/113248/2 "2017-12-27T05:27:26Z")

</div>

Using [nested `inner_hits`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#nested-inner-hits) you can get the count of documents that are nested inside your top-level documents. However, you won't get a total count for the whole result set, you need to iterate over each hit and sum up the nested count on the client side.

---

<div class="post-metadata">

### Author: ![bkazez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bkazez/32/25991_2.png) [@bkazez](https://discuss.elastic.co/u/bkazez)
#### Post date: [December 28, 2017, 2:44pm UTC](https://discuss.elastic.co/t/count-nested-documents-only/113248/3 "2017-12-28T14:44:44Z")

</div>

That's a bummer, since many records have just 1 or 2 nested documents, and the result sets can be very large (the eventual solution needs to work even for 15k results). It seems people have needed this for quite some time and I have to imagine that internally, since the nested objects are separate documents, it must be possible to get this count faster from elasticsearch's internals than from a client-side count? Any chance of seeing an API for this someday?

---

<div class="post-metadata">

### Author: ![oguz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/oguz/32/41862_2.png) [@oguz](https://discuss.elastic.co/u/oguz)
#### Post date: [December 28, 2017, 3:06pm UTC](https://discuss.elastic.co/t/count-nested-documents-only/113248/4 "2017-12-28T15:06:42Z")

</div>

I also wanted to get inner hit count before but since it is not possible i changed my mapping to a parent-child mapping. This way you can get parent-child objects separately with has\_child, has\_parent queries.

---

<div class="post-metadata">

### Author: ![bkazez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bkazez/32/25991_2.png) [@bkazez](https://discuss.elastic.co/u/bkazez)
#### Post date: [December 28, 2017, 5:00pm UTC](https://discuss.elastic.co/t/count-nested-documents-only/113248/5 "2017-12-28T17:00:21Z")

</div>

Thanks @oguz - I considered parent-child queries, but the Elasticsearch guide says that they 'can be 5 to 10 times slower than the equivalent nested query'. Unfortunately query speed is most important for this application. Any other thoughts?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [December 28, 2017, 6:29pm UTC](https://discuss.elastic.co/t/count-nested-documents-only/113248/6 "2017-12-28T18:29:55Z")

</div>

Can't you compute this value at index time?

---

<div class="post-metadata">

### Author: ![bkazez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bkazez/32/25991_2.png) [@bkazez](https://discuss.elastic.co/u/bkazez)
#### Post date: [December 28, 2017, 8:29pm UTC](https://discuss.elastic.co/t/count-nested-documents-only/113248/7 "2017-12-28T20:29:42Z")

</div>

Ah of course. I did that and now I have the total result count but it doesn't work with "size" for result pagination ([Pagination in aggregation](https://discuss.elastic.co/t/pagination-in-aggregation/79423)). I can add a filter aggregation, but I believe I can't set the size on that filter. I'm an ES beginner, though...hopefully I'm missing something!

---

<div class="post-metadata">

### Author: ![Magnus\_Kessler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnus_kessler/32/42001_2.png) [@Magnus\_Kessler](https://discuss.elastic.co/u/Magnus_Kessler)
#### Post date: [December 29, 2017, 5:57am UTC](https://discuss.elastic.co/t/count-nested-documents-only/113248/8 "2017-12-29T05:57:26Z")

</div>

Have a look if your use case allows you to use the approach described here: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#\_filtering\_values\_with\_partitions](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_with_partitions)

With ES 6.1, there's also new functionality specifically designed to allow deep pagination (similar to the scroll API) in aggregations:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html)

---

<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: [January 26, 2018, 5:57am UTC](https://discuss.elastic.co/t/count-nested-documents-only/113248/9 "2018-01-26T05:57:56Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
