Accessing parsed date values in Update by Query script

Inside of the script context for an Update By Query, is there any way (other than parsing the raw strings), to access the doc values for date fields? I'm trying to write a script to handle some dates on some of our objects, and we have a number of different supported input formats, which are all handled by the field mapping on the index.

I would be happy to either access to doc value field as a string, or as an actual Object in Painless. I've tried doc['field_name'], but this returned as "Variable [doc] is not defined." I tried adding the "docvalue_fields" line in the update_by_query request, but it does not appear to help.

If the above is not possible, can someone point to a working parseBest example using a DateTimeFormatter? I see the call is supported, but my attempts to use it have failed. I used it similarly to how I would in Java:
TemporalAccessor temporalAccessor = dtf.parseBest(date, OffsetDateTime::from, LocalDateTime::from);

Update scripts only have access to the source of the document being updated, so you will need to do parsing yourself in this case.

Regarding your issues with using parseBest failing, can you elaborate on the error you receive?

I apologize, I don't have the error in front of me right now, but basically it didn't like the lambda references in the varargs.

Hi Jeff_Bolle,

Unfortunately, two compounding issues have occurred here. Currently, Painless doesn't support either varargs. To resolve this use an array in place of the varargs. However, method references cannot be stored as part of local variables, but there is another work around. If you create a method, you can return the result of a method reference and then create an array in place of the varargs argument.

Something similar to the following should work:

TemporalQuery getTemporalQuery(TemporalQuery tq) {
    return tq
}

... <other code> ...

TemporalAccessor temporalAccessor = dtf.parseBest(date, new TemporalQuery[] {getTemporalQuery(OffsetDateTime::from), getTemporalQuery(LocalDateTime::from)}));

... <other code> ...

Will this work around work for you for now?

--Jack

Jack,
Thanks, that is working perfectly.

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