Elasticsearch date_histogram not show full range aggregation

73 views Asked by At

This is my query. I'm using date_histogram with range from Jan - Dec 2021.

{
  "from": 0,
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "medicalRecordDiagnosesDischarge.catDiseaseGroupDetail.catDiseasesGroup.diseasesGroupId": {
              "value": 13,
              "boost": 1
            }
          }
        },
        {
          "range": {
            "examinationDate": {
              "from": "2021-01-01T00:00:00.000Z",
              "to": "2022-01-01T00:00:00.000Z",
              "include_lower": true,
              "include_upper": false,
              "boost": 1
            }
          }
        }
      ]
    }
  },
  "aggregations": {
    "agg_name": {
      "date_histogram": {
        "field": "examinationDate",
        "format": "MM",
        "calendar_interval": "1M",
        "offset": 0,
        "order": {
          "_key": "asc"
        },
        "keyed": false,
        "min_doc_count": 0
      }
    }
  }
}

And results:

"buckets" : [
        {
          "key_as_string" : "01",
          "key" : 1609459200000,
          "doc_count" : 66
        },
        {
          "key_as_string" : "02",
          "key" : 1612137600000,
          "doc_count" : 18
        },
        {
          "key_as_string" : "03",
          "key" : 1614556800000,
          "doc_count" : 114
        },
        {
          "key_as_string" : "04",
          "key" : 1617235200000,
          "doc_count" : 15
        },
        {
          "key_as_string" : "05",
          "key" : 1619827200000,
          "doc_count" : 22
        },
        {
          "key_as_string" : "06",
          "key" : 1622505600000,
          "doc_count" : 1
        },
        {
          "key_as_string" : "07",
          "key" : 1625097600000,
          "doc_count" : 2
        },
        {
          "key_as_string" : "08",
          "key" : 1627776000000,
          "doc_count" : 3
        }
      ]

as you can see that result only show data from 01 -> 08 month. Missing 09,10,11,12 month data

The result of query that I want is:

{
          "key_as_string" : "01"
        },
        {
          "key_as_string" : "02"
        },
        {
          "key_as_string" : "03"
        },
        {
          "key_as_string" : "04"
        },
        {
          "key_as_string" : "05"
        },
        {
          "key_as_string" : "06"
        },
        {
          "key_as_string" : "07"
        },
        {
          "key_as_string" : "08"
        },
        {
          "key_as_string" : "09"
        },
        {
          "key_as_string" : "10"
        },
        {
          "key_as_string" : "11"
        },
        {
          "key_as_string" : "12"
        }

How can I get my desired result? I tried using extended_bounds but it has been douple result which wrong. (same with calendar_interval is quarter)

1

There are 1 answers

4
Hossein Jafari On

Have you tried using the size parameter within the aggregation?

{
   "aggregations": {
    "agg_name": {
      "date_histogram": {
        "field": "examinationDate",
        ...
        "size": 12
      }
    }
  }
}