Unable to get min_doc_count bucket for histogram aggs


(Vleango) #1

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.


(Thiago Souza) #2

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:

{
  "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"
        }
      }
    }
  }
}

(Vleango) #3

Thank you for the response.


(system) #4

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