# Painless scripted field - access query range

**URL:** <https://discuss.elastic.co/t/painless-scripted-field-access-query-range/268135>\
**Category:** Elasticsearch\
**Created:** [March 23, 2021, 7:37pm UTC](https://discuss.elastic.co/t/painless-scripted-field-access-query-range/268135 "2021-03-23T19:37:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nuno\_Centeio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nuno_centeio/32/84067_2.png) [@Nuno\_Centeio](https://discuss.elastic.co/u/Nuno_Centeio)\
**Post date:** [March 23, 2021, 7:37pm UTC](https://discuss.elastic.co/t/painless-scripted-field-access-query-range/268135/1 "2021-03-23T19:37:28Z")

</div>

Hi,

I'm new to Painless (and to Elasticsearch as well) and I'm building a query that looks like this:

```auto
  "script_fields": {
    "PeriodTime": {
      "script": {
        "source": "doc['@timestamp'].value.millis - doc['previousStateDateTime'].value.millis",
        "lang": "painless"
      }
    }
  },
  "stored_fields": [
    "*"
  ],
  "_source": {
    "excludes": []
  },
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "match_all": {}
        },
        {
          "range": {
            "@timestamp": {
              "gte": "2021-03-23T00:00:00.000Z",
              "lte": "2021-03-23T23:59:59.000Z",
              "format": "strict_date_optional_time"
            }
          }
        }
      ],
      "should": [],
      "must_not": []
    }
  }

```

I would like to receive the "gte" and "lte" values inside the source of the scripted field. Is this possible?

Thanks

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [March 26, 2021, 3:38pm UTC](https://discuss.elastic.co/t/painless-scripted-field-access-query-range/268135/2 "2021-03-26T15:38:03Z")

</div>

I don't completely understand your question. Do you want to access `gte` and `lte` values from the range query inside `script_fields` `source`? If yes, it is not possible. And why would you need that? You are forming a query yourself, you can put in the source whatever you need, including, for example direct values: "2021-03-23T00:00:00.000Z"

---

<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:** [April 23, 2021, 3:38pm UTC](https://discuss.elastic.co/t/painless-scripted-field-access-query-range/268135/3 "2021-04-23T15:38:39Z")

</div>

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