Elasticsearch Date histogram aggregation without considering year

789 views Asked by At

I want to aggregate data with respect to month and date, I'm trying with date histogram aggregation but its considering full date object.

I have data like:

      {
          "created" : "2020-09-26",
          "id" : 6,
          "name" : "Halum khamu"      
      },
      {
          "created" : "2021-09-26",
          "id" : 7,
          "name" : "new new"
      },
      {
          "created" : "2020-10-07",
          "id" : 8,
          "name" : "Halum new"
      }

and I'm using this query:

GET /regulations/_search/
{
  "aggs": {
    "news_over_time": {
      "date_histogram": {
        "field": "created",
        "calendar_interval": "day",
        "keyed": true,
        "format": "yyy-MM-dd",
        "min_doc_count": 1
      }
    }
  }
}

Its returning data for me:

      "buckets" : {
        "2020-09-26" : {
          "key_as_string" : "2020-09-26",
          "key" : 1600300800000,
          "doc_count" : 1
        },
        "2021-09-26" : {
          "key_as_string" : "2021-09-26",
          "key" : 1601337600000,
          "doc_count" : 1
        },
        "2020-10-07" : {
          "key_as_string" : "2020-10-07",
          "key" : 1632873600000,
          "doc_count" : 1
        }
      }

But my expected output will be without considering year:

      "buckets" : {
        "09-26" : {
          "key_as_string" : "09-26",
          "key" : 1600300800000,
          "doc_count" : 2
        },
        "10-07" : {
          "key_as_string" : "10-07",
          "key" : 1632873600000,
          "doc_count" : 1
        }
      }

How can I do that?

1

There are 1 answers

0
Joe - Check out my books On

There's no straightforward way to solve this but this should get the job done:

GET regulations/_search
{
  "size": 0,
  "aggs": {
    "day_histogram": {
      "scripted_metric": {
        "init_script": "state.day_map = [:];",
        "map_script": """
          def created = doc.created.value;
          
          def month = created.getMonthOfYear();
          def day = created.getDayOfMonth();
          
          def key = String.format('%02d', new def[] { month })  
                    + '-' + 
                    String.format('%02d', new def[] { day });
          
          if (state.day_map.containsKey(key)) {
            state.day_map[key] += 1;
          } else {
            state.day_map[key] = 1;
          }
        """,
        "combine_script": "return state.day_map",
        "reduce_script": "return states"
      }
    }
  }
}

yielding

{
  ...
  "aggregations":{
    "day_histogram":{
      "value":[
        {
          "09-26":2,
          "10-07":1
        }
      ]
    }
  }
}

EDIT -- There actually is an easier way:

GET regulations/_search
{
  "size": 0,
  "aggs": {
    "day_histogram": {
      "terms": {
        "script": {
          "source": "doc.created.value.monthOfYear + '-' + doc.created.value.dayOfMonth"
        },
        "size": 10
      }
    }
  }
}