# Unable to get min\_doc\_count bucket for histogram aggs

**URL:** <https://discuss.elastic.co/t/unable-to-get-min-doc-count-bucket-for-histogram-aggs/132369>\
**Category:** Elasticsearch\
**Created:** [May 17, 2018, 3:36pm UTC](https://discuss.elastic.co/t/unable-to-get-min-doc-count-bucket-for-histogram-aggs/132369 "2018-05-17T15:36:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vleango](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vleango/32/31342_2.png) [@vleango](https://discuss.elastic.co/u/vleango)\
**Post date:** [May 17, 2018, 3:36pm UTC](https://discuss.elastic.co/t/unable-to-get-min-doc-count-bucket-for-histogram-aggs/132369/1 "2018-05-17T15:36:33Z")

</div>

Expected: When performing a range query, I expect the histogram buckets to show all docs.  
Actual: Only the bucket that matches the range query is returned.

When performing this "match\_all" query:

```
GET /article/_search
{
  "query": {
    "match_all": {}
  }, 
  "aggs": {
    "archives": {
      "date_histogram": {
        "min_doc_count": 0, 
        "field": "created_at",
        "interval": "month"
      }
    }
  }
}

```

I get two "archives" buckets:

```
"aggregations": {
    "archives": {
      "buckets": [
        {
          "key_as_string": "2018-04-01T00:00:00.000Z",
          "key": 1522540800000,
          "doc_count": 1
        },
        {
          "key_as_string": "2018-05-01T00:00:00.000Z",
          "key": 1525132800000,
          "doc_count": 4
        }
      ]
    }

```

...

When performing this range query:

```
GET /article/_search
{
  "query": {
    "range": {
      			"created_at": {
        			"gte": "2018-05-01||/M",
        			"lt": "2018-05-01||+1M/M"
      			}
    		}
  }, 
  "aggs": {
    "archives": {
      "date_histogram": {
        "min_doc_count": 0, 
        "field": "created_at",
        "interval": "month"
      }
    }
  }
}

```

I expect the first bucket to be present with "doc\_count" = 0, but instead I only get the matching bucket in the response:

```
"aggregations": {
    "archives": {
      "buckets": [
        {
          "key_as_string": "2018-05-01T00:00:00.000Z",
          "key": 1525132800000,
          "doc_count": 4
        }
      ]
    },

```

Can someone please point me in the right direction.

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [May 20, 2018, 1:26am UTC](https://discuss.elastic.co/t/unable-to-get-min-doc-count-bucket-for-histogram-aggs/132369/2 "2018-05-20T01:26:50Z")

</div>

Generally speaking, aggregations operates _within_ the query only. This means that this date histogram, by default, will operate within the range query. But a date histogram also supports extended bounds, so you could achieve what you want with:

```json
{
  "query": {
    "range": {
      "created_at": {
        "gte": "2018-05-01||/M",
        "lt": "2018-05-01||+1M/M"
      }
    }
  },
  "aggs": {
    "archives": {
      "date_histogram": {
        "min_doc_count": 0,
        "field": "created_at",
        "interval": "month",
        "extended_bounds": {
          "min": "2018-04-01||/M",
          "max": "2018-05-01||/M"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![vleango](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vleango/32/31342_2.png) [@vleango](https://discuss.elastic.co/u/vleango)\
**Post date:** [May 31, 2018, 4:15pm UTC](https://discuss.elastic.co/t/unable-to-get-min-doc-count-bucket-for-histogram-aggs/132369/3 "2018-05-31T16:15:35Z")

</div>

Thank you for the response.

---

<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:** [June 28, 2018, 4:15pm UTC](https://discuss.elastic.co/t/unable-to-get-min-doc-count-bucket-for-histogram-aggs/132369/4 "2018-06-28T16:15:54Z")

</div>

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