# Is the missing value option officially supported in Date Range aggregations?

**URL:** <https://discuss.elastic.co/t/is-the-missing-value-option-officially-supported-in-date-range-aggregations/45855>\
**Category:** Elasticsearch\
**Created:** [March 30, 2016, 10:09pm UTC](https://discuss.elastic.co/t/is-the-missing-value-option-officially-supported-in-date-range-aggregations/45855 "2016-03-30T22:09:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MattQuinn4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattquinn4/32/8778_2.png) [@MattQuinn4](https://discuss.elastic.co/u/MattQuinn4)\
**Post date:** [March 30, 2016, 10:09pm UTC](https://discuss.elastic.co/t/is-the-missing-value-option-officially-supported-in-date-range-aggregations/45855/1 "2016-03-30T22:09:14Z")

</div>

Terms and Histogram aggregations support a missing value option, which will default any missing or null values to a specified value. But the documentation does not mention this option for Date Range aggregations.

It looks like support for this feature was added to several aggregations in this pull request, but not Date Range aggregations:

> <https://github.com/elastic/elasticsearch/pull/11042>

Eventhough the docs do not mention support for it, using this option in a date range agg does seem to work. Is this just not officially supported for Date Range? Is this option safe to use?

I have the following query, which returns any docs with a null user\_date\_of\_birth in the 'Unknown' bucket:

```
Request:
GET myindex/user/_search
{
  "aggs": {
    "age_groups": {
      "range": {
        "field": "user_date_of_birth",
        "missing": "1899-12-31",
        "ranges": [
          {
            "key": "Unknown", 
            "to": "1900-01-01"
          },
          {
            "from": "1950-01-01"
          }
        ]
      },
      "aggs": {
        "age_group_count": {
          "value_count": {
            "field": "user_id"
          }
        }
      }
    }
  }
}

Response:
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "myindex",
        "_type": "user",
        "_id": "2",
        "_score": 1,
        "_source": {
          "user_id": 2,
          "user_date_of_birth": null
        }
      },
      {
        "_index": "myindex",
        "_type": "user",
        "_id": "1",
        "_score": 1,
        "_source": {
          "user_id": 1,
          "user_date_of_birth": "1950-01-02"
        }
      }
    ]
  },
  "aggregations": {
    "age_groups": {
      "buckets": [
        {
          "key": "Unknown",
          "to": -2208988800000,
          "to_as_string": "1900-01-01T00:00:00.000Z",
          "doc_count": 1,
          "age_group_count": {
            "value": 1
          }
        },
        {
          "key": "1950-01-01T00:00:00.000Z-*",
          "from": -631152000000,
          "from_as_string": "1950-01-01T00:00:00.000Z",
          "doc_count": 1,
          "age_group_count": {
            "value": 1
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [March 31, 2016, 8:13am UTC](https://discuss.elastic.co/t/is-the-missing-value-option-officially-supported-in-date-range-aggregations/45855/2 "2016-03-31T08:13:05Z")

</div>

In my opinion the missing option is mostly useful on numeric metric aggregations (stats, percentiles, etc.). We also added support for it to the terms aggregation since having a bucket for missing values was a feature that had been requested for a long time. I am less sure about use-cases for bucket aggregations since the missing value would be put in the same bucket as other values. For instance, I don't think the missing option is used a lot on histograms.

In your case, you seem to want to have a bucket that _only_ contains missing values. Maybe it would be better to use the `missing` aggregation for this?

---

<div class="post-metadata">

**Author:** ![MattQuinn4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattquinn4/32/8778_2.png) [@MattQuinn4](https://discuss.elastic.co/u/MattQuinn4)\
**Post date:** [March 31, 2016, 1:07pm UTC](https://discuss.elastic.co/t/is-the-missing-value-option-officially-supported-in-date-range-aggregations/45855/3 "2016-03-31T13:07:09Z")

</div>

Thanks for the reply. That was just a simple example. In my real use case I _do_ want missing values to be grouped in the same bucket as other values.

I'm just curious as to why the missing value option works for range aggregations but it is not documented as being supported.

---

<div class="post-metadata">

**Author:** ![Carmen](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@Carmen](https://discuss.elastic.co/u/Carmen)\
**Post date:** [April 1, 2016, 4:05pm UTC](https://discuss.elastic.co/t/is-the-missing-value-option-officially-supported-in-date-range-aggregations/45855/4 "2016-04-01T16:05:59Z")

</div>

I'd also love clarification on whether this is an actual safe-to-use feature in range aggregations. When bucketing by date ranges, it'd be great to be able to just specify a bucket for documents where that value is null.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [April 4, 2016, 6:50am UTC](https://discuss.elastic.co/t/is-the-missing-value-option-officially-supported-in-date-range-aggregations/45855/5 "2016-04-04T06:50:08Z")

</div>

> [@MattQuinn4](#):
>
> In my real use case I do want missing values to be grouped in the same bucket as other values.

What is your real use-case? Documenting this feature would mean that we need to commit to it, so we want to make sure it would be helpful. Having an example use-case would also help give a good example in the docs.

---

<div class="post-metadata">

**Author:** ![MattQuinn4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattquinn4/32/8778_2.png) [@MattQuinn4](https://discuss.elastic.co/u/MattQuinn4)\
**Post date:** [April 4, 2016, 7:03pm UTC](https://discuss.elastic.co/t/is-the-missing-value-option-officially-supported-in-date-range-aggregations/45855/6 "2016-04-04T19:03:12Z")

</div>

In my use case I want to group clients by age group. Any date of birth less than 1900 or a null/missing date of birth I want to fall into an 'Other' bucket.

```
Request:
GET myindex/user/_search
{
  "aggs": {
    "age_groups": {
      "date_range": {
        "field": "user_date_of_birth",
        "missing": "1899-12-31",
        "ranges": [
          {
              "key": "Generation Z",
              "from": "2000"
            },
            {
              "key": "Generation Y",
              "from": "1980",
              "to": "2000"
            },
            {
              "key": "Generation X",
              "from": "1965",
              "to": "1980"
            },
            {
              "key": "Baby Boomer",
              "from": "1946",
              "to": "1965"
            },
            {
              "key": "Silent Generation",
              "from": "1925",
              "to": "1946"
            },
            {
              "key": "Greatest Generation",
              "from": "1900",
              "to": "1925"
            },
            {
              "key": "Other",
              "to": "1900"
            }
        ]
      }
    }
  }
}

Response:
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "myindex",
        "_type": "user",
        "_id": "2",
        "_score": 1,
        "_source": {
          "user_id": 2,
          "user_date_of_birth": "1890-01-01"
        }
      },
      {
        "_index": "myindex",
        "_type": "user",
        "_id": "1",
        "_score": 1,
        "_source": {
          "user_id": 1,
          "user_date_of_birth": "1960-01-01"
        }
      },
      {
        "_index": "myindex",
        "_type": "user",
        "_id": "3",
        "_score": 1,
        "_source": {
          "user_id": 3
        }
      }
    ]
  },
  "aggregations": {
    "age_groups": {
      "buckets": [
        {
          "key": "Other",
          "to": -2208988800000,
          "to_as_string": "1900-01-01T00:00:00.000Z",
          "doc_count": 2
        },
        {
          "key": "Greatest Generation",
          "from": -2208988800000,
          "from_as_string": "1900-01-01T00:00:00.000Z",
          "to": -1420070400000,
          "to_as_string": "1925-01-01T00:00:00.000Z",
          "doc_count": 0
        },
        {
          "key": "Silent Generation",
          "from": -1420070400000,
          "from_as_string": "1925-01-01T00:00:00.000Z",
          "to": -757382400000,
          "to_as_string": "1946-01-01T00:00:00.000Z",
          "doc_count": 0
        },
        {
          "key": "Baby Boomer",
          "from": -757382400000,
          "from_as_string": "1946-01-01T00:00:00.000Z",
          "to": -157766400000,
          "to_as_string": "1965-01-01T00:00:00.000Z",
          "doc_count": 1
        },
        {
          "key": "Generation X",
          "from": -157766400000,
          "from_as_string": "1965-01-01T00:00:00.000Z",
          "to": 315532800000,
          "to_as_string": "1980-01-01T00:00:00.000Z",
          "doc_count": 0
        },
        {
          "key": "Generation Y",
          "from": 315532800000,
          "from_as_string": "1980-01-01T00:00:00.000Z",
          "to": 946684800000,
          "to_as_string": "2000-01-01T00:00:00.000Z",
          "doc_count": 0
        },
        {
          "key": "Generation Z",
          "from": 946684800000,
          "from_as_string": "2000-01-01T00:00:00.000Z",
          "doc_count": 0
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [April 7, 2016, 3:52pm UTC](https://discuss.elastic.co/t/is-the-missing-value-option-officially-supported-in-date-range-aggregations/45855/7 "2016-04-07T15:52:48Z")

</div>

Thank you. I opened [https://github.com/elastic/elasticsearch/issues/17597](https://github.com/elastic/elasticsearch/issues/17597).

---

<div class="post-metadata">

**Author:** ![MattQuinn4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattquinn4/32/8778_2.png) [@MattQuinn4](https://discuss.elastic.co/u/MattQuinn4)\
**Post date:** [April 7, 2016, 3:53pm UTC](https://discuss.elastic.co/t/is-the-missing-value-option-officially-supported-in-date-range-aggregations/45855/8 "2016-04-07T15:53:42Z")

</div>

Awesome, thanks.

---

<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:** [July 5, 2017, 11:01pm UTC](https://discuss.elastic.co/t/is-the-missing-value-option-officially-supported-in-date-range-aggregations/45855/9 "2017-07-05T23:01:25Z")

</div>


