# Get all distincts values of a field ( more than 10k values)

**URL:** https://discuss.elastic.co/t/get-all-distincts-values-of-a-field-more-than-10k-values/195101
**Category:** Elasticsearch
**Created:** [August 13, 2019, 9:01pm UTC](https://discuss.elastic.co/t/get-all-distincts-values-of-a-field-more-than-10k-values/195101 "2019-08-13T21:01:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![humartinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/humartinez/32/46395_2.png) [@humartinez](https://discuss.elastic.co/u/humartinez)
#### Post date: [August 13, 2019, 9:01pm UTC](https://discuss.elastic.co/t/get-all-distincts-values-of-a-field-more-than-10k-values/195101/1 "2019-08-13T21:01:47Z")

</div>

Hi there,  
Perhaps this question was asked many times but I haven't seen an answer that fits me. Im looking for the best way to get all the distincts values of a field in a group of indices and I manage to create a script in order to do this but It doesn't feel right to me so Im asking help form the experts.

```auto
partitions=25
rm run*
for (( i=0;i<$partitions;i++))
do
  curl -s -u user:password 'http://10.x.x.x:9200/index-*'/_search?pretty -H 'Content-Type: application/json' -d"
  {
     \"size\": 0,
     \"aggs\": {
        \"expired_sessions\": {
           \"terms\": {
              \"field\": \"data.device.deviceid\",
              \"include\": {
                 \"partition\": $i,
                 \"num_partitions\": $partitions
              },
              \"size\": 10000
           }
        }
     }
  }
  " > run.$i
done
cat run*|jq .aggregations.expired_sessions.buckets[].key

```

In fact the number of devices is differs when I run a cardinality query

```auto
GET index-*/_search
{
  "size": 0,
  "aggs": {
    "count": {
      "cardinality": {
        "field": "data.device.deviceid"
      }
    }
  }
}

```

The cardinality query retuns 62102 vs the term agg that returns 61990

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [August 13, 2019, 9:17pm UTC](https://discuss.elastic.co/t/get-all-distincts-values-of-a-field-more-than-10k-values/195101/2 "2019-08-13T21:17:37Z")

</div>

A couple of points:

1. the cardinality aggs is, by design, approximate - see the docs
2. if you don’t need to sort the terms by a child agg the ‘composite’ agg is probably simpler than using the ‘terms’ aggregation with partitioning

---

<div class="post-metadata">

### Author: ![humartinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/humartinez/32/46395_2.png) [@humartinez](https://discuss.elastic.co/u/humartinez)
#### Post date: [August 16, 2019, 2:55pm UTC](https://discuss.elastic.co/t/get-all-distincts-values-of-a-field-more-than-10k-values/195101/3 "2019-08-16T14:55:22Z")

</div>

Colud you please give me an example of composite agg to do this, I can't figure It out

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [August 16, 2019, 3:04pm UTC](https://discuss.elastic.co/t/get-all-distincts-values-of-a-field-more-than-10k-values/195101/4 "2019-08-16T15:04:06Z")

</div>

The [after](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html#_after) param is what allows you to page the composite agg.

---

<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: [September 13, 2019, 3:04pm UTC](https://discuss.elastic.co/t/get-all-distincts-values-of-a-field-more-than-10k-values/195101/5 "2019-09-13T15:04:11Z")

</div>

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