# Cardinality aggregation always returning one extra count

**URL:** <https://discuss.elastic.co/t/cardinality-aggregation-always-returning-one-extra-count/243462>\
**Category:** Elasticsearch\
**Created:** [August 2, 2020, 2:49pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-always-returning-one-extra-count/243462 "2020-08-02T14:49:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hardik\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hardik_jain/32/73198_2.png) [@Hardik\_Jain](https://discuss.elastic.co/u/Hardik_Jain)\
**Post date:** [August 2, 2020, 2:49pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-always-returning-one-extra-count/243462/1 "2020-08-02T14:49:15Z")

</div>

I am new to elasticsearch and wanted to query the number of clients at `fixed_interval` of 1 hour and hence used the **cardinality** aggregation along with **date\_histogram** aggregation. The problem is, the cardinality aggregation is always returning one extra count. Below are the mapping, document, query and result:~

The **mapping** of the index used,

 ![mapping](https://us1.discourse-cdn.com/elastic/original/3X/5/b/5b8fa1737923bcd1f6149ef8639198cf8dd6b7a9.png)

The **document** that is being queried,

 ![document](https://us1.discourse-cdn.com/elastic/original/3X/b/c/bc07072eed492671fbfcd28aa46a197ee800eaed.png)

The **query** that I used,

```auto
{'query': {'nested': {'path': 'tags',
   'query': {'bool': {'must': [{'match': {'tags.object_id': {'query': 'e21ed0ac-701e-4fe6-b08b-00477f742774'}}},
      {'match': {'tags.content_type': {'query': 'config.device'}}}]}}}},
 '_source': False,
 'size': 0,
 'aggs': {'GroupByTime': {'nested': {'path': 'points',
    'aggs': {'set_range': {'filter': {'range': {'points.time': {'from': 'now-7d/d',
         'to': 'now/d'}}},
      'aggs': {'time': {'date_histogram': {'field': 'points.time',
         'fixed_interval': '1h',
         'format': 'date_time_no_millis',
         'order': {'_key': 'desc'},
         'time_zone': 'Asia/Kolkata'},
        'aggs': {'nest': {'nested': {'path': 'points.fields',
           'aggs': {'wifi_clients': {'cardinality': {'field': 'points.fields.clients.keyword',
              'missing': 0}}}}}}}}}}}}}}

```

The **result** obtained,

```auto
{'took': 1,
 'timed_out': False,
 '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},
 'hits': {'total': {'value': 1, 'relation': 'eq'},
  'max_score': None,
  'hits': []},
 'aggregations': {'GroupByTime': {'meta': {},
   'doc_count': 3,
   'set_range': {'meta': {},
    'doc_count': 3,
    'time': {'buckets': [{'key_as_string': '2020-08-02T18:00:00Asia/Kolkata',
       'key': 1596371400000,
       'doc_count': 3,
       'nest': {'doc_count': 3, 'wifi_clients': {'value': 3}}}]}}}}}

```

I got a result of **3** for _wifi\_clients value_ but was expecting **2** (there are only two distinct **clients** as can be seen in the document image). I have tried with different number of clients but every time got one extra count 😕.

**Note** : I was using the `elasticsearch-python client` so few variables might seem odd like _False_ in place of _false_ (stating to avoid any confusion 🙂 )

I would like to know why there is always one extra count being returned 🤔. Thanks!

---

<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:** [August 30, 2020, 2:49pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-always-returning-one-extra-count/243462/2 "2020-08-30T14:49:18Z")

</div>

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