# Script\_field

**URL:** <https://discuss.elastic.co/t/script-field/340660>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [August 11, 2023, 5:21pm UTC](https://discuss.elastic.co/t/script-field/340660 "2023-08-11T17:21:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![poonamd](https://avatars.discourse-cdn.com/v4/letter/p/58f4c7/32.png) [@poonamd](https://discuss.elastic.co/u/poonamd)\
**Post date:** [August 11, 2023, 5:21pm UTC](https://discuss.elastic.co/t/script-field/340660/1 "2023-08-11T17:21:16Z")

</div>

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?

````auto
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"}}}
      ]
    }
  }
}```
````

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 8, 2023, 5:21pm UTC](https://discuss.elastic.co/t/script-field/340660/2 "2023-09-08T17:21:58Z")

</div>

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