# Return only matching bucket in aggregation

**URL:** https://discuss.elastic.co/t/return-only-matching-bucket-in-aggregation/53713
**Category:** Elasticsearch
**Created:** [June 22, 2016, 9:49pm UTC](https://discuss.elastic.co/t/return-only-matching-bucket-in-aggregation/53713 "2016-06-22T21:49:22Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Pradeep\_Gowda](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@Pradeep\_Gowda](https://discuss.elastic.co/u/Pradeep_Gowda)
#### Post date: [June 22, 2016, 9:49pm UTC](https://discuss.elastic.co/t/return-only-matching-bucket-in-aggregation/53713/1 "2016-06-22T21:49:22Z")

</div>

As per the document for min\_doc\_count

> Setting min\_doc\_count=0 will also return buckets for terms that didn’t match any hit. However, some of the returned terms which have a document count of zero might only belong to deleted documents or documents from other types, so there is no warranty that a match\_all query would find a positive document count for those terms.

Is there any way to return only the matching buckets and if not part of the cluster fill with 0.

```
{
  "aggs": {
    "matching_term": {
      "filter": {
        "query": {
          "bool": {
            "must": [
              {
                "terms": {
                  "test_filed": [
                    "a",
                    "b",
                    "c",
                    "d",
                    "e",
                    "f"
                  ]
                }
              }
            ]
          }
        }
      },
      "aggs": {
        "time": {
          "date_histogram": {
            "field": "time",
            "interval": "2h",
            "min_doc_count": 0
          },
          "aggregations": {
            "test_filed": {
              "terms": {
                "field": "test_filed",
                "shard_size": 0,
                "min_doc_count": 0,
                "size": 5
              },
              "aggregations": {
                "test_filed_value": {
                  "sum": {
                    "field": "test_filed_value"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

Actaul response is as below for an bucket

```
{
          "key_as_string" : "2016-06-21T20:00:00.000Z",
          "key" : 1466539200000,
          "doc_count" : 5949,
          "test_filed" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [ {
              "key" : "a",
              "doc_count" : 5949,
              "total_bytes" : {
                "value" : 3.0950937E7
              }
            }, {
              "key" : "b",
              "doc_count" : 0,
              "total_bytes" : {
                "value" : 0.0
              }
            }, {
              "key" : "x",
              "doc_count" : 0,
              "total_bytes" : {
                "value" : 0.0
              }
            }, {
              "key" : "y",
              "doc_count" : 0,
              "total_bytes" : {
                "value" : 0.0
              }
            }, {
              "key" : "z",
              "doc_count" : 0,
              "total_bytes" : {
                "value" : 0.0
              }
            } ]
          }
        }

```

but the expected response is

```
{
              "key_as_string" : "2016-06-21T20:00:00.000Z",
              "key" : 1466539200000,
              "doc_count" : 5949,
              "test_filed" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [ {
                  "key" : "a",
                  "doc_count" : 5949,
                  "total_bytes" : {
                    "value" : 3.0950937E7
                  }
                }, {
                  "key" : "b",
                  "doc_count" : 0,
                  "total_bytes" : {
                    "value" : 0.0
                  }
                }, {
                  "key" : "d",
                  "doc_count" : 0,
                  "total_bytes" : {
                    "value" : 0.0
                  }
                }, {
                  "key" : "e",
                  "doc_count" : 0,
                  "total_bytes" : {
                    "value" : 0.0
                  }
                }, {
                  "key" : "f",
                  "doc_count" : 0,
                  "total_bytes" : {
                    "value" : 0.0
                  }
                } ]
              }
            }

```

So how can I restrict the bucket to the matching term?

---

<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, 10:41pm UTC](https://discuss.elastic.co/t/return-only-matching-bucket-in-aggregation/53713/2 "2017-07-05T22:41:17Z")

</div>


