# Terms Agg

**URL:** <https://discuss.elastic.co/t/terms-agg/170966>\
**Category:** Elasticsearch\
**Created:** [March 5, 2019, 6:03pm UTC](https://discuss.elastic.co/t/terms-agg/170966 "2019-03-05T18:03:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![aniaks](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aniaks](https://discuss.elastic.co/u/aniaks)\
**Post date:** [March 5, 2019, 6:03pm UTC](https://discuss.elastic.co/t/terms-agg/170966/1 "2019-03-05T18:03:25Z")

</div>

When running a Terms Aggregation query setting min\_doc\_count: 0 doesn't return terms with no values with zero. is there anything that needs to be checked.

---

<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:** [March 5, 2019, 6:36pm UTC](https://discuss.elastic.co/t/terms-agg/170966/2 "2019-03-05T18:36:39Z")

</div>

We only gather the values for matching docs. If you want to get aggregations on values in docs other than just those that match your query see the ‘global’ aggregation for a broader scope

---

<div class="post-metadata">

**Author:** ![aniaks](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aniaks](https://discuss.elastic.co/u/aniaks)\
**Post date:** [March 5, 2019, 6:40pm UTC](https://discuss.elastic.co/t/terms-agg/170966/3 "2019-03-05T18:40:54Z")

</div>

My understanding from the documentation is that when min\_doc\_count is set to 0 - all the terms in the search range will be returned even if that term do not have any documents in that range... Is my understanding right ? I am not sure if i am missing something.

---

<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:** [March 5, 2019, 6:45pm UTC](https://discuss.elastic.co/t/terms-agg/170966/4 "2019-03-05T18:45:36Z")

</div>

My bad. You’re right. It should return non-matching terms but subject to the ‘size’ restriction for how many terms to bring back (default 10)

---

<div class="post-metadata">

**Author:** ![aniaks](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aniaks](https://discuss.elastic.co/u/aniaks)\
**Post date:** [March 5, 2019, 7:12pm UTC](https://discuss.elastic.co/t/terms-agg/170966/5 "2019-03-05T19:12:04Z")

</div>

My size is 15, Behavior is like below

1. For a given time in a time range i am getting 3 terms with 0,
2. For another time in a time range i am getting 5 terms with 0  
3 Another time in a time range i am getting 2 terms with 0

with min\_doc\_count - we should be getting the same number of terms for all the time in a given range. is there anything should i check as to why the terms returned are not same for all time in the searched time range ?

---

<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:** [March 5, 2019, 7:27pm UTC](https://discuss.elastic.co/t/terms-agg/170966/6 "2019-03-05T19:27:04Z")

</div>

How many non-zero-count terms do you get in each of those ranges? It might be that they are more competitive choices for making up your top 15

---

<div class="post-metadata">

**Author:** ![aniaks](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aniaks](https://discuss.elastic.co/u/aniaks)\
**Post date:** [March 5, 2019, 7:29pm UTC](https://discuss.elastic.co/t/terms-agg/170966/7 "2019-03-05T19:29:49Z")

</div>

I am supposed to get 5 terms all the time, non zero terms varies depending on the time in the given range. May i know what is the impact of size in this behavior ? I am not clear on that part

In all of them max 1 or 2 are the non-zero terms

---

<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:** [March 5, 2019, 8:25pm UTC](https://discuss.elastic.co/t/terms-agg/170966/8 "2019-03-05T20:25:33Z")

</div>

I think I need to see some JSON to make requests/responses clearer

---

<div class="post-metadata">

**Author:** ![aniaks](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aniaks](https://discuss.elastic.co/u/aniaks)\
**Post date:** [March 6, 2019, 2:59pm UTC](https://discuss.elastic.co/t/terms-agg/170966/9 "2019-03-06T14:59:46Z")

</div>

For the below request

```auto
    "query": {
        "match_all": {}
    }, 
    "size": 0, 
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "timestamp",
                "interval" : "month"
            },
            "aggs": {
                "tags": {
                    "terms": {
                        "field": "tags"
                    }
                }
            }
        }
    }

```

Response is like below

```auto
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 11,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "sales_per_month": {
         "buckets": [
            {
               "key_as_string": "2016-01-01T00:00:00.000Z",
               "key": 1451606400000,
               "doc_count": 5,
               "tags": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "blue",
                        "doc_count": 3
                     },
                     {
                        "key": "green",
                        "doc_count": 2
                     },
                     {
                        "key": "red",
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key_as_string": "2016-02-01T00:00:00.000Z",
               "key": 1454284800000,
               "doc_count": 5,
               "tags": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "green",
                        "doc_count": 4
                     },
                     {
                        "key": "red",
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key_as_string": "2016-03-01T00:00:00.000Z",
               "key": 1456790400000,
               "doc_count": 1,
               "tags": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "blue",
                        "doc_count": 1
                     }
                  ]
               }
            }
         ]
      }
   }
}

```

In the above response for response key "1456790400000" i am expecting "Green" & "RED" also in the bucket with value as zero. But i am getting only "Blue" with Value "1" ....

Also for response key "1454284800000" i need "BLUE" in the buckets with "0"  
Precisely i want all the colors in the buckets returned with "0" even if they are not present.

Please let me know if any way we could achieve this ?

---

<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:** [March 6, 2019, 3:52pm UTC](https://discuss.elastic.co/t/terms-agg/170966/10 "2019-03-06T15:52:49Z")

</div>

> [@aniaks](#):
>
> Please let me know if any way we could achieve this ?

Add `"min_doc_count":0` to the terms agg?

---

<div class="post-metadata">

**Author:** ![aniaks](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aniaks](https://discuss.elastic.co/u/aniaks)\
**Post date:** [March 6, 2019, 4:09pm UTC](https://discuss.elastic.co/t/terms-agg/170966/11 "2019-03-06T16:09:22Z")

</div>

In spite of adding min\_doc\_count - I am not getting the intended results. Let me know if something is missing ....

---

<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:** [March 6, 2019, 4:11pm UTC](https://discuss.elastic.co/t/terms-agg/170966/12 "2019-03-06T16:11:40Z")

</div>

Working here on 6.6.0:

Setup:

```auto
DELETE test
PUT test
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "_doc":{
      "properties":{
        "timestamp":{
          "type":"date"
        },
        "tags":{
          "type":"keyword"
        }
      }
    }
  }
}
POST test/_doc/1
{
  "timestamp":"2016-02-01T00:00:00.000Z",
  "tags" : ["green", "red"]
}
POST test/_doc/2
{
  "timestamp":"2016-03-01T00:00:00.000Z",
  "tags" : ["blue"]
}
POST test/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "sales_per_month": {
      "date_histogram": {
        "field": "timestamp",
        "interval": "month"
      },
      "aggs": {
        "tags": {
          "terms": {
            "field": "tags",
            "min_doc_count": 0
          }
        }
      }
    }
  }
}

```

Response:

```auto
{
  "took" : 94,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.0,
    "hits" : []
  },
  "aggregations" : {
    "sales_per_month" : {
      "buckets" : [
        {
          "key_as_string" : "2016-02-01T00:00:00.000Z",
          "key" : 1454284800000,
          "doc_count" : 1,
          "tags" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "green",
                "doc_count" : 1
              },
              {
                "key" : "red",
                "doc_count" : 1
              },
              {
                "key" : "blue",
                "doc_count" : 0
              }
            ]
          }
        },
        {
          "key_as_string" : "2016-03-01T00:00:00.000Z",
          "key" : 1456790400000,
          "doc_count" : 1,
          "tags" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "blue",
                "doc_count" : 1
              },
              {
                "key" : "green",
                "doc_count" : 0
              },
              {
                "key" : "red",
                "doc_count" : 0
              }
            ]
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![aniaks](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aniaks](https://discuss.elastic.co/u/aniaks)\
**Post date:** [March 15, 2019, 12:30am UTC](https://discuss.elastic.co/t/terms-agg/170966/13 "2019-03-15T00:30:52Z")

</div>

I tried this in the application where there are millions of docs. Inspite of setting min-doc-count to 0 , the terms with no docs are not returned with zero. Would be great if you could let me know under what conditions this could happen ? Thanks in advance.

---

<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:** [March 15, 2019, 11:04am UTC](https://discuss.elastic.co/t/terms-agg/170966/14 "2019-03-15T11:04:38Z")

</div>

> [@aniaks](#):
>
> Would be great if you could let me know under what conditions this could happen ?

Maybe you have a lot of competing terms and `size` or `shard_size` settings that aren't high enough to consider all the values that should be returned?

---

<div class="post-metadata">

**Author:** ![aniaks](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aniaks](https://discuss.elastic.co/u/aniaks)\
**Post date:** [March 15, 2019, 1:38pm UTC](https://discuss.elastic.co/t/terms-agg/170966/15 "2019-03-15T13:38:14Z")

</div>

the field we are trying to do term aggregation It has high cardinality around 4200. If that would cause any impact , what should be the approach to handle this situation ?

---

<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:** [March 15, 2019, 1:48pm UTC](https://discuss.elastic.co/t/terms-agg/170966/16 "2019-03-15T13:48:57Z")

</div>

> [@aniaks](#):
>
> what should be the approach to handle this situation ?

Increase the `shard_size` as per the [guidance in the docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_shard_size_3).  
However, consider how much data you're asking for if you're nesting the `terms` agg under a `date_histogram` as per your example as the number of results becomes 4,200 x numberOfDateBuckets.

---

<div class="post-metadata">

**Author:** ![aniaks](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aniaks](https://discuss.elastic.co/u/aniaks)\
**Post date:** [March 18, 2019, 3:05pm UTC](https://discuss.elastic.co/t/terms-agg/170966/17 "2019-03-18T15:05:43Z")

</div>

shardsize by default is same as Size. I tried this and still the same issue. Let me know if anything i need to check why this issue is happening only when there is high cardinality ?

---

<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:** [March 18, 2019, 3:20pm UTC](https://discuss.elastic.co/t/terms-agg/170966/18 "2019-03-18T15:20:47Z")

</div>

> [@aniaks](#):
>
> shardsize by default is same as Size. I tried this and still the same issue.

I'm unclear - what numbers did you set for `shard_size` . and `size`?

---

<div class="post-metadata">

**Author:** ![aniaks](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aniaks](https://discuss.elastic.co/u/aniaks)\
**Post date:** [March 18, 2019, 3:40pm UTC](https://discuss.elastic.co/t/terms-agg/170966/19 "2019-03-18T15:40:00Z")

</div>

shard\_size and size both were set to 500

---

<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:** [March 18, 2019, 3:43pm UTC](https://discuss.elastic.co/t/terms-agg/170966/20 "2019-03-18T15:43:05Z")

</div>

I'm confused then. You said you wanted _all_ terms (even those with doc\_count=0) and that there were 4,200 of them. Why pick sizes less than 4,200 if that's the case?

[Next page](https://discuss.elastic.co/t/terms-agg/170966.md?page=2)
