# Are ScriptedMetric aggregations compatible with Pipeline Aggregations?

**URL:** https://discuss.elastic.co/t/are-scriptedmetric-aggregations-compatible-with-pipeline-aggregations/72814
**Category:** Elasticsearch
**Created:** [January 25, 2017, 9:14pm UTC](https://discuss.elastic.co/t/are-scriptedmetric-aggregations-compatible-with-pipeline-aggregations/72814 "2017-01-25T21:14:52Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![btokarchuk](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@btokarchuk](https://discuss.elastic.co/u/btokarchuk)
#### Post date: [January 25, 2017, 9:14pm UTC](https://discuss.elastic.co/t/are-scriptedmetric-aggregations-compatible-with-pipeline-aggregations/72814/1 "2017-01-25T21:14:52Z")

</div>

I have a histogram aggregation that returns n documents per bucket that I was then computing an Average sub aggregation on and then finally doing a pipeline aggregation of all the bucket averages. This was working great, but I wanted to change it so I could compute the average on the "largest 4" values in each histogram bucket. I wrote a scripted metric that now does this wonderfully, however my pipeline aggregation now complains with the following error:

```
buckets_path must reference either a number value or a single value numeric metric aggregation, got: org.elasticsearch.search.aggregations.metrics.scripted.InternalScriptedMetric

```

Sample output from my scripted metric:

```
aggregations": {
      "by_vehicle_number": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": 26,
            "doc_count": 174,
            "samples": {
              "buckets": [
                {
                  "key": 1485254085512,
                  "doc_count": 12,
                  "avg_cno": {
                    "value": 41.666666666666664
                  }
                },
                {
                  "key": 1485254086514,
                  "doc_count": 12,
                  "avg_cno": {
                    "value": 42.5
                  }
                }]
            }
          }
        ]
      }
}
```

---

<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 22, 2017, 9:15pm UTC](https://discuss.elastic.co/t/are-scriptedmetric-aggregations-compatible-with-pipeline-aggregations/72814/2 "2017-02-22T21:15:02Z")

</div>

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