# Date\_histogram - extended bounds, converting null to 0

**URL:** <https://discuss.elastic.co/t/date-histogram-extended-bounds-converting-null-to-0/296719>\
**Category:** Elasticsearch\
**Created:** [February 9, 2022, 12:00pm UTC](https://discuss.elastic.co/t/date-histogram-extended-bounds-converting-null-to-0/296719 "2022-02-09T12:00:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elasticshane](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticshane/32/99458_2.png) [@elasticshane](https://discuss.elastic.co/u/elasticshane)\
**Post date:** [February 9, 2022, 12:00pm UTC](https://discuss.elastic.co/t/date-histogram-extended-bounds-converting-null-to-0/296719/1 "2022-02-09T12:00:19Z")

</div>

Hello,

I have a collection of rooms in Elasticsearch 7.11.0, where each room has a data and a capacity for that date:

```auto
PUT /capacities_index
{
  "mappings": {
    "properties": {
      "date": {
        "type": "date"
      },
      "capacity": {
        "type": "integer"
      }
    }
  }
}

```

Data for these three dates:

```auto
POST _bulk
{"index":{"_index":"capacities_index"}}
{"date":"2023-01-01", "capacity": 10 }
{"index":{"_index":"capacities_index"}}
{"date":"2023-01-02", "capacity": 5 }
{"index":{"_index":"capacities_index"}}
{"date":"2023-01-03", "capacity": 3 }

```

I have the following query:

```auto
GET /capacities_index/_search?size=0
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "date": {
              "gte": "2023-01-01",
              "lte": "2023-01-04"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "per_day": {
      "date_histogram": {
        "field": "date",
        "calendar_interval": "day",
        "min_doc_count": 0,
        "extended_bounds": {
          "min": "2023-01-01",
          "max": "2023-01-04"
        }
      },
      "aggs": {
        "capacity_by_day": {
          "min": {
            "field": "capacity"
          }
        }
      }
    },
    "capacity": {
      "min_bucket": {
        "buckets_path": "per_day>capacity_by_day"
      }
    }
  }
}

```

I want to find the minimum capacity over a date range, but my range my not correspond to the dates I have in my index. For the query above, there isn't a specific entry for "2023-01-04", so I want the "capacity" value for this date to resolve to 0.

I get this (relevant part only) response:

```auto
"aggregations" : {
    "per_day" : {
      "buckets" : [
        {
          "key_as_string" : "2023-01-01T00:00:00.000Z",
          "key" : 1672531200000,
          "doc_count" : 1,
          "capacity_by_day" : {
            "value" : 10.0
          }
        },
        {
          "key_as_string" : "2023-01-02T00:00:00.000Z",
          "key" : 1672617600000,
          "doc_count" : 1,
          "capacity_by_day" : {
            "value" : 5.0
          }
        },
        {
          "key_as_string" : "2023-01-03T00:00:00.000Z",
          "key" : 1672704000000,
          "doc_count" : 1,
          "capacity_by_day" : {
            "value" : 3.0
          }
        },
        {
          "key_as_string" : "2023-01-04T00:00:00.000Z",
          "key" : 1672790400000,
          "doc_count" : 0,
          "capacity_by_day" : {
            "value" : null
          }
        }
      ]
    },
    "capacity" : {
      "value" : 3.0,
      "keys" : [
        "2023-01-03T00:00:00.000Z"
      ]
    }
  }

```

The `capacity_by_day` value is `null` (can I change this to 0?), and the `capacity` `min_bucket` aggregation ignores the null value.

Is there a way to convert this to return 0 for the range?

Or perhaps, I'm going about this completely the wrong way, and there's a better alternative to `date_histogram`?

Thanks for looking at my question 🙂

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 9, 2022, 12:48pm UTC](https://discuss.elastic.co/t/date-histogram-extended-bounds-converting-null-to-0/296719/2 "2022-02-09T12:48:07Z")

</div>

> [@elasticshane](#):
>
> ```auto
> "capacity": {
> "min_bucket": {
> "buckets_path": "per_day>capacity_by_day"
> }
> }
> 
> ```

Set [gap\_policy](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#gap-policy) in min bucket aggregation.

```auto
"capacity": {
      "min_bucket": {
        "buckets_path": "per_day>capacity_by_day",
        "gap_policy": "insert_zeros"
      }
    }

```

---

<div class="post-metadata">

**Author:** ![elasticshane](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticshane/32/99458_2.png) [@elasticshane](https://discuss.elastic.co/u/elasticshane)\
**Post date:** [February 9, 2022, 12:49pm UTC](https://discuss.elastic.co/t/date-histogram-extended-bounds-converting-null-to-0/296719/3 "2022-02-09T12:49:11Z")

</div>

Great! Thanks so much @Tomo_M🙂

---

<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:** [March 9, 2022, 12:49pm UTC](https://discuss.elastic.co/t/date-histogram-extended-bounds-converting-null-to-0/296719/4 "2022-03-09T12:49:34Z")

</div>

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