# Is it possible for buckets\_path to reference a scripted\_metric aggregation?

**URL:** https://discuss.elastic.co/t/is-it-possible-for-buckets-path-to-reference-a-scripted-metric-aggregation/296953
**Category:** Elasticsearch
**Created:** [February 11, 2022, 9:03am UTC](https://discuss.elastic.co/t/is-it-possible-for-buckets-path-to-reference-a-scripted-metric-aggregation/296953 "2022-02-11T09:03:30Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![saurab](https://avatars.discourse-cdn.com/v4/letter/s/e274bd/32.png) [@saurab](https://discuss.elastic.co/u/saurab)
#### Post date: [February 11, 2022, 9:03am UTC](https://discuss.elastic.co/t/is-it-possible-for-buckets-path-to-reference-a-scripted-metric-aggregation/296953/1 "2022-02-11T09:03:30Z")

</div>

I am working on the kibana\_sample\_data\_logs index. I ran the following aggregation:

```auto

GET kibana_sample_data_logs/_search?size=0
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "doc_buckets_for_terms_aggregation": {
      "terms": {
        "field": "clientip"
      }, 
      "aggs": {
        "max_bytes": {
          "scripted_metric": {
            "init_script": "state.max_bytes = 0L;", 
            "map_script": """ 
              def current_bytes = doc['bytes'].getValue();
              if (current_bytes > state.max_bytes)
                {state.max_bytes = current_bytes;}
            """,
            "combine_script": "return state", 
            "reduce_script": """ 
              def max_bytes = 0L;
              for (s in states) {if (Objects.isNull(s)){max_bytes=max_bytes} else if (s.max_bytes > (max_bytes))
                {max_bytes = s.max_bytes;}}
              return max_bytes
            """
          }
        }
      }
    }, 
    "max_buckets_whole_index": {
      "max_bucket": {
        "buckets_path": "doc_buckets_for_terms_aggregation>max_bytes" 
      }
    }
  }
}

```

In this snippet above, we first bucket documents on the clientip. Next we run a scripted\_metric aggregation to calculate the max\_bytes field for each bucket (meaning for each clientip). Finally, we calculate the max\_bytes for the whole index by running the max\_bucket aggregation.

But I am getting the following error:

```auto
{
  "error" : {
    "root_cause" : [],
    "type" : "search_phase_execution_exception",
    "reason" : "",
    "phase" : "fetch",
    "grouped" : true,
    "failed_shards" : [],
    "caused_by" : {
      "type" : "aggregation_execution_exception",
      "reason" : "buckets_path must reference either a number value or a single value numeric metric aggregation, got: [InternalScriptedMetric] at aggregation [max_bytes]"
    }
  },
  "status" : 500
}

```

It seems that it is not possible for buckets\_path to reference a scripted metric.... How do I change my script so that it works ? Thanks in advance...

---

<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: [March 11, 2022, 9:04am UTC](https://discuss.elastic.co/t/is-it-possible-for-buckets-path-to-reference-a-scripted-metric-aggregation/296953/2 "2022-03-11T09:04:20Z")

</div>

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