Script_field

I am trying to return a date from a painless script and then use that date in the query -> bool -> filter range query. But this does not seem to work.

  1. How should I access the first element of the newVal array?
  2. Is the String to Date conversion correct?
GET /index-name/_search
{
  "_source": {
    "includes": [
      "newVal"
    ]
  },
  "script_fields": {
    "newVal": {
      "script": {
        "source": """
          String lastChangeDate = '2022-08-04 18:22:29.985';
          DateTimeFormatter changedDateFormat = DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss.SSS');
          LocalDateTime changedDate = LocalDateTime.parse(lastChangeDate, changedDateFormat);
          return changedDate;
        """      
      }
    }
  },
  "query": 
  {
    "bool": 
    {
      "filter": 
      [ 
        {"range": { "newVal": {"lte": "now"}}}
      ]
    }
  }
}```

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