I'm trying to modify this painless script so I can retrieve the MAX date from the nested structure.
Pretend I have mapping like so:
{
"mappings": {
"hello": {
"properties": {
"field1": {"type" : "date", "format" : "yyyy-MM-dd HH:mm:ss"},
"field2": {"type" : "date", "format" : "yyyy-MM-dd HH:mm:ss"},
"test": { "type": "nested",
"properties": {
"field3": {"type" : "date", "format" : "yyyy-MM-dd HH:mm:ss"},
"field4": {"type": "integer"},
"test2": { "type": "nested",
"properties": {
"field5": {"type" : "date", "format" : "yyyy-MM-dd HH:mm:ss"}
}
}
}
}
}
}
}
}
What I want to do is retrieve the MAX date from test.field3
I've tried a few things like:
Date maxDate
for (int i = 0; i < params._source['test'].size(); i++) {
if (params._source['test'][i].field3 > score) {
maxDate= params._source['test'][i].field3
}
} return maxDate
Any help would be hugely helpful! I get the feeling I'm hitting a date parse problem, but maybe I'm wrong.
Here's a document if anyone is feeling game:
{
"field1" : "2018-01-02 10:00:00",
"field2" : "2018-01-04 10:00:00",
"test": [
{
"field3": "2018-01-04 10:00:00",
"field6": 1,
"test2": [{
"field4": "2018-01-04 10:00:00"
}
]
}
]
}
I would do it like this with Java 8 Streams
params._source.test.stream()
.mapToLong(f -> new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').parse(f.field3).getTime())
.max()
.getAsLong()