# Cardinality over Date Histogram

**URL:** <https://discuss.elastic.co/t/cardinality-over-date-histogram/171373>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [March 7, 2019, 6:41pm UTC](https://discuss.elastic.co/t/cardinality-over-date-histogram/171373 "2019-03-07T18:41:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcellus\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@Marcellus\_Tavares](https://discuss.elastic.co/u/Marcellus_Tavares)\
**Post date:** [March 7, 2019, 6:41pm UTC](https://discuss.elastic.co/t/cardinality-over-date-histogram/171373/1 "2019-03-07T18:41:16Z")

</div>

Hi all,

What would be the best way to query Elasticsearch in order to implement a date histogram representing the total number of unique visitors metric?

Considering the following data:

```
PUT /events

{
   "mappings" : {
        "_doc" : {
            "properties" : {
                "userId" : { "type" : "keyword" },
      					 "eventDate" : { "type" : "date" }
            }
        }
    }
}

POST /events/_bulk

{ "index" : { "_index" : "events", "_type" : "_doc", "_id" : "1" } }
{"userId": "1","eventDate": "2019-03-04T13:40:18.514Z"}
{ "index" : { "_index" : "events", "_type" : "_doc", "_id" : "2" } }
{"userId": "2","eventDate": "2019-03-04T13:46:18.514Z"}
{ "index" : { "_index" : "events", "_type" : "_doc", "_id" : "3" } }
{"userId": "3","eventDate": "2019-03-04T13:50:18.514Z"}
{ "index" : { "_index" : "events", "_type" : "_doc", "_id" : "4" } }
{"userId": "1","eventDate": "2019-03-05T13:46:18.514Z"}
{ "index" : { "_index" : "events", "_type" : "_doc", "_id" : "5" } }
{"userId": "4","eventDate": "2019-03-05T13:46:18.514Z"}
{ "index" : { "_index" : "events", "_type" : "_doc", "_id" : "6" } }

```

Now, if I query the cardinality of the userId field I get the 4 distinct visitors.

```
POST /events/_search

{
  "size": 0,
  "aggs": {
		"visitors": {
			"cardinality": {
				"field": "userId"
			}
		}
  }
}

```

However, distributing the documents over a date histogram, I get a total sum of 5 because there's a repeated userId in both buckets.

```
POST /events/_search
  {
      "size": 0,
      "aggs": {
        "visits_over_time": {
          "date_histogram": {
            "field": "eventDate",
            "interval": "1d"
          },
          "aggs": {
            "visitors": {
              "cardinality": {
                "field": "userId"
              }
            }
          }
        }
      }
    }

```

Is there a way to filter out those repeated values? What would be the best way to accomplish this?

Thanks a lot in advance!

---

<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:** [April 4, 2019, 6:41pm UTC](https://discuss.elastic.co/t/cardinality-over-date-histogram/171373/2 "2019-04-04T18:41:26Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
