Weekyear_week as scripted field


#1

How do I create a scripted field showing timestamp formatted as 'weekyear_week'?

The weekyear_week format is described here:
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#date-math

For creating a scripted field containing dayOfWeek in my time zone (CET) I use the following in Kibana 6.

LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value.millis), ZoneId.of('CET')).getDayOfWeek()


(Joe Fleming) #2

I don't enough about Painless to actually help you out here, but looking at the API reference, it seems like you might be able to specify a custom format using the DateTImeFormatter.


(Jared Carey) #3

I get Unsupported field: OffsetSeconds" when trying to use DateTimeFormatter on LocalDateTime.

This worked ok, although - I haven't really checked if it is correct.

if (doc['@timestamp'] != null) {
    Instant instant = Instant.ofEpochMilli( doc['@timestamp'].value.millis );
    ZoneId zoneId = ZoneId.of ( "America/Los_Angeles" );
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    DateTimeFormatter formatter = builder.appendPattern("YYYY'-W'ww").toFormatter();
    String formattedDate = formatter.format(
      ZonedDateTime.ofInstant ( instant , zoneId )
    );
    return formattedDate;
}

#4

Unfortunately, the output does not comply with the local week definitions.
If time zone is set to "Europe/Copenhagen" in the suggested script then the week changes between Saturday and Sunday although by definition a new week begins on Mondays in Denmark.
https://savvytime.com/current-week/denmark

How should the script be adjusted to end up with a week definition valid in the time zone used in the scripted field?


#5

Any suggestions?


(system) #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.