kavi
kavi

Reputation: 172

How to group by month in Elastic search

I am using elastic search version 6.0.0 for group by month, I am using date histogram aggregation. example which I've tried :

{  
   "from":0,
   "size":2000,
   "_source":{  
      "includes":[  
         "cost",
         "date"
      ],
      "excludes":[  

      ],
      "aggregations":{  
         "date_hist_agg":{  
            "date_histogram":{  
               "field":"date",
               "interval":"month",
               "format":"M",
               "order":{  
                  "_key":"asc"
               },
               "min_doc_count":1
            },
            "aggregations":{  
               "cost":{  
                  "sum":{  
                     "field":"cost"
                  }
               }
            }
         }
      }
   }
}

and as a result i got 1(Jan/January) multiple times. As I have data of January-2016 ,January-2017 , January-2018 so will return 3 times January. but i Want January only once which contains the sum of All years of January.

Upvotes: 6

Views: 8883

Answers (2)

Ovinz
Ovinz

Reputation: 485

We can use the calendar_interval with month value:

Documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html#calendar_interval_examples

GET my_index/_search
{
  "size": 0, 
  "query": {},
  "aggs": {
    "over_time": {
      "date_histogram": {
        "field": "yourDateAttribute",
        "calendar_interval": "month",
        "format": "yyyy-MM" // <--- control the output format
      }
    }
  }
}

Upvotes: 1

Val
Val

Reputation: 217504

Instead of using a date_histogram aggregation you could use a terms aggregation with a script that extracts the month from the date.

{
  "from": 0,
  "size": 2000,
  "_source": {"includes": ["cost","date"],"excludes"[]},
  "aggregations": {
    "date_hist_agg": {
      "terms": {
        "script": "doc['date'].date.monthOfYear",
        "order": {
          "_key": "asc"
        },
        "min_doc_count": 1
      },
      "aggregations": {
        "cost": {
          "sum": {
            "field": "cost"
          }
        }
      }
    }
  }
}

Note that using scripting is not optimal, if you know you'll need the month information, just create another field with that information so you can use a simple terms aggregation on it without having to use scripting.

Upvotes: 9

Related Questions