Confusion between JodaCompatibleZonedDateTime and ZonedDateTime

Hi everyone!

I just tried to do some date calculatoin in painless and just don't get how I can access the functions of the JodaCompatibleZonedDateTime given by an index field.

In my index I have a field called "OXACTIVEFROM" with the following mapping:

"OXACTIVEFROM": {
  "type": "date",
  "format": "yyyy-MM-dd HH:mm:ss",
  "fields": {
    "unanalyzed": {
      "format": "yyyy-MM-dd HH:mm:ss",
      "type": "date"
    },
    "lowercase": {
      "format": "yyyy-MM-dd HH:mm:ss",
      "type": "date"
    },
    "sorting": {
      "format": "yyyy-MM-dd HH:mm:ss",
      "type": "date"
    }
  }
},

In an aggregation I use the following script, to calculate a boolean if this date is older than 60 days:

{
  "query": {...}
  "aggregations": {
    "60days": {
      "terms": {
        "script": {
          "params": {
            "now": 1581083160
          },
          "lang": "painless",
          "source": "if(!doc.containsKey('OXACTIVEFROM.unanalyzed') || doc['OXACTIVEFROM.unanalyzed'].size()==0 || doc['OXACTIVEFROM.unanalyzed'].value.getYear() == 0) return false; Instant instant = Instant.ofEpochMilli(params['now']); ZonedDateTime today = ZonedDateTime.ofInstant(instant, ZoneId.of('Z')); ZonedDateTime activefrom = doc['OXACTIVEFROM.unanalyzed'].value; return ChronoUnit.DAYS.between(activefrom, today) <= 60;"

        },
        "exclude": "false"
      },
    },
  }
}

This script just fails with

"type": "class_cast_exception",
"reason": "Cannot cast org.elasticsearch.script.JodaCompatibleZonedDateTime to java.time.ZonedDateTime""

For better readability the script again:

if(!doc.containsKey('OXACTIVEFROM.unanalyzed') || doc['OXACTIVEFROM.unanalyzed'].size()==0 || doc['OXACTIVEFROM.unanalyzed'].value.getYear() == 0) return false;
Instant instant = Instant.ofEpochMilli(params['now']);
ZonedDateTime today = ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));
ZonedDateTime activefrom = doc['OXACTIVEFROM.unanalyzed'].value;
return ChronoUnit.DAYS.between(activefrom, today) <= 60;

So it fails on the line where I get the value from the document and assign it to "activefrom"

So I googled and found the source of this class in ES: https://github.com/elastic/elasticsearch/blob/7.3/server/src/main/java/org/elasticsearch/script/JodaCompatibleZonedDateTime.java#L75

So I tried to use the "getZonedDateTime" to get the real ZoneDateTime:

ZonedDateTime activefrom = doc['OXACTIVEFROM.unanalyzed'].value.getZonedDateTime();
return ChronoUnit.DAYS.between(activefrom, today) <= 60;

But unfortunately this fails with:

"type": "illegal_argument_exception",
"reason": "dynamic method [org.elasticsearch.script.JodaCompatibleZonedDateTime, getZonedDateTime/0] not found"

Am I holding it wrong?!

Hopefully someone can enlighten me on this :smiley:

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