# Bucket script aggregation error

**URL:** https://discuss.elastic.co/t/bucket-script-aggregation-error/114106
**Category:** Elasticsearch
**Created:** [January 4, 2018, 2:09pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-error/114106 "2018-01-04T14:09:56Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Frank\_Matera](https://avatars.discourse-cdn.com/v4/letter/f/ba9def/32.png) [@Frank\_Matera](https://discuss.elastic.co/u/Frank_Matera)
#### Post date: [January 4, 2018, 2:09pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-error/114106/1 "2018-01-04T14:09:57Z")

</div>

I am trying to create a bucket script based off of 2 single metric buckets and am having no luck.

My initial bucket query works:

```
{
  "query": {
    "bool": {
      "must": [
        { "match": { "call_ty": "10"} },
        { "match": { "otg.keyword": "agg-0002363334"} }
      ]
    }
  },
  "size": 0,
  "aggs": {
    "Attempts": {
      "value_count": { "field": "dur"}
    },
    "Completed":{
        "filter": {
          "range": {
          "dur": {
             "from" : 1 
          }
      }
    }
  }
}
}`Preformatted text`

```

Returning:

```
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 10,
    "successful": 10,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 190,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "Attempts": {
      "value": 190
    },
    "Completed": {
      "doc_count": 23
    }
  }
}

```

When I try to add the bucket script I get an error:

```
{
  "query": {
    "bool": {
      "must": [
        { "match": { "call_ty": "10"} },
        { "match": { "otg.keyword": "agg-0002363334"} }
      ]
    }
  },
  "size": 0,
  "aggs": {
    "attempts": {
      "value_count": { "field": "dur"}
    },
    "completed":{
        "filter": {
          "range": {
          "dur": {
             "from" : 1 
          }
      }
    }
    },
  "ASR": {
    "bucket_script": {
      "buckets_path": {
        "att": "attempts",
        "com": "completed>_count"
      },
      "script": "com / att*100"
    }
  }
}
}

```

Error:

```
{
  "error": {
    "root_cause": [
      {
        "type": "aggregation_execution_exception",
        "reason": "Invalid pipeline aggregation named [ASR] of type [bucket_script]. Only sibling pipeline aggregations are allowed at the top level"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "cdrs2",
        "node": "7407ULsCQkWlIrFxhis8tg",
        "reason": {
          "type": "aggregation_execution_exception",
          "reason": "Invalid pipeline aggregation named [ASR] of type [bucket_script]. Only sibling pipeline aggregations are allowed at the top level"
        }
      }
    ]
  },
  "status": 500
}

```

Any assistance with this would be greatly appreciated. Thanks!

---

<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: [January 5, 2018, 9:53am UTC](https://discuss.elastic.co/t/bucket-script-aggregation-error/114106/2 "2018-01-05T09:53:52Z")

</div>

The `buckets_path` aggregation is a parent pipeline aggregation, which means it needs to be nested inside another aggregation. You can not provide it in the top level of your aggregation tree. Not only that, the parent aggregation needs to be a multi-bucket aggregation too.

Here's workaround (some would call it an ugly hack 😉) to get to what you need: nest your aggs inside a `filters` aggregation that contains just a single `match_all` filter:

```
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "call_ty": "10"
          }
        },
        {
          "match": {
            "otg.keyword": "agg-0002363334"
          }
        }
      ]
    }
  },
  "size": 0,
  "aggs": {
    "all_matching_docs": {
      "filters": {
        "filters": {
          "all": {
            "match_all": {}
          }
        }
      },
      "aggs": {
        "attempts": {
          "value_count": {
            "field": "dur"
          }
        },
        "completed": {
          "filter": {
            "range": {
              "dur": {
                "from": 1
              }
            }
          }
        },
        "ASR": {
          "bucket_script": {
            "buckets_path": {
              "att": "attempts",
              "com": "completed>_count"
            },
            "script": "com / att*100"
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

### Author: ![Frank\_Matera](https://avatars.discourse-cdn.com/v4/letter/f/ba9def/32.png) [@Frank\_Matera](https://discuss.elastic.co/u/Frank_Matera)
#### Post date: [January 5, 2018, 12:49pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-error/114106/3 "2018-01-05T12:49:05Z")

</div>

@abdon Thank you for your reply and explanation. When I run this, I am receiving an error that "com" is not defined.

```
{
  "error": {
    "root_cause": [],
    "type": "search_phase_execution_exception",
    "reason": "",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "script_exception",
      "reason": "compile error",
      "script_stack": [
        "com / att*100",
        "^---- HERE"
      ],
      "script": "com / att*100",
      "lang": "painless",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Variable [com] is not defined."
      }
    }
  },
  "status": 503
}

```

Using it without the bucket script, returns the correct results:

```
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 190,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "all_matching_docs": {
      "buckets": {
        "all": {
          "doc_count": 190,
          "completed": {
            "doc_count": 23
          },
          "attempts": {
            "value": 190
          }
        }
      }
    }
  }
}

```

Thanks in advance.

---

<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: [January 5, 2018, 12:52pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-error/114106/4 "2018-01-05T12:52:03Z")

</div>

Your script uses the old syntax for parameters. Try this as the script instead:

`"script": "params.com / params.att*100"`

---

<div class="post-metadata">

### Author: ![Frank\_Matera](https://avatars.discourse-cdn.com/v4/letter/f/ba9def/32.png) [@Frank\_Matera](https://discuss.elastic.co/u/Frank_Matera)
#### Post date: [January 5, 2018, 12:54pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-error/114106/5 "2018-01-05T12:54:30Z")

</div>

Just entered that and it worked. Thanks again for your help!

---

<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: [February 2, 2018, 12:54pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-error/114106/6 "2018-02-02T12:54:45Z")

</div>

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