简体   繁体   中英

Elasticsearch date_histogram not show full range aggregation

This is my query. I'm using date_histogram with range from Jun - 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 result above? I was try extended_bounds but it has been douple result which wrong. (same with calendar_interval is quarter)

Have you tried using the size parameter within the aggregation?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM