# Get number of unique values in a field

**URL:** https://discuss.elastic.co/t/get-number-of-unique-values-in-a-field/20772
**Category:** Elasticsearch
**Created:** [November 17, 2014, 6:55am UTC](https://discuss.elastic.co/t/get-number-of-unique-values-in-a-field/20772 "2014-11-17T06:55:19Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Saurabh\_Minni](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saurabh_minni/32/1103_2.png) [@Saurabh\_Minni](https://discuss.elastic.co/u/Saurabh_Minni)
#### Post date: [November 17, 2014, 6:55am UTC](https://discuss.elastic.co/t/get-number-of-unique-values-in-a-field/20772/1 "2014-11-17T06:55:19Z")

</div>

Hi,  
I was trying to check Cardinality Aggregation. I believe it will give me an  
approx value of the number of unique users.

Below is what I am using.  
{  
"aggs" : {  
"user\_count" : {  
"cardinality" : {  
"field" : "userid"  
}  
}  
}  
}

Can some one confirm a few things for me.

1. What is the accuracy of the result.
2. Is this is the only way or are there other options to do this as well.
3. This feature is experimental as per docs, what are the future roadmaps  
for this, if any ?

Thanks,  
Saurabh

--  
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/35040cd2-49ed-4363-857e-f66892d64faf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/35040cd2-49ed-4363-857e-f66892d64faf%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: [November 17, 2014, 10:56am UTC](https://discuss.elastic.co/t/get-number-of-unique-values-in-a-field/20772/2 "2014-11-17T10:56:35Z")

</div>

Hi,

On Mon, Nov 17, 2014 at 7:55 AM, Saurabh Minni [saurabh.minni@gmail.com](mailto:saurabh.minni@gmail.com)  
wrote:

> Hi,  
> I was trying to check Cardinality Aggregation. I believe it will give me  
> an approx value of the number of unique users.
> 
> Below is what I am using.  
> {  
> "aggs" : {  
> "user\_count" : {  
> "cardinality" : {  
> "field" : "userid"  
> }  
> }  
> }  
> }
> 
> Can some one confirm a few things for me.
> 
> 1. What is the accuracy of the result.

The accuracy is quite good in general, we tried to give some examples in  
the documentations to show that even with rather low values of the  
precision threshold, the error is often very low. The paper about  
HyperLogLog++ (the algorithm beneath the cardinality aggregation) gives  
more information about the error margin that you can expect (see figure 8  
in particular).

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

[http://stefanheule.com/papers/edbt2013-hyperloglog.pdf](http://stefanheule.com/papers/edbt2013-hyperloglog.pdf)

> 1. Is this is the only way or are there other options to do this as well.

Not really. If you know the cardinality is going to be low (\< 1000), you  
could use a terms aggregation with a size of 0 (which tells elasticsearch  
to return all terms) and count the number of terms returned. Although this  
would help you find out the exact number of terms, this would not scale for  
high cardinalities, and the `cardinality` aggregation has optimizations  
that make it almost accurate when cardinalities are low anyway.

> 1. This feature is experimental as per docs, what are the future roadmaps  
> for this, if any ?

There are no concrete plans at the moment. When we added this aggregation  
in Elasticsearch 1.1, it was quite new in terms of functionalities that  
elasticsearch exposes, so we wanted to make it experimental in order to  
have the freedom to modify it based on feedback. The experimental flag will  
very likely be removed in the next major version.

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

---

<div class="post-metadata">

### Author: ![Saurabh\_Minni](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saurabh_minni/32/1103_2.png) [@Saurabh\_Minni](https://discuss.elastic.co/u/Saurabh_Minni)
#### Post date: [November 17, 2014, 11:05am UTC](https://discuss.elastic.co/t/get-number-of-unique-values-in-a-field/20772/3 "2014-11-17T11:05:04Z")

</div>

Hi Adrien,  
Thanks for the quick reply, this answers everything for me.

-Saurabh

On Mon, Nov 17, 2014 at 4:26 PM, Adrien Grand \<  
[adrien.grand@elasticsearch.com](mailto:adrien.grand@elasticsearch.com)\> wrote:

> Hi,
> 
> On Mon, Nov 17, 2014 at 7:55 AM, Saurabh Minni [saurabh.minni@gmail.com](mailto:saurabh.minni@gmail.com)  
> wrote:
> 
> > Hi,  
> > I was trying to check Cardinality Aggregation. I believe it will give me  
> > an approx value of the number of unique users.
> > 
> > Below is what I am using.  
> > {  
> > "aggs" : {  
> > "user\_count" : {  
> > "cardinality" : {  
> > "field" : "userid"  
> > }  
> > }  
> > }  
> > }
> > 
> > Can some one confirm a few things for me.
> > 
> > 1. What is the accuracy of the result.
> 
> The accuracy is quite good in general, we tried to give some examples in  
> the documentations to show that even with rather low values of the  
> precision threshold, the error is often very low. The paper about  
> HyperLogLog++ (the algorithm beneath the cardinality aggregation) gives  
> more information about the error margin that you can expect (see figure 8  
> in particular).
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html#_counts_are_approximate)  
> [http://stefanheule.com/papers/edbt2013-hyperloglog.pdf](http://stefanheule.com/papers/edbt2013-hyperloglog.pdf)
> 
> > 1. Is this is the only way or are there other options to do this as well.
> 
> Not really. If you know the cardinality is going to be low (\< 1000), you  
> could use a terms aggregation with a size of 0 (which tells elasticsearch  
> to return all terms) and count the number of terms returned. Although this  
> would help you find out the exact number of terms, this would not scale for  
> high cardinalities, and the `cardinality` aggregation has optimizations  
> that make it almost accurate when cardinalities are low anyway.
> 
> > 1. This feature is experimental as per docs, what are the future roadmaps  
> > for this, if any ?
> 
> There are no concrete plans at the moment. When we added this aggregation  
> in Elasticsearch 1.1, it was quite new in terms of functionalities that  
> elasticsearch exposes, so we wanted to make it experimental in order to  
> have the freedom to modify it based on feedback. The experimental flag will  
> very likely be removed in the next major version.
> 
> --  
> Adrien Grand
> 
> --  
> 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/zx9UFt2JPNY/unsubscribe](https://groups.google.com/d/topic/elasticsearch/zx9UFt2JPNY/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/CAL6Z4j6\_EWm0\_hNyuNGmTc%3DjUxd8RyctDAC65XPOWyZGhggBaw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6_EWm0_hNyuNGmTc%3DjUxd8RyctDAC65XPOWyZGhggBaw%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6\_EWm0\_hNyuNGmTc%3DjUxd8RyctDAC65XPOWyZGhggBaw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6_EWm0_hNyuNGmTc%3DjUxd8RyctDAC65XPOWyZGhggBaw%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/CAHGf3smr%2BzvvagNQEQa1tdh7m5JxxH1bhGaaOq%3Dm35MLPqq0yg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHGf3smr%2BzvvagNQEQa1tdh7m5JxxH1bhGaaOq%3Dm35MLPqq0yg%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, 12:49am UTC](https://discuss.elastic.co/t/get-number-of-unique-values-in-a-field/20772/4 "2017-07-06T00:49:35Z")

</div>


