# Getting Totals from a Terms Aggregation

**URL:** <https://discuss.elastic.co/t/getting-totals-from-a-terms-aggregation/18701>\
**Category:** Elasticsearch\
**Created:** [July 16, 2014, 1:56pm UTC](https://discuss.elastic.co/t/getting-totals-from-a-terms-aggregation/18701 "2014-07-16T13:56:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Sander](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_sander/32/838_2.png) [@Michael\_Sander](https://discuss.elastic.co/u/Michael_Sander)\
**Post date:** [July 16, 2014, 1:56pm UTC](https://discuss.elastic.co/t/getting-totals-from-a-terms-aggregation/18701/1 "2014-07-16T13:56:50Z")

</div>

Hi,

I've started using aggregations heavily to make nifty little pie charts  
like the one below. To generate the pie chart below, I ran a terms  
aggregation on a particular field which has entries such as Sterne, Fish,  
etc. However, the terms aggregation only returns a doc\_count for each  
individual term, but I'd also like to know the total doc\_count so that I  
can create another pie slice for "other." Is there any way to do this in a  
single aggregation call?

Thanks,

[https://lh6.googleusercontent.com/-QaE4hTZVH-s/U8aD-Hifq\_I/AAAAAAAACTQ/jnCH4frOURk/s1600/pie.png](https://lh6.googleusercontent.com/-QaE4hTZVH-s/U8aD-Hifq_I/AAAAAAAACTQ/jnCH4frOURk/s1600/pie.png)

--  
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/3f2f9311-52ff-4917-9d50-a48635f60957%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3f2f9311-52ff-4917-9d50-a48635f60957%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Itamar\_Syn\_Hershko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itamar_syn_hershko/32/725_2.png) [@Itamar\_Syn\_Hershko](https://discuss.elastic.co/u/Itamar_Syn_Hershko)\
**Post date:** [July 16, 2014, 2:01pm UTC](https://discuss.elastic.co/t/getting-totals-from-a-terms-aggregation/18701/2 "2014-07-16T14:01:46Z")

</div>

You get it from the search request that wraps the terms aggregation, under  
total hits in the root of the response

--

Itamar Syn-Hershko  
[http://code972.com](http://code972.com) | @synhershko [https://twitter.com/synhershko](https://twitter.com/synhershko)  
Freelance Developer & Consultant  
Author of RavenDB in Action [http://manning.com/synhershko/](http://manning.com/synhershko/)

On Wed, Jul 16, 2014 at 4:56 PM, Michael Sander [michael.sander@gmail.com](mailto:michael.sander@gmail.com)  
wrote:

> Hi,
> 
> I've started using aggregations heavily to make nifty little pie charts  
> like the one below. To generate the pie chart below, I ran a terms  
> aggregation on a particular field which has entries such as Sterne, Fish,  
> etc. However, the terms aggregation only returns a doc\_count for each  
> individual term, but I'd also like to know the total doc\_count so that I  
> can create another pie slice for "other." Is there any way to do this in a  
> single aggregation call?
> 
> Thanks,
> 
> [https://lh6.googleusercontent.com/-QaE4hTZVH-s/U8aD-Hifq\_I/AAAAAAAACTQ/jnCH4frOURk/s1600/pie.png](https://lh6.googleusercontent.com/-QaE4hTZVH-s/U8aD-Hifq_I/AAAAAAAACTQ/jnCH4frOURk/s1600/pie.png)
> 
> --  
> 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/3f2f9311-52ff-4917-9d50-a48635f60957%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3f2f9311-52ff-4917-9d50-a48635f60957%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/3f2f9311-52ff-4917-9d50-a48635f60957%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/3f2f9311-52ff-4917-9d50-a48635f60957%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/CAHTr4ZuzRTe48eGgm%2BweE%2BnkzhZTpc8wp0bZoCYbGvLeEkZS5Q%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHTr4ZuzRTe48eGgm%2BweE%2BnkzhZTpc8wp0bZoCYbGvLeEkZS5Q%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Michael\_Sander](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_sander/32/838_2.png) [@Michael\_Sander](https://discuss.elastic.co/u/Michael_Sander)\
**Post date:** [July 16, 2014, 2:07pm UTC](https://discuss.elastic.co/t/getting-totals-from-a-terms-aggregation/18701/3 "2014-07-16T14:07:38Z")

</div>

Hm, I don't think that works as a general solution. If you're using  
sub-aggregations or a filter aggregation, the total for the entire search  
will be too high.

On Wed, Jul 16, 2014 at 10:01 AM, Itamar Syn-Hershko [itamar@code972.com](mailto:itamar@code972.com)  
wrote:

> You get it from the search request that wraps the terms aggregation, under  
> total hits in the root of the response
> 
> --
> 
> Itamar Syn-Hershko  
> [http://code972.com](http://code972.com) | @synhershko [https://twitter.com/synhershko](https://twitter.com/synhershko)  
> Freelance Developer & Consultant  
> Author of RavenDB in Action [http://manning.com/synhershko/](http://manning.com/synhershko/)
> 
> On Wed, Jul 16, 2014 at 4:56 PM, Michael Sander [michael.sander@gmail.com](mailto:michael.sander@gmail.com)  
> wrote:
> 
> > Hi,
> > 
> > I've started using aggregations heavily to make nifty little pie charts  
> > like the one below. To generate the pie chart below, I ran a terms  
> > aggregation on a particular field which has entries such as Sterne, Fish,  
> > etc. However, the terms aggregation only returns a doc\_count for each  
> > individual term, but I'd also like to know the total doc\_count so that I  
> > can create another pie slice for "other." Is there any way to do this in a  
> > single aggregation call?
> > 
> > Thanks,
> > 
> > [https://lh6.googleusercontent.com/-QaE4hTZVH-s/U8aD-Hifq\_I/AAAAAAAACTQ/jnCH4frOURk/s1600/pie.png](https://lh6.googleusercontent.com/-QaE4hTZVH-s/U8aD-Hifq_I/AAAAAAAACTQ/jnCH4frOURk/s1600/pie.png)
> > 
> > --  
> > 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/3f2f9311-52ff-4917-9d50-a48635f60957%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3f2f9311-52ff-4917-9d50-a48635f60957%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/3f2f9311-52ff-4917-9d50-a48635f60957%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/3f2f9311-52ff-4917-9d50-a48635f60957%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 a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/OM6KSy88iEo/unsubscribe](https://groups.google.com/d/topic/elasticsearch/OM6KSy88iEo/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/CAHTr4ZuzRTe48eGgm%2BweE%2BnkzhZTpc8wp0bZoCYbGvLeEkZS5Q%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHTr4ZuzRTe48eGgm%2BweE%2BnkzhZTpc8wp0bZoCYbGvLeEkZS5Q%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAHTr4ZuzRTe48eGgm%2BweE%2BnkzhZTpc8wp0bZoCYbGvLeEkZS5Q%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAHTr4ZuzRTe48eGgm%2BweE%2BnkzhZTpc8wp0bZoCYbGvLeEkZS5Q%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/CAFa8q3aSX%3D6QmTK5Qw9NoQoaHMChgbSkaYBwfqwPePVHLQqnKw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAFa8q3aSX%3D6QmTK5Qw9NoQoaHMChgbSkaYBwfqwPePVHLQqnKw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Itamar\_Syn\_Hershko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itamar_syn_hershko/32/725_2.png) [@Itamar\_Syn\_Hershko](https://discuss.elastic.co/u/Itamar_Syn_Hershko)\
**Post date:** [July 16, 2014, 2:17pm UTC](https://discuss.elastic.co/t/getting-totals-from-a-terms-aggregation/18701/4 "2014-07-16T14:17:21Z")

</div>

Aggregations operate on the results of a search query, so you can  
definitely use that total also when you have sub-aggregations. As for  
filter aggregations, you can have a subtract which acts as a sink for all  
unused docs and subtract it's count from the total count

--

Itamar Syn-Hershko  
[http://code972.com](http://code972.com) | @synhershko [https://twitter.com/synhershko](https://twitter.com/synhershko)  
Freelance Developer & Consultant  
Author of RavenDB in Action [http://manning.com/synhershko/](http://manning.com/synhershko/)

On Wed, Jul 16, 2014 at 5:07 PM, Michael Sander [michael.sander@gmail.com](mailto:michael.sander@gmail.com)  
wrote:

> Hm, I don't think that works as a general solution. If you're using  
> sub-aggregations or a filter aggregation, the total for the entire search  
> will be too high.
> 
> On Wed, Jul 16, 2014 at 10:01 AM, Itamar Syn-Hershko [itamar@code972.com](mailto:itamar@code972.com)  
> wrote:
> 
> > You get it from the search request that wraps the terms aggregation,  
> > under total hits in the root of the response
> > 
> > --
> > 
> > Itamar Syn-Hershko  
> > [http://code972.com](http://code972.com) | @synhershko [https://twitter.com/synhershko](https://twitter.com/synhershko)  
> > Freelance Developer & Consultant  
> > Author of RavenDB in Action [http://manning.com/synhershko/](http://manning.com/synhershko/)
> > 
> > On Wed, Jul 16, 2014 at 4:56 PM, Michael Sander \<[michael.sander@gmail.com](mailto:michael.sander@gmail.com)
> > 
> > > wrote:
> > 
> > > Hi,
> > > 
> > > I've started using aggregations heavily to make nifty little pie charts  
> > > like the one below. To generate the pie chart below, I ran a terms  
> > > aggregation on a particular field which has entries such as Sterne, Fish,  
> > > etc. However, the terms aggregation only returns a doc\_count for each  
> > > individual term, but I'd also like to know the total doc\_count so that I  
> > > can create another pie slice for "other." Is there any way to do this in a  
> > > single aggregation call?
> > > 
> > > Thanks,
> > > 
> > > [https://lh6.googleusercontent.com/-QaE4hTZVH-s/U8aD-Hifq\_I/AAAAAAAACTQ/jnCH4frOURk/s1600/pie.png](https://lh6.googleusercontent.com/-QaE4hTZVH-s/U8aD-Hifq_I/AAAAAAAACTQ/jnCH4frOURk/s1600/pie.png)
> > > 
> > > --  
> > > 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/3f2f9311-52ff-4917-9d50-a48635f60957%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3f2f9311-52ff-4917-9d50-a48635f60957%40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/3f2f9311-52ff-4917-9d50-a48635f60957%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/3f2f9311-52ff-4917-9d50-a48635f60957%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 a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/OM6KSy88iEo/unsubscribe](https://groups.google.com/d/topic/elasticsearch/OM6KSy88iEo/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/CAHTr4ZuzRTe48eGgm%2BweE%2BnkzhZTpc8wp0bZoCYbGvLeEkZS5Q%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHTr4ZuzRTe48eGgm%2BweE%2BnkzhZTpc8wp0bZoCYbGvLeEkZS5Q%40mail.gmail.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/CAHTr4ZuzRTe48eGgm%2BweE%2BnkzhZTpc8wp0bZoCYbGvLeEkZS5Q%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAHTr4ZuzRTe48eGgm%2BweE%2BnkzhZTpc8wp0bZoCYbGvLeEkZS5Q%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/CAFa8q3aSX%3D6QmTK5Qw9NoQoaHMChgbSkaYBwfqwPePVHLQqnKw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAFa8q3aSX%3D6QmTK5Qw9NoQoaHMChgbSkaYBwfqwPePVHLQqnKw%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAFa8q3aSX%3D6QmTK5Qw9NoQoaHMChgbSkaYBwfqwPePVHLQqnKw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAFa8q3aSX%3D6QmTK5Qw9NoQoaHMChgbSkaYBwfqwPePVHLQqnKw%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/CAHTr4ZvnieNQrHGpHfC0nc8E1hUWKU81bx4hYJD0wzVREZ7QBg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHTr4ZvnieNQrHGpHfC0nc8E1hUWKU81bx4hYJD0wzVREZ7QBg%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/getting-totals-from-a-terms-aggregation/18701/5 "2017-07-06T01:15:27Z")

</div>


