# How can I set the range against an existing fields in the same document?

**URL:** <https://discuss.elastic.co/t/how-can-i-set-the-range-against-an-existing-fields-in-the-same-document/212410>\
**Category:** Elasticsearch\
**Created:** [December 18, 2019, 9:59pm UTC](https://discuss.elastic.co/t/how-can-i-set-the-range-against-an-existing-fields-in-the-same-document/212410 "2019-12-18T21:59:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![EliuFlorez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliuflorez/32/40884_2.png) [@EliuFlorez](https://discuss.elastic.co/u/EliuFlorez)\
**Post date:** [December 18, 2019, 9:59pm UTC](https://discuss.elastic.co/t/how-can-i-set-the-range-against-an-existing-fields-in-the-same-document/212410/1 "2019-12-18T21:59:28Z")

</div>

How can I set the range against an existing fields in the same document?

```auto
GET my_index/_search
{
    "query": {
        "range" : {
            "date" : {
                "gte" : "now-[period]d/d",
                "lte" : "now/d"
            }
        }
    }
}

```

---

<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:** [December 19, 2019, 10:23am UTC](https://discuss.elastic.co/t/how-can-i-set-the-range-against-an-existing-fields-in-the-same-document/212410/2 "2019-12-19T10:23:54Z")

</div>

Are you trying to filter by a duration by comparing start and end times within a document? If so, maybe updating those documents using update by query and then executing a fast query would work as well?

A very slow alternative could a [script\_query](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-dsl-script-query.html) to filter out documents with a duration or a [script\_field](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-request-body.html#request-body-search-script-fields) to display such, if that is what you are after.

---

<div class="post-metadata">

**Author:** ![EliuFlorez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliuflorez/32/40884_2.png) [@EliuFlorez](https://discuss.elastic.co/u/EliuFlorez)\
**Post date:** [December 19, 2019, 2:17pm UTC](https://discuss.elastic.co/t/how-can-i-set-the-range-against-an-existing-fields-in-the-same-document/212410/3 "2019-12-19T14:17:38Z")

</div>

Hello, the idea is to perform the same SQL query but in the ES conversion. where ' **period**' is the field that contains the number of days to calculate. So my question is how can I set the fields within it, that is, evaluate the field of the same document in a Range query.

**Example SQL**

```auto
(UNIX_TIMESTAMP(DATE_ADD(`date`, INTERVAL `period` DAY)) > UNIX_TIMESTAMP(NOW())

```

**Example ES**

```auto
GET my_index/_search
{
    "query": {
        "range" : {
            "date" : {
                "gte" : "now-[period]d/d",
                "lte" : "now/d"
            }
        }
    }
}

```

---

<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:** [December 20, 2019, 9:03am UTC](https://discuss.elastic.co/t/how-can-i-set-the-range-against-an-existing-fields-in-the-same-document/212410/4 "2019-12-20T09:03:40Z")

</div>

See [https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-dsl-range-query.html#ranges-on-dates](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-dsl-range-query.html#ranges-on-dates) - which is specifying a period and uses rounding.

---

<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:** [January 17, 2020, 9:03am UTC](https://discuss.elastic.co/t/how-can-i-set-the-range-against-an-existing-fields-in-the-same-document/212410/5 "2020-01-17T09:03:43Z")

</div>

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