# Date Histogram Aggregation with Empty Days

**URL:** <https://discuss.elastic.co/t/date-histogram-aggregation-with-empty-days/271436>\
**Category:** Elasticsearch\
**Created:** [April 27, 2021, 7:44pm UTC](https://discuss.elastic.co/t/date-histogram-aggregation-with-empty-days/271436 "2021-04-27T19:44:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)\
**Post date:** [April 27, 2021, 7:44pm UTC](https://discuss.elastic.co/t/date-histogram-aggregation-with-empty-days/271436/1 "2021-04-27T19:44:03Z")

</div>

With the Date Histogram Aggregation is it possible to return a complete list of days, even if none of them have found results?

I am using a query to look for sales that occurred in a range of days, but in some cases there are no sales in the defined period, or just a sale at the end of the period, so only the days that have something appear.

```auto
localhost:9200/sales/_search?size=0`

{
    "query": {
        "range": {
                "date": {
                    "from": "2020-01-01T00:00:00+0300",
                    "to": "2020-03-01T00:00:00+0300",
                    "include_lower": true,
                    "include_upper": true,
                    "boost": 1.0
                }
            }
    },
    "aggs": {
            "sale_date": {
                "date_histogram": {
                "field": "date",
                "interval": "day"
            }
        }
    }
}

```

Returns:

```auto
{
        "took": 1,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 0,
            "max_score": 0.0,
            "hits": []
        },
        "aggregations": {
            "sale_date": {
                "buckets": []
            }
        }
}

```

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 27, 2021, 7:59pm UTC](https://discuss.elastic.co/t/date-histogram-aggregation-with-empty-days/271436/2 "2021-04-27T19:59:37Z")

</div>

You can set min\_dco\_count to 0 and it should fill it in.

---

<div class="post-metadata">

**Author:** ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)\
**Post date:** [April 27, 2021, 8:10pm UTC](https://discuss.elastic.co/t/date-histogram-aggregation-with-empty-days/271436/3 "2021-04-27T20:10:12Z")

</div>

> [@nik9000](#):
>
> min\_dco\_count

I put the `min_doc_count` inside the date\_histogram, and it didn't work. (I'm using Elasticsearch 6.8)

In querys where there is no data on any day, he returned the empty buckets. In querys that are only one day with data (e.g. 2020-01-01T00: 00:00+0300 until 2021-03-11T00:00:00 + 300), the bucket came with just one day.

```auto
QUERY:
{
    "query": {
        "range": {
            "date": {
                "from": "2020-01-01T00:00:00+0300",
                "to": "2021-03-11T00:00:00+0300",
                "include_lower": true,
                "include_upper": true,
                "boost": 1.0
            }
        }
    },
    "aggs": {
        "sale_date": {
            "date": {
                "field": "date",
                "interval": "day",
                "min_doc_count": 0
            }
        }
        
    }
}
RESULT:
{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 8,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "sale_date": {
            "buckets": [
                {
                    "key_as_string": "2021-03-10T00:00:00Z",
                    "key": 1615334400000,
                    "doc_count": 8
                }
            ]
        }
    }
}

```

Another Example:

```auto
QUERY:
{
    "query": {
        "range": {
            "date": {
                "from": "2020-01-01T00:00:00+0300",
                "to": "2020-03-11T00:00:00+0300",
                "include_lower": true,
                "include_upper": true,
                "boost": 1.0
            }
        }
    },
    "aggs": {
        "sale_date": {
            "date": {
                "field": "date",
                "interval": "day",
                "min_doc_count": 0
            }
        }
        
    }
}
RESULT:
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "sale_date": {
            "buckets": []
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 25, 2021, 8:11pm UTC](https://discuss.elastic.co/t/date-histogram-aggregation-with-empty-days/271436/4 "2021-05-25T20:11:03Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
