# Cardinality Aggregation - Different Unique Counts!

**URL:** <https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433>\
**Category:** Elasticsearch\
**Created:** [May 9, 2015, 2:17pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433 "2015-05-09T14:17:59Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmpmarketing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hmpmarketing/32/44892_2.png) [@hmpmarketing](https://discuss.elastic.co/u/hmpmarketing)\
**Post date:** [May 9, 2015, 2:17pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/1 "2015-05-09T14:17:59Z")

</div>

Hey guys,

I am still trying to figure this out, 2 ElasticSearch queries, unique count on one is

112019

And the other :

164322

Here are the Requests and responses [http://pastebin.com/4kHJiL9t](http://pastebin.com/4kHJiL9t) and [http://pastebin.com/KL9XyQ6w](http://pastebin.com/KL9XyQ6w)

The actual count match for both but not the unique.

FYI, the ip\_address field type is string, Im storing IPs like I get them from client (xxx.xxx.xxx.xxx)

I just can't understand why they are coming up different, and honestly would like to know which is probably right.

Can anyone help?

Thanks so much

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [May 9, 2015, 3:17pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/2 "2015-05-09T15:17:17Z")

</div>

Maybe it is a surprise to you but cardinality aggregation unique count is just an approximated count using hyperloglog++ algorithm.

[http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations-metrics-cardinality-aggregation.html#\_counts\_are\_approximate](http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations-metrics-cardinality-aggregation.html#_counts_are_approximate)

---

<div class="post-metadata">

**Author:** ![hmpmarketing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hmpmarketing/32/44892_2.png) [@hmpmarketing](https://discuss.elastic.co/u/hmpmarketing)\
**Post date:** [May 9, 2015, 3:22pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/3 "2015-05-09T15:22:52Z")

</div>

HI jprante, thanks for your reply!

Yes, I did know the unique count is only an estimate. What I can't understand is, why both queries as I pasted on pastebin, give completely different results when it comes to the unique count.

I would like to know what Im doing wrong as they both should match?

Thanks again!

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [May 9, 2015, 5:10pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/4 "2015-05-09T17:10:50Z")

</div>

You are not doing anything wrong. In the first query, the cardinality approximation runs over the result set of the query once. In the second query, you run a date histogram and run cardinality approximations on each histo bucket. So the approximation of the cardinality is different in the two queries.

---

<div class="post-metadata">

**Author:** ![hmpmarketing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hmpmarketing/32/44892_2.png) [@hmpmarketing](https://discuss.elastic.co/u/hmpmarketing)\
**Post date:** [May 10, 2015, 9:36am UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/5 "2015-05-10T09:36:04Z")

</div>

Thanks again. I see what you are saying now.

My concern is, how accurate are they?

I am using my results to compare data against traffic purchased from ad networks. So the unique count is used to match against what we purchase at the other end, so our analytical team would pour over these numbers to make sure what we get is what we purchased, hence the need for accuracy. We really love using elastic search and its speed, seems that only thing of concern would be a unique count of ip addresses!

Would the field type affect accuracy? At the moment, we store ip address in their "native" format (xxx.xxx.xxx.xxx), field type = string. Would it make any diff storing it say, INT by converting the ip to a long number (i.e.: using php's ip2long() )?

Thanks again for the support!

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [May 10, 2015, 10:22am UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/6 "2015-05-10T10:22:37Z")

</div>

You can read in the documentation there is an error rate of 5% in the approximation.

I do not recommend approximative cardinality aggregations as a method to control financial transactions.

---

<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:** [May 10, 2015, 9:36pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/7 "2015-05-10T21:36:52Z")

</div>

How do you deduce the field cardinality from the second query? It seems to me that you are summing up unique counts per bucket which is incorrect since some ip addresses might fall into several hourly buckets and could thus be counted several times.

---

<div class="post-metadata">

**Author:** ![hmpmarketing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hmpmarketing/32/44892_2.png) [@hmpmarketing](https://discuss.elastic.co/u/hmpmarketing)\
**Post date:** [May 10, 2015, 9:44pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/8 "2015-05-10T21:44:59Z")

</div>

Hi @jpountz

Im not sure I understand what you mean by "deduce the field cardinality". Sorry, a bit new to ElasticSearch 😄

Yes, Im summing up the unique counts per bucket on the second query. I wish I knew the proper approach to this if Im doing anything wrong. According to @jprante it seems right, I just wanted to get the counts to at least match on both queries! Or at least another way to count uniques.

Every time our system gets an ad impression we write a document to ES and have a stats page where uses can see number of impressions per supplier\_id. I need to also show a unique count based on unique ad hits. Using mySQL I simply use a DISTINCT on the ip\_address field, but now I have come up with this problem on ES!

---

<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:** [May 10, 2015, 9:50pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/9 "2015-05-10T21:50:23Z")

</div>

@hmpmarketing Imagine you have two documents:

```
{ "@timestamp": "2015-05-11T00:00:00.000Z", "ip_address": "192.168.0.1" }
{ "@timestamp": "2015-05-11T01:00:00.000Z", "ip_address": "192.168.0.1" }

```

If you run a cardinality aggregation on all document you will have a count of 1 because both documents have the same ip address.

However if you build an hourly date histogram and run a cardinality aggregation on each bucket, both buckets will have a unique count of 1 because the `192.168.0.1` ip address appears once in each bucket. So the sum of the cardinalities on each bucket would be two.

---

<div class="post-metadata">

**Author:** ![hmpmarketing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hmpmarketing/32/44892_2.png) [@hmpmarketing](https://discuss.elastic.co/u/hmpmarketing)\
**Post date:** [May 10, 2015, 9:53pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/10 "2015-05-10T21:53:39Z")

</div>

@jpountz I forgot to mention I think, the required unique\_count is based on a 24h period. So my objetive is to show unique ad hits within a 24h period per supplier id. So as per your example, for me the count of 1 would be the right one.

---

<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:** [May 10, 2015, 9:58pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/11 "2015-05-10T21:58:53Z")

</div>

But this is not what your second query does? Your second query builds one bucket per hour and then computes how many documents there are inside each hourly bucket.

---

<div class="post-metadata">

**Author:** ![hmpmarketing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hmpmarketing/32/44892_2.png) [@hmpmarketing](https://discuss.elastic.co/u/hmpmarketing)\
**Post date:** [May 10, 2015, 10:03pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/12 "2015-05-10T22:03:34Z")

</div>

@jpountz yes. But my reason for posting is why both queries unique count don't match when querying between the 2 timestamps? (1430953200000 and 1431039599999). The first query returns 112019 and the date histogram returns 164322 ☹

---

<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:** [May 11, 2015, 7:46am UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/13 "2015-05-11T07:46:36Z")

</div>

> [@hmpmarketing](#):
>
> why both queries unique count don't match when querying between the 2 timestamps? (1430953200000 and 1431039599999). The first query returns 112019 and the date histogram returns 164322

There is no way to know the number of unique ip addresses between 1430953200000 and 1431039599999 with the result of the 2nd query. It is wrong to assume that the sum of the unique counts of ip addresses for each hourly bucket should be equal to the unique count of ip addresses for the whole range. See my previous example.

---

<div class="post-metadata">

**Author:** ![hmpmarketing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hmpmarketing/32/44892_2.png) [@hmpmarketing](https://discuss.elastic.co/u/hmpmarketing)\
**Post date:** [May 11, 2015, 9:00am UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/14 "2015-05-11T09:00:17Z")

</div>

Hi @jpountz Good morning (UK here)!

Thans for your kind reply. I suppose I am expecting something that won't really be possible with ElasticSearch.

Do you suggest any other approach to count the uniqueness of documents or I am basically helpless?

---

<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:** [May 11, 2015, 9:33am UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/15 "2015-05-11T09:33:26Z")

</div>

Actually I'm still not exactly sure to know what you are after, could you try to explain what you would like to compute with this aggregation? Do I get it right that you would like to only get counts for new ip addresses that have not been seen before?

If this is the case, you could work it out by using a range aggregation and building your buckets manually. I tried to build an example:

```auto
DELETE test 

PUT test 
{
  "mappings": {
    "test": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "ip_address": {
          "type": "ip"
        }
      }
    }
  }
}

PUT test/test/1
{
  "@timestamp": "2015-05-11T11:05:00",
  "ip_address": "192.168.0.1"
}

PUT test/test/2
{
  "@timestamp": "2015-05-11T12:35:00",
  "ip_address": "192.168.0.2"
}

PUT test/test/3
{
  "@timestamp": "2015-05-11T13:02:00",
  "ip_address": "192.168.0.1"
}

GET test/_search
{
  "aggs": {
    "by_hour": {
      "date_range": {
        "field": "@timestamp", 
        "ranges": [
          {
            "from": "2015-05-11T11:00:00",
            "to": "2015-05-11T12:00:00"
          },
                    {
            "from": "2015-05-11T11:00:00",
            "to": "2015-05-11T13:00:00"
          },
                    {
            "from": "2015-05-11T11:00:00",
            "to": "2015-05-11T14:00:00"
          }
        ]
      },
      "aggs": {
        "unique_ip_count": {
          "cardinality": {
            "field": "ip_address"
          }
        }
      }
    }
  }
}

```

And for instance the response would tell you that there were 2 unique ip addresses between 11AM and 1PM as well as between 11AM and 14AM so you would know that no new ip addresses were seen between 1PM and 2PM.

---

<div class="post-metadata">

**Author:** ![hmpmarketing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hmpmarketing/32/44892_2.png) [@hmpmarketing](https://discuss.elastic.co/u/hmpmarketing)\
**Post date:** [May 12, 2015, 9:37am UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/16 "2015-05-12T09:37:23Z")

</div>

Hi @jpountz! Again appreciate your reply. Sorry I was in transit yesterday and could not reply.

Let me try to explain my scenario and what Im after:

We are an adserver. Every ad hit that comes to our adservers, we create a document on ES that looks like this:

[http://pastebin.com/gZTN8mAm](http://pastebin.com/gZTN8mAm)

My current project is to switch our statistics page to ES. On our stats page we display information pertaining to each campaign\_id where users can check out info about a campaign, the most important metric being impressions and uniques by campaign\_id.

At the moment, we have some really cumbersome system to count impressions/uniques by using nginx logs instead of a straight INSERT into mySQL the moment the ad hit happens, since we deal with a lot of traffic. So our current setup involves parsing the info from the logs to database, where I simply use a DISTINCT() on the ip\_address field to count unique hits between dates inputted by user. A typical query would be something like:

SELECT campaign\_id, count(\*) as impressions, count(distinct(ip\_address)) as uniques FROM table WHERE timestamp BETWEEN X AND Y

The plan now is to collect all this data to ES instead and run queries to it, and be able to calculate the impressions as well as an approximate number of unique users that have hit our ads between 2 timestamps based on their IP address since this is probably the most accurate way to describe a single visitor.

I gave 2 examples on my first post because I will use the date histogram to generate an hourly chart as well as a total unique count per campaign when needed by user.

I hope I have explained it properly!

Thanks so much again!

---

<div class="post-metadata">

**Author:** ![hmpmarketing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hmpmarketing/32/44892_2.png) [@hmpmarketing](https://discuss.elastic.co/u/hmpmarketing)\
**Post date:** [May 12, 2015, 9:46am UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/17 "2015-05-12T09:46:15Z")

</div>

I forgot to mention, the queries will always be 24h periods, so between 2 days = 48h, 3 = 72h and so on

---

<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:** [May 12, 2015, 10:15pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/18 "2015-05-12T22:15:06Z")

</div>

Thanks for the explanation. The hourly chart makes sense, I think the only thing here is that you beware of is that the daily cardinality is not the sum of the hourly cardinalities since some ip addresses might appear on several hourly buckets.

---

<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:14am UTC](https://discuss.elastic.co/t/cardinality-aggregation-different-unique-counts/433/19 "2017-07-06T00:14:23Z")

</div>


