# How to build a terms agg with a high cardinate filed and a low cardinate field

**URL:** <https://discuss.elastic.co/t/how-to-build-a-terms-agg-with-a-high-cardinate-filed-and-a-low-cardinate-field/250305>\
**Category:** Elasticsearch\
**Created:** [September 29, 2020, 7:38am UTC](https://discuss.elastic.co/t/how-to-build-a-terms-agg-with-a-high-cardinate-filed-and-a-low-cardinate-field/250305 "2020-09-29T07:38:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zjd-melo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zjd-melo/32/90749_2.png) [@zjd-melo](https://discuss.elastic.co/u/zjd-melo)\
**Post date:** [September 29, 2020, 7:38am UTC](https://discuss.elastic.co/t/how-to-build-a-terms-agg-with-a-high-cardinate-filed-and-a-low-cardinate-field/250305/1 "2020-09-29T07:38:10Z")

</div>

Hello,  
there are two fields in my docs, cid and os. Cid is a high cardinate field, while os  
either android or ios.  
I want to know the difference between the following aggs, which is better.

```auto
GET _search
{
  "size": 0, 
  "aggs":{
      "cid":{
        "terms": {
          "field": "cid",
          "size": 1000
        },
        "aggs": {
          "os": {
            "terms": {
              "field": "os"
            }
          }
        }
      }
    }
}

GET _search
{
  "size": 0, 
  "aggs":{
      "os":{
        "terms": {
          "field": "os"
        },
        "aggs": {
          "cid": {
            "terms": {
              "field": "cid",
              "size": 1000
            }
          }
        }
      }
    }
}

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 29, 2020, 9:49am UTC](https://discuss.elastic.co/t/how-to-build-a-terms-agg-with-a-high-cardinate-filed-and-a-low-cardinate-field/250305/2 "2020-09-29T09:49:41Z")

</div>

There is no notion of _better_, as both return different data

The first one returns the first 1000 'cids' and for each of those, you will get a count that returns the operating system, either android or ios. This means you will see a thousand different cids.

The second query however can return more than 1000 different cids, as it will return the first 1000 cids for android and the first 1000 cids for IOS, which might be completely different cids, up to 2000 unique ones.

Hope that answers the questions.

---

<div class="post-metadata">

**Author:** ![zjd-melo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zjd-melo/32/90749_2.png) [@zjd-melo](https://discuss.elastic.co/u/zjd-melo)\
**Post date:** [September 29, 2020, 9:59am UTC](https://discuss.elastic.co/t/how-to-build-a-terms-agg-with-a-high-cardinate-filed-and-a-low-cardinate-field/250305/3 "2020-09-29T09:59:33Z")

</div>

Thanks,  
if the unique value of cids is under 1000, the results is same, in this situation, which is better?

---

<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:** [September 29, 2020, 10:05am UTC](https://discuss.elastic.co/t/how-to-build-a-terms-agg-with-a-high-cardinate-filed-and-a-low-cardinate-field/250305/4 "2020-09-29T10:05:30Z")

</div>

If a cid typically only has one os then the os-\>cid form will use less bytes than the cid-\>os to convey the same information.  
Either way, watch out for non-zero values in [doc\_count\_error\_upper\_bound](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_calculating_document_count_error) in results. If this happens consider increasing [shard\_size](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_shard_size_3) setting to trade RAM for accuracy.

---

<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:** [October 27, 2020, 10:05am UTC](https://discuss.elastic.co/t/how-to-build-a-terms-agg-with-a-high-cardinate-filed-and-a-low-cardinate-field/250305/5 "2020-10-27T10:05:43Z")

</div>

Closed on October 27, 2020, 10:05am UTC

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