Weekyear_week as scripted field

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()

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.

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;
}

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.

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

Any suggestions?

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