# Bucket\_script within Children aggregation

**URL:** https://discuss.elastic.co/t/bucket-script-within-children-aggregation/63515
**Category:** Elasticsearch
**Created:** [October 20, 2016, 1:50pm UTC](https://discuss.elastic.co/t/bucket-script-within-children-aggregation/63515 "2016-10-20T13:50:11Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![lwinch2006](https://avatars.discourse-cdn.com/v4/letter/l/779978/32.png) [@lwinch2006](https://discuss.elastic.co/u/lwinch2006)
#### Post date: [October 20, 2016, 1:50pm UTC](https://discuss.elastic.co/t/bucket-script-within-children-aggregation/63515/1 "2016-10-20T13:50:11Z")

</div>

Hello.  
I would like to ask if it possible to run bucket script within children aggregation in a query.  
Aggregation looks like this one.

```
"aggs": {
"summary": {
  "terms": {
    "field": "customerId",
    "size": 100000
  },
  "aggs": {
    "summary_per_children": {
      "children": {
        "type": "projects"
      },
      "aggs": {
        "Total_number_of_job_views": {
          "sum": {
            "field": "numberOfUsers"
          }
        },
        "Total_number_of_applications": {
          "sum": {
            "field": "numberOfApplicants"
          }
        },
        "Job views turning into an application": {
          "bucket_script": {
            "buckets_path": {
              "totalViews": "Total_number_of_job_views",
              "totalApplicants": "Total_number_of_applications"
            },
            "script": {
              "lang": "painless",
              "inline": "if (params.totalViews > 0) {params.totalApplicants / params.totalViews * 100} else {0}"
            }
          }
        }            
      }
    }
  }
}
  }

```

So now I get just this error below.

```
{
   "error": {
"root_cause": [],
"type": "reduce_search_phase_exception",
"reason": "[reduce] ",
"phase": "fetch",
"grouped": true,
"failed_shards": [],
"caused_by": {
  "type": "class_cast_exception",
  "reason": "org.elasticsearch.search.aggregations.bucket.children.InternalChildren cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation"
}
  },
  "status": 503
}

```

OR this one if pathes are  
"totalViews": "summary\_per\_children\>Total\_number\_of\_job\_views",  
"totalApplicants": "summary\_per\_children\>Total\_number\_of\_applications"

```
{
  "error": {
"root_cause": [
  {
    "type": "illegal_argument_exception",
    "reason": "No aggregation found for path [summary_per_children>Total_number_of_applications]"
  }
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
  {
    "shard": 0,
    "index": "",
    "node": "",
    "reason": {
      "type": "illegal_argument_exception",
      "reason": "No aggregation found for path [summary_per_children>Total_number_of_applications]"
    }
  }
],
"caused_by": {
  "type": "illegal_argument_exception",
  "reason": "No aggregation found for path [summary_per_children>Total_number_of_applications]"
}
  },
  "status": 400
}
```

---

<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: [July 5, 2017, 10:10pm UTC](https://discuss.elastic.co/t/bucket-script-within-children-aggregation/63515/2 "2017-07-05T22:10:48Z")

</div>


