Painless script "Cannot cast from [boolean] to [java.lang.String]" issue

Having trouble spotting the error in my script.

"reason": "class_cast_exception: Cannot cast from [boolean] to [java.lang.String]."

The Scripted field:

Instant Currentdate = Instant.ofEpochMilli(new Date().getTime());
long Fakedate = 956851031000L;
Instant Duedate = doc['duedate'].empty ? Instant.ofEpochMilli(Fakedate) : Instant.ofEpochMilli(doc['duedate'].value.getMillis());
Instant removed_date = doc['removed_date'].empty ? Instant.ofEpochMilli(Fakedate) : Instant.ofEpochMilli(doc['removed_date'].value.getMillis());

if (doc['duedate'].value == null || !doc.containsKey('duedate') || doc['duedate'].empty) { 
    return false;
    
} else if (Currentdate.isAfter(Duedate) && doc['Status'].value.contains('Open')){
    return true;
    
} else if (removed_date.isAfter(Duedate) && doc['Status'].value.contains('Closed')){
    return true;
    
} else {
    return false;
    
}

Here is the response:

{
  "took": 108,
  "timed_out": false,
  "terminated_early": false,
  "num_reduce_phases": 0,
  "_shards": {
    "total": 6,
    "successful": 0,
    "skipped": 0,
    "failed": 4,
    "failures": [
      {
        "shard": 0,
        "index": "",
        "node": "",
        "reason": {
          "type": "script_exception",
          "reason": "compile error",
          "script_stack": [
            "... '].empty) { \r\n    return false;\r\n    \r\n} else if ( ...",
            "                             ^---- HERE"
          ],
          "script": "Instant Currentdate = Instant.ofEpochMilli(new Date().getTime());\r\nlong Fakedate = 956851031000L;\r\nInstant Duedate = doc['duedate'].empty ? Instant.ofEpochMilli(Fakedate) : Instant.ofEpochMilli(doc['duedate'].value.getMillis());\r\nInstant removed_date = doc['removed_date'].empty ? Instant.ofEpochMilli(Fakedate) : Instant.ofEpochMilli(doc['removed_date'].value.getMillis());\r\n\r\nif (!doc.containsKey('duedate') || doc['duedate'].empty) { \r\n    return false;\r\n    \r\n} else if (Currentdate.isAfter(Duedate) && doc['Status'].value.contains('Open')){\r\n    return true;\r\n    \r\n} else if (removed_date.isAfter(Duedate) && doc['Status'].value.contains('Closed')){\r\n    return true;\r\n    \r\n} else {\r\n    return false;\r\n    \r\n}",
          "lang": "painless",
          "position": {
            "offset": 450,
            "start": 425,
            "end": 475
          },
          "caused_by": {
            "type": "class_cast_exception",
            "reason": "class_cast_exception: Cannot cast from [boolean] to [java.lang.String]."
          }
        }
      },
      {
        "shard": 0,
        "index": "",
        "node": "",
        "reason": {
          "type": "script_exception",
          "reason": "compile error",
          "script_stack": [
            "... '].empty) { \r\n    return false;\r\n    \r\n} else if ( ...",
            "                             ^---- HERE"
          ],
          "script": "Instant Currentdate = Instant.ofEpochMilli(new Date().getTime());\r\nlong Fakedate = 956851031000L;\r\nInstant Duedate = doc['duedate'].empty ? Instant.ofEpochMilli(Fakedate) : Instant.ofEpochMilli(doc['duedate'].value.getMillis());\r\nInstant removed_date = doc['removed_date'].empty ? Instant.ofEpochMilli(Fakedate) : Instant.ofEpochMilli(doc['removed_date'].value.getMillis());\r\n\r\nif (!doc.containsKey('duedate') || doc['duedate'].empty) { \r\n    return false;\r\n    \r\n} else if (Currentdate.isAfter(Duedate) && doc['Status'].value.contains('Open')){\r\n    return true;\r\n    \r\n} else if (removed_date.isAfter(Duedate) && doc['Status'].value.contains('Closed')){\r\n    return true;\r\n    \r\n} else {\r\n    return false;\r\n    \r\n}",
          "lang": "painless",
          "position": {
            "offset": 450,
            "start": 425,
            "end": 475
          },
          "caused_by": {
            "type": "class_cast_exception",
            "reason": "class_cast_exception: Cannot cast from [boolean] to [java.lang.String]."
          }
        }
      }
    ]
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

Hi and welcome to our community
So you are using a scripted field and not a runtime field, right (second option is the recommended way btw). Did you set the type of your field to boolean?

Thx & best,
Matthias

yes, the type is boolean. what's the difference between the two?

Yes it is, see below:

Any other ideas? Version is 7.9.3.

Also, This is done for a specific visual in a kibana dashboard, how would you accomplish this with the least impact to performance?

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