# Using a bucket script aggregation inside filter aggreagtion

**URL:** <https://discuss.elastic.co/t/using-a-bucket-script-aggregation-inside-filter-aggreagtion/139139>\
**Category:** Elasticsearch\
**Created:** [July 9, 2018, 10:24am UTC](https://discuss.elastic.co/t/using-a-bucket-script-aggregation-inside-filter-aggreagtion/139139 "2018-07-09T10:24:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gevmart](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gevmart](https://discuss.elastic.co/u/gevmart)\
**Post date:** [July 9, 2018, 10:24am UTC](https://discuss.elastic.co/t/using-a-bucket-script-aggregation-inside-filter-aggreagtion/139139/1 "2018-07-09T10:24:46Z")

</div>

Hi,

I am trying to use bucket\_script aggregations inside a filter aggregation.

My query looks like this (I used count for simplicity in this example, but actual bucket script that I need is more complicated, so I do need a bucket\_script)

> "aggs": {  
> "filt": {  
> "filter": {  
> "bool": {  
> "should": [{  
> "match": {  
> "age": "24"  
> }}]  
> }  
> },  
> "aggs": {  
> "bucket\_agg": {  
> "bucket\_script": {  
> "buckets\_path": {  
> "tc": "\_count"  
> },  
> "script": "params.tc"  
> }  
> }  
> }  
> }  
> }

I get an error from Elasticsearch saying

> {  
> "error": {  
> "root\_cause": ,  
> "type": "search\_phase\_execution\_exception",  
> "reason": "",  
> "phase": "fetch",  
> "grouped": true,  
> "failed\_shards": ,  
> "caused\_by": {  
> "type": "class\_cast\_exception",  
> "reason": "org.elasticsearch.search.aggregations.bucket.filter.InternalFilter cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation"  
> }  
> },  
> "status": 503  
> }

However, when I wrap up the bucket script aggregation inside another aggregation, terms aggregation for example, it works

Query

> ```
> "aggs": {
> "filt": {
> "filter": {
> "bool": {
> "should": [{
> "match": {
> "age": "24"
> }}]
> }
> },
> "aggs": {
> "name": {
> "terms": {
> "field": "name"
> },
> "aggs": {
> "bucket_agg": {
> "bucket_script": {
> "buckets_path": {
> "tc": "_count"
> },
> "script": "params.tc"
> }
> }
> }
> }
> }
> }
> }
> 
> ```

Is there a reason why a bucket directly under a filter is not supported and is there a way to work around this?

Thank you,  
Gevorg

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [July 9, 2018, 10:50am UTC](https://discuss.elastic.co/t/using-a-bucket-script-aggregation-inside-filter-aggreagtion/139139/2 "2018-07-09T10:50:27Z")

</div>

The `bucket_script` aggregation operates on multi-bucket aggregations. The `filter` aggregation only creates a single bucket.

The workaround is to use a `filters` aggregation instead of a `filter` agg (note the extra "s" for the plural "filters"). Something like this should work:

```auto
{
  "aggs": {
    "filt": {
      "filters": {
        "filters": {
          "age_24": {
            "bool": {
              "should": [
                {
                  "match": {
                    "age": "24"
                  }
                }
              ]
            }
          }
        }
      },
      "aggs": {
        "bucket_agg": {
          "bucket_script": {
            "buckets_path": {
              "tc": "_count"
            },
            "script": "params.tc"
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![gevmart](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gevmart](https://discuss.elastic.co/u/gevmart)\
**Post date:** [July 9, 2018, 11:22am UTC](https://discuss.elastic.co/t/using-a-bucket-script-aggregation-inside-filter-aggreagtion/139139/3 "2018-07-09T11:22:12Z")

</div>

That worked. Thank you!

But this raises another question for me. If the reason that filter doesn't work with bucket\_script is that we have a multi-bucket aggregation, why does it work with terms aggregation? It is also a multi-bucket aggregation, isn't it?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [July 9, 2018, 11:36am UTC](https://discuss.elastic.co/t/using-a-bucket-script-aggregation-inside-filter-aggreagtion/139139/4 "2018-07-09T11:36:51Z")

</div>

It is the other way around: both the terms as well as the filters aggregation are multi-bucket. It did not work with the filter aggregation because it is single-bucket.

---

<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:** [August 6, 2018, 11:36am UTC](https://discuss.elastic.co/t/using-a-bucket-script-aggregation-inside-filter-aggreagtion/139139/5 "2018-08-06T11:36:56Z")

</div>

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