-
Notifications
You must be signed in to change notification settings - Fork 25.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
5.0.0-rc Painless support for working with dates #21121
Comments
I'll try to update (https://www.elastic.co/guide/en/elasticsearch/reference/5.0/docs-update.html) with a date example once this is figured out :) |
Elastic reserves GitHub for bug reports and feature requests; please post general questions on the Elastic Discourse forum. |
Considering you don't have a single painless test for dates or docs (https://github.com/elastic/elasticsearch/blob/master/modules/lang-painless/src/test/java/org/elasticsearch/painless/CastTests.java) I think this is a bug/feature report. |
I've created a post on https://discuss.elastic.co/t/comparing-dates-in-painless/63943 and renamed this title |
Looks like it's almost impossible to parse an ISO-8601 date with painless (http://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date) am I missing something? |
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
|
I'm reopening this because I think it is interesting. I mean, you have a workaround but it kind of sucks for a few reasons:
I don't know how many changes should stem from this issue but my instinct is "a couple". I do want to be clear on point number 2. Painless's list of priorities are:
The work around you have has trouble with 2 and 3 and 4. I don't know if we'll ever get 4, but we should be able to get 2 and 3. |
Yeah, I ended up having to do even more work as stated in here (https://discuss.elastic.co/t/comparing-dates-in-painless/63943). I had to create a function to parse the date into a Instant (in a try block) and return a default value. It definitely wasn't straight forward. I appreciate you reopening this. |
@nik9000 Point 1 to me is certainly the most interesting here. On point 3, I thought @rjernst made the change to Date fields to expose these things as dates? Maybe I'm misremembering, though. I think for compatibility with Groovy, it's best effort, but if something doesn't make sense or we agree it's a poor feature we should definitely not be forced to implement it. Groovy has a lot of really insecure/overly lenient features. |
The change I made was for |
Some additional feedback. I seem to be hitting more issues around this (I'm working to get to the bottom of this.).. I have a date _explain
{
"query": {
"script": {
"script": "Debug.explain(doc.date)"
}
}
} which returns
So I tried to convert it by doing both |
|
@rjernst good to know, I just also figured I could do this as well (get the utc time) |
@rjernst is that (doc['date'.date) ]available on reindex scripts? Getting a null reference when I try that outside of regular explain. |
That would be available in any script that has |
In reindex |
@nik9000 thanks!, looks like it's a string, I can do |
Hello, I also encounter a lot of painful problems when trying to work with datetime in painless... For instance, i wanted to apply a TZ to two dates and get the difference but cannot find a way to do it in painless... In groovy, i could do something like: Do you plan to improve this aspect of painless or do i need to keep using groovy if i want to do complex operations on date object ? |
I don't think a lot of these are whitelisted. I'm not sure why, but right now most of java's time API is whitelisted and joda isn't.... I expect something like this should work:
|
Thank you very much @nik9000, it works well !! |
And now I'm torn! Painless supports java's time api well because we expected to port Elasticsearch to java's time api and drop joda time at some point. But we haven't yet. Either we should push the migration through in 6.0 or fix up joda's date support in painless or both.... |
I don't think we should do that. The java 8 time api in painless works fine. |
Yeah. I'm fairly sure the right thing to do is to complete the port. For the time being things like date aggregations can work if you return millis-since-epoch from the script. |
I'm also against adding support for Joda. Dates are already challenging to use, but having two libraries would really step up the difficulty level. |
Removing Joda is an eventuality. |
I agree with not adding support for joda to painless. We should (at some stage) start working on #12829 to replace joda with java time instead |
@rjernst I wouldn't say it works fine, but it works.. It's been a PITA working with dates in elastic painless... |
@niemyjski thanks for the help. |
it seems like this issue can be closed since we have a way forward with java time... closing |
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)
The last script update returns
json
{
"error": {
"root_cause": [
{
"type": "remote_transport_exception",
"reason": "[8mJiEA3][local[1]][indices:data/write/update[s]]"
}
],
"type": "illegal_argument_exception",
"reason": "failed to execute script",
"caused_by": {
"type": "script_exception",
"reason": "runtime error",
"caused_by": {
"type": "class_cast_exception",
"reason": "Cannot apply [<] operation to types [java.lang.String] and [java.lang.String]."
},
"script_stack": [
"if (ctx._source.last_occurrence < params.maxOccurrenceDateUtc) { ",
" ^---- HERE"
],
"script": "if (ctx._source.total_occurrences == 0 || ctx._source.first_occurrence > params.minOccurrenceDateUtc) { ctx._source.first_occurrence = params.minOccurrenceDateUtc; } if (ctx._source.last_occurrence < params.maxOccurrenceDateUtc) { ctx._source.last_occurrence = params.maxOccurrenceDateUtc; } ctx._source.total_occurrences += params.count;",
"lang": "painless"
}
},
"status": 400
}
Is there anything special I need to do to cast a field (which is mapped as a date) and stored as a string to a date reliably?
The text was updated successfully, but these errors were encountered: