I have set of log documents categorized with event-ids which is string in form of timestamp.
I want to find the document with the earliest timestamp so I can always visualize the current set of logs.
This is what I have done with scripted field but not working, please I need help.
```painless
def event_latest = 0L;
for (def i = 0; i < doc['event_id'].length; i++) {
def current_date = doc['event_id'][i].getValue().toInstant().toEpochMilli();
if (current_date > event_latest)
event_latest = current_date;
}
return event_latest;
You have to call parse with the value from the doc, you can only call toInstant on an already parsed date:
def d = ZonedDateTime.parse(doc['indexer_job_id'][i].getValue());
def timestamp = d.toInstant().toEpochMilli();
But re-reading your question I'm not even sure whether the thing you want to do is possible using scripted fields. To you want to find the latest event id within the current document (indexer_job_id being an array field with multiple entries), or are there multiple documents you want to find the latest?
For the first use case this is the right approach, but it's not for the latter one. A scripted field is executed once for each document, you can't look up values within other documents inside of it.
To visualize the value of a field from the latest document in the current time range, you can use a metric visualization and the "Top Hit" aggregation. If indexer_job_id is always a date, you should specify it as date in the index mapping, then Elasticsearch will know how to handle it.
Thanks @flash1293.
Yes, it is the later, I want to find the latest event of multiple documents so I can dynamically visual the values of the latest event; ie I want visualisation that will always find the latest values of the current or latest event not manually with a specific event_id.
If not possible with scripted field, Is any approach to dynamically visualized values based on the latest event. ?
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.