# How to filter nested aggregated buckets in ES

**URL:** <https://discuss.elastic.co/t/how-to-filter-nested-aggregated-buckets-in-es/67114>\
**Category:** Elasticsearch\
**Created:** [November 24, 2016, 1:41pm UTC](https://discuss.elastic.co/t/how-to-filter-nested-aggregated-buckets-in-es/67114 "2016-11-24T13:41:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![k88](https://avatars.discourse-cdn.com/v4/letter/k/a3d4f5/32.png) [@k88](https://discuss.elastic.co/u/k88)\
**Post date:** [November 24, 2016, 1:41pm UTC](https://discuss.elastic.co/t/how-to-filter-nested-aggregated-buckets-in-es/67114/1 "2016-11-24T13:41:45Z")

</div>

Hi,  
I know elsticserach provides bucket\_selector aggregation to filter the documents based on the aggregated buckets, but I have troubling, using this to filter the nested aggregated buckets.

I have a schema hierarchy as below in ES 5.0.

customer -  
Products -  
Events -

"mappings":{  
"customer":{  
"properties":{  
"party\_id":{  
"type":"integer"  
},  
"member\_class":{  
"type":"integer"  
},  
"age":{  
"type":"integer"  
},  
....  
"product":{  
"type":"nested",  
"properties":{  
"product\_id":{  
"type":"integer"  
},  
"price":{  
"type":"integer"  
},  
....,  
"event":{  
"type":"nested",  
"properties":{  
"type":{  
"type":"keyword"  
},  
....  
}  
}  
}  
}

```
     }
  }

```

}

With the bucket selector, I can group the classes and filter based on the total salary. Note that all these fields are in root document level.

{  
"size": 0,  
"aggs" : {  
"grp\_ages" : {  
"terms" : {  
"field" : "member\_class"  
},  
"aggs": {  
"total\_salary": {  
"sum": {  
"field": "salary"  
}  
},  
"sales\_bucket\_filter": {  
"bucket\_selector": {  
"buckets\_path": {  
"totalSal": "total\_salary"  
},  
"script": "params.totalSal \> 100"  
}  
}  
}  
}  
}  
}

But I cannot apply this to nested fields in product level.

{

"size":0,  
"aggs":{  
"products":{  
"nested":{  
"path":"product"  
},  
"aggs":{

```
              "product_price_sum":{  
                 "sum":{  
                    "field":"product.price"
                 }
              },
                 "percentage":{  
                 "bucket_selector":{  
                    "buckets_path":{  
                       "total_sum":"product_price_sum"
                    },
                    "script":"params.total_sum > 100"
                 }
              }
           }
        }
     }

```

}

I'm getting the below error as the response.

{  
"error": {  
"root\_cause": [],  
"type": "reduce\_search\_phase\_exception",  
"reason": "[reduce] ",  
"phase": "merge",  
"grouped": true,  
"failed\_shards": [],  
"caused\_by": {  
"type": "class\_cast\_exception",  
"reason": "org.elasticsearch.search.aggregations.bucket.nested.InternalNested cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation"  
}  
},  
"status": 503  
}

How can I achieve this nested aggregated filtering in elasticsearch?  
Is the bucket selector doesn't support nested filtering ? Any alternatives ?

Any feedback is appropriated.

Thanks.

---

<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:** [December 22, 2016, 1:41pm UTC](https://discuss.elastic.co/t/how-to-filter-nested-aggregated-buckets-in-es/67114/2 "2016-12-22T13:41:49Z")

</div>

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