Date Histogram Aggregation with Empty Days

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.

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:

{
        "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": []
            }
        }
}

You can set min_dco_count to 0 and it should fill it in.

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.

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:

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": []
        }
    }
}

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