Aggregating transaction data for payments

I was able to put together a solution, based on your notes:

Below are the codeblocks:

transform:

"aggs": {
  "status_history": {
    "init_script": "state.status_history = new HashMap();",
    "map_script": """
        def c_date = doc['@timestamp'].getValue().toString();
        def c_status = doc['status.keyword'].getValue();
        state.status_history.put(c_date, c_status);
    """,
    "combine_script": "return state",
    "reduce_script": "return states[0].status_history"
  }
}

Runtime mapping script source:

def min_time = '2021-09-08T14:10:01';
def max_time = '2021-09-08T14:30:01';
def myHash = new HashMap(params['_source']['status_history']);

def max_key = '';
def max_value = 'empty';

for (entry in myHash.entrySet()) {
     def key = entry.getKey();
     def value = entry.getValue();
     int i = min_time.compareTo(key);
     int j = max_time.compareTo(key);
     if (i < 0 && j > 0 && key.compareTo(max_key) > 0) {
         max_key = key;
         max_value = value;
     }
}

emit(max_value);
2 Likes