# Filter out terms aggregation buckets in elastic search after applying aggregation

**URL:** https://discuss.elastic.co/t/filter-out-terms-aggregation-buckets-in-elastic-search-after-applying-aggregation/269486
**Category:** Elasticsearch
**Created:** [April 7, 2021, 2:32pm UTC](https://discuss.elastic.co/t/filter-out-terms-aggregation-buckets-in-elastic-search-after-applying-aggregation/269486 "2021-04-07T14:32:09Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ruzeenpalsetia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ruzeenpalsetia/32/86724_2.png) [@ruzeenpalsetia](https://discuss.elastic.co/u/ruzeenpalsetia)
#### Post date: [April 7, 2021, 2:32pm UTC](https://discuss.elastic.co/t/filter-out-terms-aggregation-buckets-in-elastic-search-after-applying-aggregation/269486/1 "2021-04-07T14:32:09Z")

</div>

Below is snapshot of the dataset:

```auto
recordNo employeeId employeeStatus employeeAddr
   1 employeeA Permanent   
   2 employeeA ABC
   3 employeeB Contract    
   4 employeeB CDE

```

I want to get the list of employees along with employeeStatus and employeeAddr.

So I am using terms aggregation on employeeId and then using sub-aggregations of employeeStatus and employeeAddr to get these details. Below query returns the results correctly.

```auto
{
    "aggregations": {
        "Employee": {
            "terms": {
                "field": "employeeID"
            
            },
            "aggregations": {
                "employeeStatus": {
                    "terms": {"field": "employeeStatus"}
                },
                "employeeAddr": {
                    "terms": {"field": "employeeAddr"}
                }
            }
        }
    }
}
        

```

Now I want only the employees which are in Permanent status. So I am applying filter aggregation.

```auto
{
    "aggregations": {
        "filter_Employee_employeeID": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "employeeStatus": {"query": "Permanent"}
                            }
                        }
                    ]
                }
            },
            "aggregations": {
                "Employee": {
                    "terms": {
                        "field": "employeeID"
                    },
                    "aggregations": {
                        "employeeStatus": {
                            "terms": {"field": "employeeStatus"}
                        },
                        "employeeAddr": {
                            "terms": {"field": "employeeAddr"}
                        }
                    }
                }
            }
        }
    }    

}

```

Now the problem is that the employeeAddr aggregation returns no buckets for employeeA because record 2 gets filtered out before the aggregation is done.

Assuming that I cannot modify the data set and I want to achieve the result with a single elastic query, how can I do it?

I checked the Bucket Selector pipeline aggregation but it only works for metric aggregations. Is there a way to filter out term buckets after the aggregation is applied?

---

<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: [May 5, 2021, 2:32pm UTC](https://discuss.elastic.co/t/filter-out-terms-aggregation-buckets-in-elastic-search-after-applying-aggregation/269486/2 "2021-05-05T14:32:33Z")

</div>

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