I'm upgrading from groovy to painless and it's not so painless so far. I wanted to post this here as I think this would be a pretty common scenario and I'm having trouble with dates (gotta love dates). Ideas?

To anyone wondering I was able to get this working at least locally, I'm not sure how reliable it is at parsing different date formats as I can't specify Zulu because min date time doesn't have it (pre epoch)

Thanks for your response, I created an issue here: https://github.com/elastic/elasticsearch/issues/21121 it would be good to add a date sample / this whitelist text to the docs. I found it after looking through the source code but it was a serious pita for a non java guy working with dates (last time I worked with java was about 10 years ago).

I just looked through the classes as well. Looks like they include all the formats except that final one with the YYYY-MM-DDThh:mm:ss.sTZD. I believe it can be done with this crazy looking Java class DateTimeFormatterBuilder. (https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) I appreciate you sort of being a guinea pig here for trying out these Java Date classes. Documentation for this is definitely on my list of things to do.

I'm almost thinking I'm going to have to create a local function that returns a numeric data type and return the tick count after tying to parse with try catches as I can't get multiple formats to work.

We do not allow coercion because in most cases it doesn't make sense. To me, this is a very broken feature in Groovy. What does is mean to make a Map a boolean, empty or null? Some other arbitrary criteria? What about a String?

Not sure what you mean by empty here; does the script you provided not compile correctly? If it really doesn't then that's a bug. If you mean you can't do catch () {...} that's intentional. If you want a catch all you can use Exception, but the user must define what type of Exception to catch. Using def here doesn't really make sense in Painless.

Only way to debug right now is to throw an exception (like IllegalArgumentException). This is something we are going to fix in the future, but it's going to take some extra time.

Again, sorry for the difficultly you had with using dates, but thanks for working through it!

Like in javascript there are clear cases where !! coercion into a boolean (!! will be true if the value exists) but I agree it could be a little goofy.

I wanted a generic catch all try block. I'm not a java programmer I don't know all the exception types that will get thrown. I looked at the JavaDocs and added a try block for just the one they listed, but I know deep down there are other exceptions they will throw... I just wanted a catch all so I could fallback to my default behavior.

I wanted a generic catch all try block. I'm not a java programmer I don't know all the exception types that will get thrown. I looked at the JavaDocs and added a try block for just the one they listed, but I know deep down there are other exceptions they will throw... I just wanted a catch all so I could fallback to my default behavior.

For reference the way to do that is: try { } catch (Exception e) {}

I'm fairly torn about what you should stick in that exception handling block. It'd be great if you could log something in there but we don't support that. There is some paranoia around scripts do any IO and logging counts.

I should've have explained the Exception handling a bit more because as Nik pointed out Exception is the generic catch all for Painless. There are a few things that cannot be caught by the user such as the loop protection error when attempting to stop a runaway loop.

As for coercion, I could see us having a potentially custom operator for checking if something exists or not because it could be nice to check something against null as a shortcut. I tend to lean more towards the Java side rather than scripting side simply because less leniency leads to better performance since we can do more at compile-time.

@Jack_Conradson I'm now trying to work on a painless script (for daily index migration ) and I have a date stored as '2017-01-03T14:12:36.2770782-06:00' Is there a good way to get the utc time? I'm back to trying to parse this with out very much luck.. I've tried to convert the doc.date to a date and instant and get errors https://github.com/elastic/elasticsearch/issues/21121 converting long to java long..