yes, the german instructions helped me a lot . I now have my expected result in an array:
POST log-sup/_search?size=0
{
"query": {
"match_all": {}
},
"aggs": {
"time_taken": {
"scripted_metric": {
"init_script": "state.docs = []",
"map_script": "state.docs.add(new HashMap(params['_source']))",
"combine_script": "return state.docs",
"reduce_script": """
def docs = [];
for (s in states) {
for (d in s) {
// Datum zu EpochMilli konvertieren
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime formatted = LocalDateTime.parse(d['@t'].substring(0, 23), formatter);
// EpochMill zum Array hinzufügen
docs.add(formatted.toInstant(ZoneOffset.ofTotalSeconds(0)).toEpochMilli());
}
}
// Sortieren nach kleinsten Datum
for (def run = 0; run < docs.length; run++) {
for (def i2 = 0; i2 < docs.length - 1; i2++) {
if (docs[i2] > docs[i2 + 1]) {
// Elemente tauschen
def index1 = i2; def index2 = i2 + 1;
def valueIndex1 = docs[index1];
docs[index1] = docs[index2];
docs[index2] = valueIndex1;
}
}
}
// Neues Zeitabstand Array
long[] duration = new long[docs.length];
def lastEpochMilli = 0; def index = 0;
// Zeitabstand berechnen
for (def value : docs) {
duration[index] = index == 0 ? 0 : (value - lastEpochMilli);
lastEpochMilli = value;
index++;
}
return duration;
"""
}
}
}
}
Result:
{
"took" : 14,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 52,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"time_taken" : {
"value" : [
0,
48,
0,
1,
1,
112,
1,
0,
2222,
4,
0,
1,
0,
1,
0,
1,
0,
0,
11,
547,
3,
0,
2,
1,
1938,
2,
0,
1,
0,
1,
1,
1,
1,
2,
1,
2,
2,
1,
2,
2,
1,
2,
2,
231,
2051,
1,
1,
0,
1,
1483726,
3,
0
]
}
}
}
How can I add the whole thing to the respective document now?