Format a Scripted Date Field

Hi,

I have a scripted field using painless that is referencing from an existing field in my index. Below is the code snippet:

if(doc['market'].value == 'SAMPLE'){
    if (doc['date field].size()==0){
        return ''
    } else {
        return doc['last ship date'].value + ("45d")
    }
}

In the scripted field setting itself, I set the type as a Date and put in a Custom Format DD-MMM-YY. However when I view it on my search it still has a timestamp format and not the one I defined.

Anyone have any idea on how to resolve this?

Hi Ash. I suggest formatting the date in the Painless script like this.

if (doc['@timestamp'].size()==0){
    return ''
} else {
    ZonedDateTime input = doc['@timestamp'].value.plusDays(45);
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
    "'date:' yyyy/MM/dd 'time:' HH:mm:ss");
    String output = input.format(dtf);
    return output;
}

There's more info in the Painless Datetime API documentation.

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