# Bucket\_script aggregation on filters aggegation over nested documents

**URL:** <https://discuss.elastic.co/t/bucket-script-aggregation-on-filters-aggegation-over-nested-documents/227541>\
**Category:** Elasticsearch\
**Created:** [April 10, 2020, 5:49pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-on-filters-aggegation-over-nested-documents/227541 "2020-04-10T17:49:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anton\_Pirker](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anton_pirker/32/1223_2.png) [@Anton\_Pirker](https://discuss.elastic.co/u/Anton_Pirker)\
**Post date:** [April 10, 2020, 5:49pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-on-filters-aggegation-over-nested-documents/227541/1 "2020-04-10T17:49:23Z")

</div>

Hello!

I have my documents, and they contain nested "events" (like clicks on a website) documents.

Now I want to calculate the ration between nested events with name=x and nested events with name=y

This is my query:

```auto
    curl -XGET http://192.168.0.206:9200/user-data/_search?pretty -H 'Content-Type: application/json' -d '{
        "query": { ... },
        "aggs": {
            "conversation_clicks": {
                "aggs": {
                  "bucket-aggregation-for-clicks-per-conversation": {
                    "filters": {
                      "filters": {
                        "presentation-clicks": { "term" : { "events.name" : "presentation-custom-anchor" }},
                        "conversation-starts": { "term" : { "events.name" : "conversation-start" }}
                      }
                    }
                  }, 
                  "clicks-per-conversation": {
                    "bucket_script": {
                      "buckets_path": {
                        "my_clicks": "bucket-aggregation-for-clicks-per-conversation['presentation-clicks']>_count", 
                        "my_conversations": "bucket-aggregation-for-clicks-per-conversation['conversation-starts']>_count"
                      },
                      "script": "params.my_clicks / params.my_conversations"
                    }
                  }
                },
                "nested": {
                    "path": "events"
                }
            }
        },
        "size": 1
    }

```

So first I create two buckets of the nested events (called "presentation-click" and "conversation-starts") This is working fine.  
Now I want to divide the number of nested docs in the first bucket by the number of nested docs in the seconds bucket.

If I try this, I get this error:

```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.nested.InternalNested cannot be cast to class org.elasticsearch.search.aggregations.InternalMultiBucketAggregation (org.elasticsearch.search.aggregations.bucket.nested.InternalNested and org.elasticsearch.search.aggregations.InternalMultiBucketAggregation are in unnamed module of loader 'app')"
        }
      },
      "status" : 503
    }

```

I tried to do the same calculation on non-nested documents but it also did not work. It does not raise an error, but it just doesn't do the calculation. (see my other forum topic: [Bucket\_script aggregation on filters aggregation not showing results](https://discuss.elastic.co/t/bucket-script-aggregation-on-filters-aggregation-not-showing-results/227539) )

Can anyone help me understanding what the error message exactly means and how to fix my query so I get my desired result?

Thanks a lot,  
Anton

---

<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 8, 2020, 5:49pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-on-filters-aggegation-over-nested-documents/227541/2 "2020-05-08T17:49:25Z")

</div>

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