Comparing Date Fields of Nested Objects

Hi Everyone;

Here my painless script;

  "script_fields": {
    "seen": {
      "script": {
        "source": """
for (view in params['_source']['viewing_appointments']) { 
          if (new Date().getTime() - view.viewing_date.value.toInstant().toEpochMilli()) {
          return  true;
          }
        } return false;
"""
      }
    }
  }

Please note that;
mapping type of "viewing_appointments" is NESTED

Since its type is nested, I have to access inner values of it by using "_source" method instead of "doc".
However; when I use "_source" method, Elasticsearch takes values at "JSON" format
and seems type of "viewing_date" as "String"
and throws error when I try to compare it with "Date".

How can I deal with this problem.
Thanks a lot ...

Two solutions. First parse the string from the _source to a date, for example using ZonedDateTime.parse.

Second: You you use a mapping parameter to have the. field also in your root document, see Nested field type | Elasticsearch Guide [7.13] | Elastic

Out of curiousity: Why is this a nested field, if you do not store this within an array? (according to your script) Is this needed at all?