# Doc\_count on aggregations doesn't match total hits

**URL:** https://discuss.elastic.co/t/doc-count-on-aggregations-doesnt-match-total-hits/19652
**Category:** Elasticsearch
**Created:** [September 6, 2014, 5:49am UTC](https://discuss.elastic.co/t/doc-count-on-aggregations-doesnt-match-total-hits/19652 "2014-09-06T05:49:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ron\_Sher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ron_sher/32/4472_2.png) [@Ron\_Sher](https://discuss.elastic.co/u/Ron_Sher)
#### Post date: [September 6, 2014, 5:49am UTC](https://discuss.elastic.co/t/doc-count-on-aggregations-doesnt-match-total-hits/19652/1 "2014-09-06T05:49:46Z")

</div>

Hi,

I've started to use aggregations and it works very fast and cool. But it  
seems to get the wrong doc\_count as opposed to the total hits.

For example - here's a typical search I use (I got for it total hits - 111  
and doc\_count 1348) :  
{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"term": {  
"service\_id": "880"  
}  
},  
{  
"bool": {  
"must": {  
"term": {  
"status\_group": "paying"  
}  
}  
}  
}  
]  
}  
}  
}  
},  
"aggregations": {  
"atts": {  
"nested": {  
"path": "string\_attributes"  
},  
"aggregations": {  
"filter\_atts": {  
"filter": {  
"term": {  
"string\_attributes.key": "Sales Manager"  
}  
},  
"aggregations": {  
"values": {  
"terms": {  
"field": "string\_attributes.value",  
"size": 0,  
"order": {  
"\_term": "asc"  
}  
},  
"aggregations": {  
"reversed": {  
"reverse\_nested": {},  
"aggregations": {  
"health": {  
"terms": {  
"field": "health"  
}  
},  
"missing\_health": {  
"missing": {  
"field": "health"  
}  
},  
"avg\_contract\_value": {  
"avg": {  
"field": "contract\_value"  
}  
},  
"sum\_contract\_value": {  
"sum": {  
"field": "contract\_value"  
}  
}  
}  
}  
}  
}  
}  
}  
}  
},  
"avg\_contract\_value": {  
"avg": {  
"field": "contract\_value"  
}  
},  
"sum\_contract\_value": {  
"sum": {  
"field": "contract\_value"  
}  
}  
}  
}

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

---

