# Remove Empty Bucket from Sub Aggregation

**URL:** <https://discuss.elastic.co/t/remove-empty-bucket-from-sub-aggregation/40958>\
**Category:** Elasticsearch\
**Created:** [February 4, 2016, 12:28pm UTC](https://discuss.elastic.co/t/remove-empty-bucket-from-sub-aggregation/40958 "2016-02-04T12:28:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![code\_blue](https://avatars.discourse-cdn.com/v4/letter/c/7ea924/32.png) [@code\_blue](https://discuss.elastic.co/u/code_blue)\
**Post date:** [February 4, 2016, 12:28pm UTC](https://discuss.elastic.co/t/remove-empty-bucket-from-sub-aggregation/40958/1 "2016-02-04T12:28:50Z")

</div>

We are using multi level Aggregation:  
`{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"and": []  
}  
}  
},

```
"aggs": {
	"FIELD1": {
		"terms": {
			"field": "City"
		},
		"aggs": {
			"FIELD2": {
				"terms": {
					"field": "Class"
				}
			}
		}
	}
}

```

}`

We have buckets of "City" and each bucket of City has buckets of "Class". Now for some, Class Bucket is empty as the value of the field is NULL. How can we remove Empty Bucket from the Aggregation.

---

<div class="post-metadata">

**Author:** ![Geunmoon\_Oh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/geunmoon_oh/32/7507_2.png) [@Geunmoon\_Oh](https://discuss.elastic.co/u/Geunmoon_Oh)\
**Post date:** [February 5, 2016, 12:29am UTC](https://discuss.elastic.co/t/remove-empty-bucket-from-sub-aggregation/40958/2 "2016-02-05T00:29:29Z")

</div>

i made script below.

```
{
  "size": 0,
  "aggregations": {
    "in_product": {
      "filter": {
        "exists": {
          "field": "Class"
        }
      },
      "aggs": {
        "FIELD1": {
          "terms": {
            "field": "City"
          },
          "aggs": {
            "FIELD2": {
              "terms": {
                "field": "Class"
              }
            }
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![code\_blue](https://avatars.discourse-cdn.com/v4/letter/c/7ea924/32.png) [@code\_blue](https://discuss.elastic.co/u/code_blue)\
**Post date:** [February 5, 2016, 3:33am UTC](https://discuss.elastic.co/t/remove-empty-bucket-from-sub-aggregation/40958/3 "2016-02-05T03:33:57Z")

</div>

Thanks this worked 🙂

---

<div class="post-metadata">

**Author:** ![code\_blue](https://avatars.discourse-cdn.com/v4/letter/c/7ea924/32.png) [@code\_blue](https://discuss.elastic.co/u/code_blue)\
**Post date:** [February 5, 2016, 3:44am UTC](https://discuss.elastic.co/t/remove-empty-bucket-from-sub-aggregation/40958/4 "2016-02-05T03:44:38Z")

</div>

Another question would be how can we get back the documents even if the Class field is null. Currently we are getting empty buckets when Class is Null for some keys in City.

---

<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:18pm UTC](https://discuss.elastic.co/t/remove-empty-bucket-from-sub-aggregation/40958/5 "2017-07-05T23:18:45Z")

</div>


