# Terms Bucket Aggregation count per Year

**URL:** <https://discuss.elastic.co/t/terms-bucket-aggregation-count-per-year/284487>\
**Category:** Elasticsearch\
**Created:** [September 17, 2021, 1:44pm UTC](https://discuss.elastic.co/t/terms-bucket-aggregation-count-per-year/284487 "2021-09-17T13:44:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manisekhar\_techo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manisekhar_techo/32/78185_2.png) [@Manisekhar\_techo](https://discuss.elastic.co/u/Manisekhar_techo)\
**Post date:** [September 17, 2021, 1:44pm UTC](https://discuss.elastic.co/t/terms-bucket-aggregation-count-per-year/284487/1 "2021-09-17T13:44:38Z")

</div>

I have a terms Bucket Aggregation, is there a way for me to get the date histogram for those individual bucket items by combining the date histogram with that

date Histogram

```auto
{
    "dates": {
        "date_histogram": {
            "field": "pulbicationDate",
            "interval": "year",
            "format": "yyyy"
        }
    }
}

```

Buckets Aggregation

```auto
{
    "aggs": {
        "category": {
            "terms": {
                "field": "categoryId",
                "size": 200
            }
        }
    }
}

```

combining the both, I was Expecting something like

```auto
{
    "buckets": {
        "item1": {
            "size": 10
        },
        "datehistogram": [
            {
                "year": 2020,
                "count": 8
            },
            {
                "year": 2021,
                "count": 2
            }
        ]
    }
}

```

Thank you

---

<div class="post-metadata">

**Author:** ![tmp13](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tmp13/32/78005_2.png) [@tmp13](https://discuss.elastic.co/u/tmp13)\
**Post date:** [September 20, 2021, 7:07am UTC](https://discuss.elastic.co/t/terms-bucket-aggregation-count-per-year/284487/2 "2021-09-20T07:07:02Z")

</div>

I'm not sure I understood the question, but  
for example on simple index logs-\* that have documents like this:

```auto
        {
          "@timestamp" : 893964682,
          "clientip" : "247.37.0.0",
          "request" : "GET /images/hm_nbg.jpg HTTP/1.0",
          "status" : 304,
          "size" : 0
        }

```

You can do query (i split by month):

```auto
GET logs-*/_search
{
  "size": 0,
  "aggs": {
    "ip_list": {
      "terms": {
        "field": "clientip",
        "size": 200
      },
      "aggs": {
        "ip_over_time": {
          "date_histogram": {
            "field": "@timestamp",
            "calendar_interval": "month"
          }
        }
      }
    }
  }
}

```

and result backets will be:

```auto
...
        {
          "key" : "129.0.0.0",
          "doc_count" : 277423,
          "ip_over_time" : {
            "buckets" : [
              {
                "key_as_string" : "1998-04-01T00:00:00.000Z",
                "key" : 891388800000,
                "doc_count" : 138
              },
              {
                "key_as_string" : "1998-05-01T00:00:00.000Z",
                "key" : 893980800000,
                "doc_count" : 51589
              },
              {
                "key_as_string" : "1998-06-01T00:00:00.000Z",
                "key" : 896659200000,
                "doc_count" : 225696
              }
            ]
          }
        },
        {
          "key" : "94.35.0.0",
          "doc_count" : 258085,
          "ip_over_time" : {
            "buckets" : [
              {
                "key_as_string" : "1998-05-01T00:00:00.000Z",
                "key" : 893980800000,
                "doc_count" : 40416
              },
              {
                "key_as_string" : "1998-06-01T00:00:00.000Z",
                "key" : 896659200000,
                "doc_count" : 217669
              }
            ]
          }
        },
etc....

```

---

<div class="post-metadata">

**Author:** ![Manisekhar\_techo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manisekhar_techo/32/78185_2.png) [@Manisekhar\_techo](https://discuss.elastic.co/u/Manisekhar_techo)\
**Post date:** [September 20, 2021, 7:34am UTC](https://discuss.elastic.co/t/terms-bucket-aggregation-count-per-year/284487/3 "2021-09-20T07:34:49Z")

</div>

Thank you Petr Degtiarev,

sorry for not being descriptive. I posted the same question again in this thread  
[https://discuss.elastic.co/t/date-histogram-for-bucket-aggregation/284592](https://discuss.elastic.co/t/date-histogram-for-bucket-aggregation/284592)

funnily posted the answer as a question again. Maybe I've done something wrong when hitting the cluster. after seeing your answer only then I went back and tried my answer/question again, this time it works.

Thank you so much for your help.

---

<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 18, 2021, 7:35am UTC](https://discuss.elastic.co/t/terms-bucket-aggregation-count-per-year/284487/4 "2021-10-18T07:35:01Z")

</div>

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