# Get count of documents through aggregation

**URL:** <https://discuss.elastic.co/t/get-count-of-documents-through-aggregation/21408>\
**Category:** Elasticsearch\
**Created:** [December 27, 2014, 8:53pm UTC](https://discuss.elastic.co/t/get-count-of-documents-through-aggregation/21408 "2014-12-27T20:53:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shelef](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@Shelef](https://discuss.elastic.co/u/Shelef)\
**Post date:** [December 27, 2014, 8:53pm UTC](https://discuss.elastic.co/t/get-count-of-documents-through-aggregation/21408/1 "2014-12-27T20:53:18Z")

</div>

Is it possible to get count of documents using aggregations?  
I try using value count aggregation  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html),  
but it seems it only working if I provied the "field" parameter, counting  
only documents containing this field.  
I tried using the same aggregation with "\_id" as field with no success.  
Any ideas?

--  
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/f1379db8-5b3f-4f3a-b5c5-88f44c41d263%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f1379db8-5b3f-4f3a-b5c5-88f44c41d263%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:** [December 29, 2014, 10:44am UTC](https://discuss.elastic.co/t/get-count-of-documents-through-aggregation/21408/2 "2014-12-29T10:44:55Z")

</div>

Can you elaborate a bit more on what you would like to do? Depending on  
your use-case, you can already use the total hit count returned by the  
search API, the count API or aggregations.

On Sat, Dec 27, 2014 at 9:53 PM, myname [shlaflaf@gmail.com](mailto:shlaflaf@gmail.com) wrote:

> Is it possible to get count of documents using aggregations?  
> I try using value count aggregation  
> [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html),  
> but it seems it only working if I provied the "field" parameter, counting  
> only documents containing this field.  
> I tried using the same aggregation with "\_id" as field with no success.  
> Any ideas?
> 
> --  
> 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/f1379db8-5b3f-4f3a-b5c5-88f44c41d263%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f1379db8-5b3f-4f3a-b5c5-88f44c41d263%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/f1379db8-5b3f-4f3a-b5c5-88f44c41d263%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/f1379db8-5b3f-4f3a-b5c5-88f44c41d263%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/CAL6Z4j7dcKqV3%2ByAsGABER0YKHONDLGni%2BoWeYj1zA1ZAHF0HQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j7dcKqV3%2ByAsGABER0YKHONDLGni%2BoWeYj1zA1ZAHF0HQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Shelef](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@Shelef](https://discuss.elastic.co/u/Shelef)\
**Post date:** [December 29, 2014, 11:21am UTC](https://discuss.elastic.co/t/get-count-of-documents-through-aggregation/21408/3 "2014-12-29T11:21:55Z")

</div>

I am building SQL api for elasticsearch, I want all sql function  
(count,avg, sum) to return as aggregation result.  
I already succeed doing this query using the field \_index. it seems to  
working.

בתאריך יום שני, 29 בדצמבר 2014 12:45:07 UTC+2, מאת Adrien Grand:

> Can you elaborate a bit more on what you would like to do? Depending on  
> your use-case, you can already use the total hit count returned by the  
> search API, the count API or aggregations.
> 
> On Sat, Dec 27, 2014 at 9:53 PM, myname \<[shla...@gmail.com](mailto:shla...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > Is it possible to get count of documents using aggregations?  
> > I try using value count aggregation  
> > [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html),  
> > but it seems it only working if I provied the "field" parameter, counting  
> > only documents containing this field.  
> > I tried using the same aggregation with "\_id" as field with no success.  
> > Any ideas?
> > 
> > --  
> > 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/f1379db8-5b3f-4f3a-b5c5-88f44c41d263%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f1379db8-5b3f-4f3a-b5c5-88f44c41d263%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/f1379db8-5b3f-4f3a-b5c5-88f44c41d263%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/f1379db8-5b3f-4f3a-b5c5-88f44c41d263%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/2f0b38fe-5f82-4a77-a849-c44e147ef4ef%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2f0b38fe-5f82-4a77-a849-c44e147ef4ef%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:41am UTC](https://discuss.elastic.co/t/get-count-of-documents-through-aggregation/21408/4 "2017-07-06T00:41:38Z")

</div>


