# How to get doc\_count in terms aggregation?

**URL:** https://discuss.elastic.co/t/how-to-get-doc-count-in-terms-aggregation/315855
**Category:** Elasticsearch
**Tags:** rollups
**Created:** [October 5, 2022, 10:10am UTC](https://discuss.elastic.co/t/how-to-get-doc-count-in-terms-aggregation/315855 "2022-10-05T10:10:37Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Be-poz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/be-poz/32/111106_2.png) [@Be-poz](https://discuss.elastic.co/u/Be-poz)
#### Post date: [October 5, 2022, 10:10am UTC](https://discuss.elastic.co/t/how-to-get-doc-count-in-terms-aggregation/315855/1 "2022-10-05T10:10:37Z")

</div>

Hi i'd like to get doc\_count when i did terms aggregation. here is example i used.

```auto
PUT sample
{
  "mappings": {
    "properties": {
      "updateType": {"type": "keyword"},
      "date": {"type": "date", "format": "epoch_millis"}
    }
  }
}

POST _bulk
{"index":{"_index":"sample", "_id":"1"}}
{"updateType":"insert", "date": 1664755200000}
{"index":{"_index":"sample", "_id":"2"}}
{"updateType":"insert", "date": 1664755201000}
{"index":{"_index":"sample", "_id":"3"}}
{"updateType":"update", "date": 1664755202000}

```

In this case, when i did terms aggregation to updateType with raw data, result was

```auto
GET sample/_search
{
  "size": 0,
  "aggs": {
    "types": {
      "terms": {
        "field": "updateType",
        "size": 10
      }
    }
  }
}

  "aggregations" : {
    "types" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "insert",
          "doc_count" : 2
        },
        {
          "key" : "update",
          "doc_count" : 1
        }
      ]
    }
  }

```

and with 1d interval date histogram, result was the same.

```auto
GET sample/_search
{
  "size": 0,
  "aggs": {
    "times": {
      "date_histogram": {
        "field": "date",
        "interval": "1d"
      },
      "aggs": {
        "types": {
          "terms": {
            "field": "updateType",
            "size": 10
          }
        }
      }
    }
  }
}

  "aggregations" : {
    "times" : {
      "buckets" : [
        {
          "key_as_string" : "1664755200000",
          "key" : 1664755200000,
          "doc_count" : 3,
          "types" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "insert",
                "doc_count" : 2
              },
              {
                "key" : "update",
                "doc_count" : 1
              }
            ]
          }
        }
      ]
    }
  }

```

this is fine so far. problem is when i roll up this index with 'updateType' terms aggregation with 1day interval and search it, result was like

```auto
  "aggregations" : {
    "types" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "insert",
          "doc_count" : 1
        },
        {
          "key" : "update",
          "doc_count" : 1
        }
      ]
    }
  }

```

This is because there's 'insert' and 'updateType' documents during 1day interval. and it counts 1 document.

However, I want the number!! The **doc\_count !!** That's the important value to me.  
I searched a lot. And get a good alternative method.

```auto
    "dimensions": [
      {
        "date_histogram": {
          "source_field": "date",
          "fixed_interval": "60m"
        }
      },
      {
        "terms": {
          "source_field": "updateType"
        }
      }
    ],
    "metrics": [
      {
        "source_field": "date",
        "metrics": [
          {
            "value_count": {}
          },
          {
            "sum": {}
          }
        ]
      },
      {
        "source_field": "updateType",
        "metrics": [
          {
            "value_count": {}
          }
        ]
      }
    ]

```

I added 'updateType' value\_count metrics which can be added even if it's field type is not a numerics.

and i request like this.

```auto
GET example_rollup2/_search
{
  "size": 0,
  "aggs": {
    "types": {
      "terms": {
        "field": "updateType",
        "size": 10
      },
      "aggs": {
        "count": {
          "value_count": {
            "field": "updateType"
          }
        }
      }
    }
  }
}

and then response comes with like below.

  "aggregations" : {
    "types" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "insert",
          "doc_count" : 1,
          "count" : {
            "value" : 2
          }
        },
        {
          "key" : "update",
          "doc_count" : 1,
          "count" : {
            "value" : 1
          }
        }
      ]
    }
  }

```

Problem seems completely solved. But it's not...  
I also want to visualize this rolled up index with kibana.  
But in kibana visualization, there's no way to process metrics aggregation in buckets of terms aggregation.

So i'm back to square one...

**Question is, How can i get terms aggregation doc\_count in rolled up index ? ?**

---

<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: [November 2, 2022, 10:11am UTC](https://discuss.elastic.co/t/how-to-get-doc-count-in-terms-aggregation/315855/2 "2022-11-02T10:11:02Z")

</div>

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