# Cardinality Aggregation gives wrong number?

**URL:** <https://discuss.elastic.co/t/cardinality-aggregation-gives-wrong-number/167116>\
**Category:** Elasticsearch\
**Created:** [February 5, 2019, 12:15pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-gives-wrong-number/167116 "2019-02-05T12:15:55Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![Wassim\_Ben\_Amor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wassim_ben_amor/32/39799_2.png) [@Wassim\_Ben\_Amor](https://discuss.elastic.co/u/Wassim_Ben_Amor)\
**Post date:** [February 5, 2019, 3:30pm UTC](https://discuss.elastic.co/t/cardinality-aggregation-gives-wrong-number/167116/10 "2019-02-05T15:30:36Z")

</div>

Thank you everyone for your help.  
I have made the correct JS code, I hope it will help others :

```
var users = {}
var N = 20;
var arr = Array.apply(null, { length: N }).map(Number.call, Number)
client.msearch({
  body: [].concat.apply([], arr.map((number) => ([
    { index: 'my_index' },
    {
      "size": 0,
      "aggs": {
        "my_terms": {
          "terms": {
            "field": "user_id",
            "include": {
              "partition": number,
              "num_partitions": 20
            },
            "size": 10000
          }
        }
      }
    }
  ])))
}, (err, result) => {
  if (err) {
    console.log(err)
  }
  else {
    result.responses.forEach(resp => {
      resp.aggregations.my_terms.buckets.forEach(term => {
        if (!users[term.key]) {
          users[term.key] = 1
        }
      })
    })
    console.log(Object.keys(users).length)
  }
})

```

I don't know if I'm choosing the right number of size & num\_partitions.  
While i know that the approximate number is near to 200k, so I have made 20 partitions of 10k.  
Please @Mark_Harwood if this is the correct way to choose the size & num\_partitions.  
Thank you.

---

_[View the full topic](https://discuss.elastic.co/t/cardinality-aggregation-gives-wrong-number/167116)._
