# How to get aggregations with buckets of zero count only for the filter provided

**URL:** <https://discuss.elastic.co/t/how-to-get-aggregations-with-buckets-of-zero-count-only-for-the-filter-provided/238861>\
**Category:** Elasticsearch\
**Created:** [June 26, 2020, 12:57pm UTC](https://discuss.elastic.co/t/how-to-get-aggregations-with-buckets-of-zero-count-only-for-the-filter-provided/238861 "2020-06-26T12:57:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiranvpawar](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@kiranvpawar](https://discuss.elastic.co/u/kiranvpawar)\
**Post date:** [June 26, 2020, 12:57pm UTC](https://discuss.elastic.co/t/how-to-get-aggregations-with-buckets-of-zero-count-only-for-the-filter-provided/238861/1 "2020-06-26T12:57:59Z")

</div>

Elastic search aggregation using min\_doc\_count=0 returns all the buckets which are not related to query results or hits or provided filter within the query. It is correct based on documentation provided by Elastic Search.

But, What I was looking for is that a way to fetch all buckets falling under filter mentioned in query but with buckets of zero count as well. Currently, it returns all buckets from whole data under that index with zero count.

Here is my query -

```auto
{
    "from": 0,
    "size": 100,
    "query": {
        "bool": {
            "filter": [
                {
                    "terms": {
                        "folderId.keyword": [
                            "ff98505e-cdff-43aa-8b05-197bc3f3265e"
                        ],
                        "boost": 1
                    }
                },
                {
                    "terms": {
                        "objectType.keyword": [
                            "File"
                        ],
                        "boost": 1
                    }
                },
                {
                    "term": {
                        "tenantId": {
                            "value": "34202",
                            "boost": 1
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
    },
      "aggs":{
        "_byformat":{
            "terms":{
               "field":"format.keyword",
               "min_doc_count":0,
               "size":200
            }
        }
   }
}

```

result with min\_doc\_count = 0, but it contains bucket with zero count from entire data under index. I need buckets with zero count but for filtered documents and not for whole data set. It looks weird but is there any way?

```auto
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "plnesdv1-34202-1",
        "_type" : "_doc",
        "_id" : "6adbda83-53ad-457f-a2ab-d5b04c643005",
        "_score" : 0.0,
        "_source" : {
          "format" : "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          "externalSharing" : "N",
          "description" : null,
          "dateModified" : null,
          "type" : "application",
          "folderId" : "ff98505e-cdff-43aa-8b05-197bc3f3265e",
          "tags" : [],
          "objectType" : "File",
          "dateCreated" : null,
          "name" : "New XLSX file",
          "tenantId" : "34202",
          "modifiedBy" : "rdt001",
          "id" : "6adbda83-53ad-457f-a2ab-d5b04c643005",
          "status" : "active",
          "expirationDate" : null
        }
      },
      {
        "_index" : "plnesdv1-34202-1",
        "_type" : "_doc",
        "_id" : "b1000a15-2d80-41f4-a5df-ba5c27f8e9c6",
        "_score" : 0.0,
        "_source" : {
          "format" : "vnd.ms-excel",
          "externalSharing" : "N",
          "description" : null,
          "dateModified" : null,
          "type" : "application",
          "folderId" : "ff98505e-cdff-43aa-8b05-197bc3f3265e",
          "tags" : [],
          "objectType" : "File",
          "dateCreated" : null,
          "name" : "New XLS file",
          "tenantId" : "34202",
          "modifiedBy" : "rdt001",
          "id" : "b1000a15-2d80-41f4-a5df-ba5c27f8e9c6",
          "status" : "active",
          "expirationDate" : null
        }
      },
      {
        "_index" : "plnesdv1-34202-1",
        "_type" : "_doc",
        "_id" : "630e9f49-3368-408d-a091-03f253127004",
        "_score" : 0.0,
        "_source" : {
          "format" : "msword",
          "externalSharing" : "N",
          "description" : null,
          "dateModified" : null,
          "type" : "application",
          "folderId" : "ff98505e-cdff-43aa-8b05-197bc3f3265e",
          "tags" : [],
          "objectType" : "File",
          "dateCreated" : null,
          "name" : "New DOC file",
          "tenantId" : "34202",
          "modifiedBy" : "rdt001",
          "id" : "630e9f49-3368-408d-a091-03f253127004",
          "status" : "active",
          "expirationDate" : null
        }
      }
    ]
  },
  "aggregations" : {
    "_byformat" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "msword",
          "doc_count" : 1
        },
        {
          "key" : "vnd.ms-excel",
          "doc_count" : 1
        },
        {
          "key" : "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          "doc_count" : 1
        },
        {
          "key" : "bmp",
          "doc_count" : 0
        },
        {
          "key" : "gif",
          "doc_count" : 0
        },
        {
          "key" : "html",
          "doc_count" : 0
        }       
      ]
    }
  }
}

```

result with min\_doc\_count = 1 but it does not contain buckets with zero count which is correct -

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "plnesdv1-34202-1",
        "_type" : "_doc",
        "_id" : "6adbda83-53ad-457f-a2ab-d5b04c643005",
        "_score" : 0.0,
        "_source" : {
          "format" : "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          "externalSharing" : "N",
          "description" : null,
          "dateModified" : null,
          "type" : "application",
          "folderId" : "ff98505e-cdff-43aa-8b05-197bc3f3265e",
          "tags" : [],
          "objectType" : "File",
          "dateCreated" : null,
          "name" : "New XLSX file",
          "tenantId" : "34202",
          "modifiedBy" : "rdt001",
          "id" : "6adbda83-53ad-457f-a2ab-d5b04c643005",
          "status" : "active",
          "expirationDate" : null
        }
      },
      {
        "_index" : "plnesdv1-34202-1",
        "_type" : "_doc",
        "_id" : "b1000a15-2d80-41f4-a5df-ba5c27f8e9c6",
        "_score" : 0.0,
        "_source" : {
          "format" : "vnd.ms-excel",
          "externalSharing" : "N",
          "description" : null,
          "dateModified" : null,
          "type" : "application",
          "folderId" : "ff98505e-cdff-43aa-8b05-197bc3f3265e",
          "tags" : [],
          "objectType" : "File",
          "dateCreated" : null,
          "name" : "New XLS file",
          "tenantId" : "34202",
          "modifiedBy" : "rdt001",
          "id" : "b1000a15-2d80-41f4-a5df-ba5c27f8e9c6",
          "status" : "active",
          "expirationDate" : null
        }
      },
      {
        "_index" : "plnesdv1-34202-1",
        "_type" : "_doc",
        "_id" : "630e9f49-3368-408d-a091-03f253127004",
        "_score" : 0.0,
        "_source" : {
          "format" : "msword",
          "externalSharing" : "N",
          "description" : null,
          "dateModified" : null,
          "type" : "application",
          "folderId" : "ff98505e-cdff-43aa-8b05-197bc3f3265e",
          "tags" : [],
          "objectType" : "File",
          "dateCreated" : null,
          "name" : "New DOC file",
          "tenantId" : "34202",
          "modifiedBy" : "rdt001",
          "id" : "630e9f49-3368-408d-a091-03f253127004",
          "status" : "active",
          "expirationDate" : null
        }
      }
    ]
  },
  "aggregations" : {
    "_byformat" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "msword",
          "doc_count" : 1
        },
        {
          "key" : "vnd.ms-excel",
          "doc_count" : 1
        },
        {
          "key" : "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          "doc_count" : 1
        }
      ]
    }
  }
}

```

Any way to get aggregation on buckets only for hits and with zero count as well.

---

<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 24, 2020, 12:58pm UTC](https://discuss.elastic.co/t/how-to-get-aggregations-with-buckets-of-zero-count-only-for-the-filter-provided/238861/2 "2020-07-24T12:58:09Z")

</div>

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