dao
(olivier hodac)
February 5, 2020, 7:52am
1
Dear all,
I am trying to create a field in my index patter that will hold the date only. I was thinking about script fields.
I create a script field like this, as mentioned in https://www.elastic.co/guide/en/elasticsearch/painless/7.5/painless-datetime.html
String datetime = '1983-10-13T22:15:30Z';
ZonedDateTime zdt = ZonedDateTime.parse(datetime);
String ret = zdt.format(DateTimeFormatter.ISO_INSTANT);
return ret;
Works fine:
Now replacing the constant by the @timestamp :
String datetime = doc['@timestamp'].value;
ZonedDateTime zdt = ZonedDateTime.parse(datetime);
String ret = zdt.format(DateTimeFormatter.ISO_INSTANT);
return ret;
seems to fail:
What's wrong?
ropc
(Romain Chanu)
February 5, 2020, 8:35am
2
Hi @dao - What is the type of @timestamp
in your index?
dao
(olivier hodac)
February 5, 2020, 8:53am
3
Yes there are documents. Here is the same preview with @timestamp as additional field, and a very simple script that works
ropc
(Romain Chanu)
February 5, 2020, 8:57am
4
thanks @dao , can i confirm with you the type of @timestamp
? is a date
or string
type?
ropc
(Romain Chanu)
February 5, 2020, 9:34am
6
If it is a date
type, I would expect some exceptions/errors related to type conversion / cast.
Using this script in my environment:
String datetime = doc['@timestamp'].value;
ZonedDateTime zdt = ZonedDateTime.parse(datetime);
String ret = zdt.format(DateTimeFormatter.ISO_INSTANT);
return ret;
throws a class_cast_exception
with the following reason Cannot cast org.elasticsearch.script.JodaCompatibleZonedDateTime to java.lang.String
You can refer to our documentation that gives example on how to use a Datetime Input From an Indexed Document .
For example:
ZonedDateTime input = doc['@timestamp'].value;
String output = input.format(DateTimeFormatter.ISO_INSTANT);
return output;
I hope that helps.
dao
(olivier hodac)
February 5, 2020, 2:16pm
7
Works fine, thank's
Did not find the that @timestamp is a ZonedDateTime in the documentation
system
(system)
Closed
March 4, 2020, 2:16pm
8
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.