# Aggregations on date fields

**URL:** <https://discuss.elastic.co/t/aggregations-on-date-fields/73262>\
**Category:** Elasticsearch\
**Created:** [January 30, 2017, 6:44pm UTC](https://discuss.elastic.co/t/aggregations-on-date-fields/73262 "2017-01-30T18:44:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevenmwall](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevenmwall/32/12199_2.png) [@stevenmwall](https://discuss.elastic.co/u/stevenmwall)\
**Post date:** [January 30, 2017, 6:44pm UTC](https://discuss.elastic.co/t/aggregations-on-date-fields/73262/1 "2017-01-30T18:44:40Z")

</div>

I am trying to create an aggregation on a date field (in my case, named "startDate") that will put documents into buckets based upon the startDate month (while ignoring the year). For example, documents with a start date of 2017-Jan-30 and 2016-Jan-12 should go into the same bucket.

Ultimately I would like my aggregation response to be something like this:

```
  "aggregations": {
    "startDateByMonth": {
      "buckets": [
        {
          "key_as_string": "01",
          "key": "January",
          "doc_count": 3
        },
        {
          "key_as_string": "02",
          "key": "February",
          "doc_count": 1
        },
        {
          "key_as_string": "03",
          "key": "March",
          "doc_count": 6
        },
        //Other months with doc_counts
}

```

I have tried using the Date Histogram aggregation but I don't think it is quite what I need, or at least I'm not seeing what I need to do.

I would appreciate if anyone has any ideas.

Thanks,  
Steve

---

<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:** [January 31, 2017, 9:24am UTC](https://discuss.elastic.co/t/aggregations-on-date-fields/73262/2 "2017-01-31T09:24:40Z")

</div>

Hey,

I see two solutions here. One one query time and one on index time.

#### Query time

Use a script in your aggregation to extract the month of a date. This however means to run for each document

```auto
GET logs/_search
{
  "size": 0,
  "aggs": {
    "per-month": {
      "terms": {
        "script": { "inline" : "return doc['date'].getDate().getMonthOfYear()" }
      }
    }
  }
}

```

#### Index time

how about using an ingest processor to write the month on index time and then simply run a terms aggregations on query (which is much faster). Check out this pipeline

```auto
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "inline": "ctx.month = LocalDateTime.parse(ctx.date).getMonth()"
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "date": "2017-02-26T00:00"
      }
    }
  ]
}

```

Creating the above pipeline and adding it to your index operations will always create your field of the month in each document.

--Alex

---

<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:** [February 28, 2017, 9:25am UTC](https://discuss.elastic.co/t/aggregations-on-date-fields/73262/3 "2017-02-28T09:25:18Z")

</div>

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