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 -
{
"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.
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 -
{
"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?