How to filter nested aggregated buckets in ES

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.

1 Like

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