Copy fields from latest doc in Transforms

Hi all,

I have an analytics system and for every user action I log an event. The event contains event specific but also session level data.

I've created a transform to create a user centric entity. on the transform I want to copy several values from the latest event document to the transform document.

I've got it working with the latestDocument example:

    "latest_doc": {
        "scripted_metric": {
          "init_script": "state.timestamp_latest = 0L; state.last_doc = ''", 
          "map_script": """ 
            def current_date = doc['ts'].getValue().toInstant().toEpochMilli();
            if (current_date > state.timestamp_latest)
            {state.timestamp_latest = current_date;
            state.last_doc = new HashMap(params['_source']);}
          "combine_script": "return state", 
          "reduce_script": """ 
            def last_doc = '';
            def timestamp_latest = 0L;
            for (s in states) {if (s.timestamp_latest > (timestamp_latest))
            {timestamp_latest = s.timestamp_latest; last_doc = s.last_doc;}}
            return last_doc

What I'd like is a way to just copy some of the fields directly on the root without duplicating the scripted metric. What would be a good way to achieve it?

many thanks in advance

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