<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: [September 6, 2014, 5:52am UTC](https://discuss.elastic.co/t/doc-count-on-aggregations-doesnt-match-total-hits/19652/2 "2014-09-06T05:52:18Z")

</div>

It sounds like you are using nested documents. I guess it comes from here.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 6 sept. 2014 à 07:49, Ron Sher [ron.sher@gmail.com](mailto:ron.sher@gmail.com) a écrit :

Hi,

I've started to use aggregations and it works very fast and cool. But it seems to get the wrong doc\_count as opposed to the total hits.

## For example - here's a typical search I use (I got for it total hits - 111 and doc\_count 1348) : { "query": { "filtered": { "query": { "match\_all": {} }, "filter": { "bool": { "must": [{ "term": { "service\_id": "880" } }, { "bool": { "must": { "term": { "status\_group": "paying" } } } }] } } } }, "aggregations": { "atts": { "nested": { "path": "string\_attributes" }, "aggregations": { "filter\_atts": { "filter": { "term": { "string\_attributes.key": "Sales Manager" } }, "aggregations": { "values": { "terms": { "field": "string\_attributes.value", "size": 0, "order": { "\_term": "asc" } }, "aggregations": { "reversed": { "reverse\_nested": {}, "aggregations": { "health": { "terms": { "field": "health" } }, "missing\_health": { "missing": { "field": "health" } }, "avg\_contract\_value": { "avg": { "field": "contract\_value" } }, "sum\_contract\_value": { "sum": { "field": "contract\_value" } } } } } } } } } }, "avg\_contract\_value": { "avg": { "field": "contract\_value" } }, "sum\_contract\_value": { "sum": { "field": "contract\_value" } } } }

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/CAKHuyJpNLN8Zw7cQjFjTvQTpfrPLDkrhzBmEyFXApWYti1r1tQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKHuyJpNLN8Zw7cQjFjTvQTpfrPLDkrhzBmEyFXApWYti1r1tQ%40mail.gmail.com).  
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/3A592117-5A62-4B5E-A1C3-E954C275E1FC%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/3A592117-5A62-4B5E-A1C3-E954C275E1FC%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Ron\_Sher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ron_sher/32/4472_2.png) [@Ron\_Sher](https://discuss.elastic.co/u/Ron_Sher)
#### Post date: [September 6, 2014, 2:13pm UTC](https://discuss.elastic.co/t/doc-count-on-aggregations-doesnt-match-total-hits/19652/3 "2014-09-06T14:13:16Z")

</div>

So the doc count is for the nested documents?  
what if I want to get the doc count of the "parent" docs? I guess I can use  
the total hits, right?

On Saturday, September 6, 2014 8:52:30 AM UTC+3, David Pilato wrote:

> It sounds like you are using nested documents. I guess it comes from here.
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 6 sept. 2014 à 07:49, Ron Sher \<[ron....@gmail.com](mailto:ron....@gmail.com) \<javascript:\>\> a  
> écrit :
> 
> Hi,
> 
> I've started to use aggregations and it works very fast and cool. But it  
> seems to get the wrong doc\_count as opposed to the total hits.
> 
> For example - here's a typical search I use (I got for it total hits - 111  
> and doc\_count 1348) :  
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "service\_id": "880"  
> }  
> },  
> {  
> "bool": {  
> "must": {  
> "term": {  
> "status\_group": "paying"  
> }  
> }  
> }  
> }  
> ]  
> }  
> }  
> }  
> },  
> "aggregations": {  
> "atts": {  
> "nested": {  
> "path": "string\_attributes"  
> },  
> "aggregations": {  
> "filter\_atts": {  
> "filter": {  
> "term": {  
> "string\_attributes.key": "Sales Manager"  
> }  
> },  
> "aggregations": {  
> "values": {  
> "terms": {  
> "field": "string\_attributes.value",  
> "size": 0,  
> "order": {  
> "\_term": "asc"  
> }  
> },  
> "aggregations": {  
> "reversed": {  
> "reverse\_nested": {},  
> "aggregations": {  
> "health": {  
> "terms": {  
> "field": "health"  
> }  
> },  
> "missing\_health": {  
> "missing": {  
> "field": "health"  
> }  
> },  
> "avg\_contract\_value": {  
> "avg": {  
> "field": "contract\_value"  
> }  
> },  
> "sum\_contract\_value": {  
> "sum": {  
> "field": "contract\_value"  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> },  
> "avg\_contract\_value": {  
> "avg": {  
> "field": "contract\_value"  
> }  
> },  
> "sum\_contract\_value": {  
> "sum": {  
> "field": "contract\_value"  
> }  
> }  
> }  
> }
> 
> --  
> 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAKHuyJpNLN8Zw7cQjFjTvQTpfrPLDkrhzBmEyFXApWYti1r1tQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKHuyJpNLN8Zw7cQjFjTvQTpfrPLDkrhzBmEyFXApWYti1r1tQ%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAKHuyJpNLN8Zw7cQjFjTvQTpfrPLDkrhzBmEyFXApWYti1r1tQ%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAKHuyJpNLN8Zw7cQjFjTvQTpfrPLDkrhzBmEyFXApWYti1r1tQ%40mail.gmail.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/774c45ee-6ded-4577-ab8b-e517844b34d0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/774c45ee-6ded-4577-ab8b-e517844b34d0%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, 1:03am UTC](https://discuss.elastic.co/t/doc-count-on-aggregations-doesnt-match-total-hits/19652/4 "2017-07-06T01:03:59Z")

</div>


