0

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)

Hossein Jafari
  • 94
  • 1
  • 11

1 Answers1

0

Have you tried using the size parameter within the aggregation?

{
   "aggregations": {
    "agg_name": {
      "date_histogram": {
        "field": "examinationDate",
        ...
        "size": 12
      }
    }
  }
}
Hossein Jafari
  • 94
  • 1
  • 11
  • Yes. I was try but same result. You have another idea for that? @HosseinJafari – Tho Ho Xuan Dec 26 '22 at 16:16
  • What version of Elasticsearch are you using? – Hossein Jafari Jan 02 '23 at 15:28
  • I'm using 7.10 version @ Hossein Jafari – Tho Ho Xuan Jan 05 '23 at 16:35
  • I suspect there should be an inaccurate assumption with your data, as date_histogram aggregation in ES 7 should return all of your buckets (max number of buckets is set to 10k by default if I recall). Are you sure that there are any indexed documents with `diseasesGroupId = 13` AND `2021-09-01T00:00:00.000Z <= examinationDate <= 2022-01-01T00:00:00.000Z` A simple query with the mentioned parameters should give you the clue to the problem. – Hossein Jafari Jan 06 '23 at 17:17