# Duplicate : Scripted metric not working inside transform

**URL:** <https://discuss.elastic.co/t/duplicate-scripted-metric-not-working-inside-transform/316269>\
**Category:** Kibana\
**Tags:** elastic-stack-machine-learning, transforms\
**Created:** [October 11, 2022, 5:20am UTC](https://discuss.elastic.co/t/duplicate-scripted-metric-not-working-inside-transform/316269 "2022-10-11T05:20:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hemabh\_Ravee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hemabh_ravee/32/104534_2.png) [@Hemabh\_Ravee](https://discuss.elastic.co/u/Hemabh_Ravee)\
**Post date:** [October 11, 2022, 5:20am UTC](https://discuss.elastic.co/t/duplicate-scripted-metric-not-working-inside-transform/316269/1 "2022-10-11T05:20:09Z")

</div>

First of all, I'd like to clarify that the reason I'm posting this question because I am unable to add any replies or edits to the [original one](https://discuss.elastic.co/t/scripted-metric-not-working-inside-transform/316061).  
I'm finding myself unable to post questions as well using that account(error - `You are not permitted to view the requested resource.`). So I resorted to using another account I created previously.

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?

here is the events data for an example session to try and replicate this issue

```auto
{'event': 'landed', 'website': 'rate.medicarefaq.com', 'elementType': '', 'elementId': '', 'path': '/ms', 'secondsElapsed': 17, 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.699236', 'eventId': '520M255', 'sessionId': '6DTD37W'}
{'elementType': 'button', 'elementId': 'get-free-quotes-now-bottom', 'event': 'click', 'website': 'rate.medicarefaq.com', 'path': '/ms', 'secondsElapsed': 10, 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.699281', 'eventId': 'R1DWJ61', 'sessionId': '6DTD37W'}
{'elementType': 'text-input', 'elementId': 'zip-code-input-top', 'event': 'text-input-entered', 'website': 'rate.medicarefaq.com', 'path': '/ms', 'secondsElapsed': 18, 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.699295', 'eventId': '1X7DN5L', 'sessionId': '6DTD37W'}
{'elementType': 'drop-down-input', 'elementId': 'country-input-bottom', 'event': 'drop-down-input-entered', 'website': 'rate.medicarefaq.com', 'path': '/ms', 'secondsElapsed': 14, 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.699306', 'eventId': 'YLG8FPX', 'sessionId': '6DTD37W'}
{'elementType': 'button', 'elementId': 'get-free-quotes-now-top', 'event': 'click', 'website': 'rate.medicarefaq.com', 'path': '/ms', 'secondsElapsed': 19, 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.699316', 'eventId': '520SIF7', 'sessionId': '6DTD37W'}
{'elementType': 'text-input', 'elementId': 'first-name-bottom', 'event': 'text-input-entered', 'website': 'rate.medicarefaq.com', 'path': '/ms', 'secondsElapsed': 13, 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.699551', 'eventId': 'OB586GD', 'sessionId': '6DTD37W'}
{'elementType': 'button', 'elementId': 'footer-compare-rates-button', 'event': 'click', 'website': 'rate.medicarefaq.com', 'path': '/ms', 'secondsElapsed': 13, 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.699586', 'eventId': 'YGP5KGT', 'sessionId': '6DTD37W'}
{'elementType': 'text-input', 'elementId': 'first-name-bottom', 'event': 'text-input-entered', 'website': 'rate.medicarefaq.com', 'path': '/ms', 'secondsElapsed': 11, 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.699600', 'eventId': '7CAJDYV', 'sessionId': '6DTD37W'}
{'elementType': 'text-input', 'elementId': 'first-name-bottom', 'event': 'text-input-entered', 'website': 'rate.medicarefaq.com', 'path': '/ms', 'secondsElapsed': 11, 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.699611', 'eventId': 'LJKY6EK', 'sessionId': '6DTD37W'}
{'event': 'idle', 'idleTimeInSeconds': '60', 'website': 'rate.medicarefaq.com', 'path': '/ms', 'secondsElapsed': 17, 'elementType': '', 'elementId': '', 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.701535', 'eventId': 'A7X1FIQ', 'sessionId': '6DTD37W'}
{'elementType': 'drop-down-input', 'elementId': 'country-input-bottom', 'event': 'drop-down-input-entered', 'website': 'rate.medicarefaq.com', 'path': '/ms', 'secondsElapsed': 16, 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.701606', 'eventId': 'YBCFAPT', 'sessionId': '6DTD37W'}
{'elementType': 'text-input', 'elementId': 'first-name-bottom', 'event': 'text-input-entered', 'website': 'rate.medicarefaq.com', 'path': '/ms', 'secondsElapsed': 19, 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.701623', 'eventId': '5LCJ6JT', 'sessionId': '6DTD37W'}
{'event': 'exited', 'website': 'rate.medicarefaq.com', 'path': '/ms', 'secondsElapsed': 11, 'elementType': '', 'elementId': '', 'userId': 'Y1VTSL6', 'timestamp': '2022-10-09T16:23:28.701635', 'eventId': 'WG1YGHK', 'sessionId': '6DTD37W'}

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 11, 2022, 5:30am UTC](https://discuss.elastic.co/t/duplicate-scripted-metric-not-working-inside-transform/316269/2 "2022-10-11T05:30:26Z")

</div>

> [@Hemabh\_Ravee](#):
>
> First of all, I'd like to clarify that the reason I'm posting this question because I am unable to add any replies or edits to the [original one](https://discuss.elastic.co/t/scripted-metric-not-working-inside-transform/316061).

There's no reason that should be, are you getting an error or something?

---

<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 11, 2022, 5:42am UTC](https://discuss.elastic.co/t/duplicate-scripted-metric-not-working-inside-transform/316269/3 "2022-10-11T05:42:04Z")

</div>

No, i'm just not able to see the edit button for it. I think it may an issue with the question in particular since I can't reply to it even from my second account - the reply button just isn't there.  
The only error I got was when I tried posting another question from that account - ` You are not permitted to view the requested resource.`

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 11, 2022, 5:49am UTC](https://discuss.elastic.co/t/duplicate-scripted-metric-not-working-inside-transform/316269/4 "2022-10-11T05:49:40Z")

</div>

I am chasing down why this is happening.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 11, 2022, 6:40am UTC](https://discuss.elastic.co/t/duplicate-scripted-metric-not-working-inside-transform/316269/5 "2022-10-11T06:40:23Z")

</div>

Ok it's been fixed, so will close this 🙂

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 11, 2022, 6:40am UTC](https://discuss.elastic.co/t/duplicate-scripted-metric-not-working-inside-transform/316269/6 "2022-10-11T06:40:26Z")

</div>


