# Can I use a Range Query in Scripted Field

**URL:** <https://discuss.elastic.co/t/can-i-use-a-range-query-in-scripted-field/106047>\
**Category:** Elasticsearch\
**Created:** [November 1, 2017, 2:41pm UTC](https://discuss.elastic.co/t/can-i-use-a-range-query-in-scripted-field/106047 "2017-11-01T14:41:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![crickes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/crickes/32/18009_2.png) [@crickes](https://discuss.elastic.co/u/crickes)\
**Post date:** [November 1, 2017, 2:41pm UTC](https://discuss.elastic.co/t/can-i-use-a-range-query-in-scripted-field/106047/1 "2017-11-01T14:41:39Z")

</div>

I am have been playing around the range field types, and in this index I have approx 2500 documents, each defining a different time\_range.  
Example document:

```
{
          "time_frame": {
            "gte": 1509355523000,
            "lte": 1509355718871
          }
}

```

A time range queries like:

```
POST index-1/_search
    {
      "size": 0, 
      "query" : {
        "range" : {
          "time_frame" : { 
            "gte" : 1509355523000,
            "lte" : 1509355523999
          }
        }
      }
    }

```

Gives me how many documents intersect with the defined time range.

```
Response: {
      "took": 0,
      "timed_out": false,
      "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 23,
        "max_score": 0,
        "hits": []
      }
    }

```

What I want to achieve is having the number of intersecting documents, returned as a scripted field. In theory I should be able to achieve this by somehow embedding the range query as a scripted field, but as far as I can tell, you can only use Painless (or similar) in a scripted field. Is there a way, I can use Painless, to access the values in the document, and use them in a range query for a scripted field?

Thanks In Advance.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [November 3, 2017, 3:05pm UTC](https://discuss.elastic.co/t/can-i-use-a-range-query-in-scripted-field/106047/2 "2017-11-03T15:05:17Z")

</div>

can you explain the context with a scripted field here? I do not follow it.

I also think you should take a closer look at the range types, which have been added recently.

See [https://www.elastic.co/guide/en/elasticsearch/reference/5.6/range.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/range.html)

---

<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:** [December 1, 2017, 3:05pm UTC](https://discuss.elastic.co/t/can-i-use-a-range-query-in-scripted-field/106047/3 "2017-12-01T15:05:20Z")

</div>

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