Date_histogram breakdown

I want to be able to break down my data by year and then further by month, im pretty sure this is possible but cant see exactly how.

I am using the following at the moment to get a yearly breakdown:

'by_year' => ['date_histogram' => ['field' => 'Date', 'calendar_interval' => 'year', 'format'=>'yyyy']]

results in

  0 => array:3 [▼
    "key_as_string" => "2015"
    "key" => 1420070400000
    "doc_count" => 40
  ]

this works great but I need it broken down further to include months of that year for example

 0 => array:3 [▼
   "key_as_string" => "2015"
   "key" => 1420070400000
   "doc_count" => 40,
                [
                   "key_as_string" => "January"
                    "key" => 1420070400000
                    "doc_count" => 4,
               ],
              [
                   "key_as_string" => "February"
                    "key" => 1420070400000
                    "doc_count" => 14,
               ],
              [
                   "key_as_string" => "March"
                    "key" => 1420070400000
                    "doc_count" => 20,
               ],
 ]

is this possible?

@aa09 Please check below post and try to use the script field in your sub aggregation.

for example -

GET indexname/_search
{
  "size": 0, 
  "aggs": {
    "by_year": {
      "date_histogram": {
        "field": "timestamp",
        "calendar_interval": "year",
        "format": "yyyy"
      },
      "aggs": {
        "month": {
          "terms": {
            "script": "doc['timestamp'].value.getMonthValue()"
          }
        }
      }
    }
  }
}

thanks ill check it out

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