# Bucket Script pipeline aggregation is not working with filter aggregation

**URL:** <https://discuss.elastic.co/t/bucket-script-pipeline-aggregation-is-not-working-with-filter-aggregation/196861>\
**Category:** Elasticsearch\
**Created:** [August 27, 2019, 2:14am UTC](https://discuss.elastic.co/t/bucket-script-pipeline-aggregation-is-not-working-with-filter-aggregation/196861 "2019-08-27T02:14:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roytmana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roytmana/32/44855_2.png) [@roytmana](https://discuss.elastic.co/u/roytmana)\
**Post date:** [August 27, 2019, 2:14am UTC](https://discuss.elastic.co/t/bucket-script-pipeline-aggregation-is-not-working-with-filter-aggregation/196861/1 "2019-08-27T02:14:16Z")

</div>

Fails with  
class org.elasticsearch.search.aggregations.bucket.filter.InternalFilter cannot be cast to class org.elasticsearch.search.aggregations.InternalMultiBucketAggregation

Should it be that restrictive? For instance it works with Filters aggregation

Even though it is a single bucket it is still very useful to be able to perform calculation within it. Particularly if I do it in a multilevel aggregation which starts from filter agg and then may have couple of levels of say range aggs and I want consistent pipeline aggs on every level

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [August 30, 2019, 4:54pm UTC](https://discuss.elastic.co/t/bucket-script-pipeline-aggregation-is-not-working-with-filter-aggregation/196861/2 "2019-08-30T16:54:56Z")

</div>

Could you please show what the actual query is you are trying to run? The error alone isn't sufficient to understand what is occurring.

---

<div class="post-metadata">

**Author:** ![roytmana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roytmana/32/44855_2.png) [@roytmana](https://discuss.elastic.co/u/roytmana)\
**Post date:** [September 19, 2019, 4:59pm UTC](https://discuss.elastic.co/t/bucket-script-pipeline-aggregation-is-not-working-with-filter-aggregation/196861/3 "2019-09-19T16:59:36Z")

</div>

```auto
POST test3/_doc/
{
    "type" : 1,
    "discounted" : 1,
    "sold" : 1
}
POST test3/_doc/
{
    "type" : 1,
    "discounted" : 1,
    "sold" : 0
}
POST test3/_doc/
{
    "type" : 1,
    "discounted" : 0,
    "sold" : 0
}

```

```auto

POST test3/_search
{
  "size": 0,
  "aggs": {
    "type1": {
      "filter": {
        "term": {
          "type": 1
        }
      },
      "aggs": {
        "discounted": {
          "sum": {
            "field": "discounted"
          }
        },
        "sold": {
          "sum": {
            "field": "sold"
          }
        },
        "discounted_sold": {
          "bucket_script": {
            "buckets_path": {
              "discounted": "discounted",
              "sold": "sold"
            },
            "script": "params.sold / params.discounted"
          }
        }
      }
    }
  }
}

```

```auto
{
  "error": {
    "root_cause": [],
    "type": "search_phase_execution_exception",
    "reason": "",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "class_cast_exception",
      "reason": "class org.elasticsearch.search.aggregations.bucket.filter.InternalFilter cannot be cast to class org.elasticsearch.search.aggregations.InternalMultiBucketAggregation (org.elasticsearch.search.aggregations.bucket.filter.InternalFilter and org.elasticsearch.search.aggregations.InternalMultiBucketAggregation are in unnamed module of loader 'app')"
    }
  },
  "status": 500
}

```

---

<div class="post-metadata">

**Author:** ![roytmana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roytmana/32/44855_2.png) [@roytmana](https://discuss.elastic.co/u/roytmana)\
**Post date:** [September 25, 2019, 12:39pm UTC](https://discuss.elastic.co/t/bucket-script-pipeline-aggregation-is-not-working-with-filter-aggregation/196861/4 "2019-09-25T12:39:31Z")

</div>

@rjernst please see fully working example in my post above

---

<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:** [October 23, 2019, 12:39pm UTC](https://discuss.elastic.co/t/bucket-script-pipeline-aggregation-is-not-working-with-filter-aggregation/196861/5 "2019-10-23T12:39:34Z")

</div>

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