# Scripted metric not working inside transform

**URL:** <https://discuss.elastic.co/t/scripted-metric-not-working-inside-transform/316061>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-machine-learning, transforms\
**Created:** [October 7, 2022, 11:01am UTC](https://discuss.elastic.co/t/scripted-metric-not-working-inside-transform/316061 "2022-10-07T11:01:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hemabh\_Ravee\_Fox](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hemabh_ravee_fox/32/111494_2.png) [@Hemabh\_Ravee\_Fox](https://discuss.elastic.co/u/Hemabh_Ravee_Fox)\
**Post date:** [October 7, 2022, 11:01am UTC](https://discuss.elastic.co/t/scripted-metric-not-working-inside-transform/316061/1 "2022-10-07T11:01:36Z")

</div>

Hi, I have an index which contains events as documents and I'm trying to aggregate them by `sessionId`. I created the following transform and so far I have been able to generate the `startTime`, `endTime`, and `sessionDuration`, along with term aggregation for the `event` field using the following JSON -

```auto
{
  "group_by": {
    "sessionId": {
      "terms": {
        "field": "sessionId"
      }
    }
  },
  "aggregations": {
    "events": {
      "terms": {
        "field": "event",
        "size": 10
      }
    },
    "endTime": {
      "max": {
        "field": "timestamp"
      }
    },
    "startTime": {
      "min": {
        "field": "timestamp"
      }
    },
    "sessionDuration": {
      "bucket_script": {
        "buckets_path": {
          "start": "startTime",
          "end": "endTime"
        },
        "script": "((params.end - params.start)/1000)"
      }
    }
  }
}

```

But here I'm only able to get term aggregation of events. But I also want to know exactly what events were fired in a session, and their order.  
So, I created the following search query to get a list of all the events in an index - which returns all the events for the entire index as a list, which is exactly what I need.

```auto
POST test30/_search?size=0
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "eventFlow": {
      "scripted_metric": {
        "init_script": "state.allEvents = [];", 
        "map_script": "state.allEvents.add(doc.event.value)",
        "combine_script": "return state.allEvents",
        "reduce_script": "List newAllEvents = new ArrayList(); for (a in states){ newAllEvents.add(a)} return newAllEvents"
      }
    }
  }
}

```

So I added the above scripted metric query to the transform like this -

```auto
{
  "group_by": {
    "sessionId": {
      "terms": {
        "field": "sessionId"
      }
    }
  },
  "aggregations": {
    "events": {
      "terms": {
        "field": "event",
        "size": 10
      }
    },
    "endTime": {
      "max": {
        "field": "timestamp"
      }
    },
    "startTime": {
      "min": {
        "field": "timestamp"
      }
    },
    "sessionDuration": {
      "bucket_script": {
        "buckets_path": {
          "start": "startTime",
          "end": "endTime"
        },
        "script": "((params.end - params.start)/1000)"
      }
    },
    "eventFlow": {
      "scripted_metric": {
        "init_script": "state.allEvents = [];",
        "map_script": "state.allEvents.add(doc.event.value)",
        "combine_script": "return state.allEvents",
        "reduce_script": "List newAllEvents = new ArrayList(); for (a in states){ newAllEvents.add(a)} return newAllEvents"
      }
    }
  }
}

```

But I don't see any new field created in the preview section of the transform. Am I missing something here?

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [October 10, 2022, 6:17am UTC](https://discuss.elastic.co/t/scripted-metric-not-working-inside-transform/316061/2 "2022-10-10T06:17:37Z")

</div>

Can you post some example documents, so I can reproduce the isse?. I don't see anything wrong, adding. One more aggregation should not break `_preview`.

Regarding your script:

> [@Hemabh\_Ravee\_Fox](#):
>
> ```auto
> "reduce_script": "List newAllEvents = new ArrayList(); for (a in states){ newAllEvents.add(a)} return newAllEvents"
> 
> ```

The for loop iterates over the shard results which are lists, By adding the list into a list, you get a list nested in your `newAllEvents` list. I guess you results contains:

```auto
    "eventFlow": {
      "value": [
        [

```

Once you have several indices or shards, the result will have several lists. I think you should use `newAllEvents.addAll(a)` in your script, to avoid the extra `[`.

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [October 11, 2022, 8:06am UTC](https://discuss.elastic.co/t/scripted-metric-not-working-inside-transform/316061/3 "2022-10-11T08:06:52Z")

</div>

I've tried it with your test data. It worked for me.

Did you use the UI or dev console? The UI has indeed a problem to show the preview.

To run a preview in dev console:

```auto
POST _transform/_preview
{
  "source": {
    "index": "YOUR_INDEX"
  },
  "pivot": {
...

```

---

<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:** [November 8, 2022, 8:07am UTC](https://discuss.elastic.co/t/scripted-metric-not-working-inside-transform/316061/4 "2022-11-08T08:07:33Z")

</div>

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