# Range query inside another range query in ElasticSearch

**URL:** <https://discuss.elastic.co/t/range-query-inside-another-range-query-in-elasticsearch/86981>\
**Category:** Elasticsearch\
**Created:** [May 24, 2017, 1:17pm UTC](https://discuss.elastic.co/t/range-query-inside-another-range-query-in-elasticsearch/86981 "2017-05-24T13:17:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![raj.shah1593](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@raj.shah1593](https://discuss.elastic.co/u/raj.shah1593)\
**Post date:** [May 24, 2017, 1:17pm UTC](https://discuss.elastic.co/t/range-query-inside-another-range-query-in-elasticsearch/86981/1 "2017-05-24T13:17:09Z")

</div>

Hello,

I have two different range queries & I want to apply one range query within another range query.  
But ES does not allow should query to be used in a range query.

Is there any other way to perform this query?  
I am trying to replicate SQL's sub-query logic.

I have tried to use "MUST" for two different range queries but my range query contains lot of search ranges and the overall AND combination is very time-intensive.

The basic algorithm will be like:

```
 **Range Query 1**
{
         "SHOULD":
          {
            from: 10,
            to: 20,
                 AND
            _Range Query 2_
            {
                from:0,
                to:5
            }
          },
          {
            from: 30,
            to: 40,
               AND
            _Range Query 2_
            {
                from:0,
                to:5
            }
          }
    }

```

Regards,  
Raj Shah

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [May 26, 2017, 9:15am UTC](https://discuss.elastic.co/t/range-query-inside-another-range-query-in-elasticsearch/86981/2 "2017-05-26T09:15:16Z")

</div>

I'm assuming your `_Range Query 2_` is on a different field? You're heading down the right path, eg:

```auto
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "filter": [
              {
                "range": {
                  "foo": {
                    "gte": 10,
                    "lt": 20
                  }
                }
              },
              {
                "range": {
                  "bar": {
                    "gte": 0,
                    "lt": 5
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "filter": [
              {
                "range": {
                  "foo": {
                    "gte": 30,
                    "lt": 40
                  }
                }
              },
              {
                "range": {
                  "bar": {
                    "gte": 0,
                    "lt": 5
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

I'm also assuming that the second clause of 0..5 isn't going to be the same for every combination, otherwise you could arrange the logic differently, eg:

```auto
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "bar": {
            "gte": 0,
            "lt": 5
          }
        }
      },
      "should": [
        {
          "range": {
            "foo": {
              "gte": 10,
              "lt": 20
            }
          }
        },
        {
          "range": {
            "foo": {
              "gte": 30,
              "lt": 40
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![raj.shah1593](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@raj.shah1593](https://discuss.elastic.co/u/raj.shah1593)\
**Post date:** [May 26, 2017, 10:05am UTC](https://discuss.elastic.co/t/range-query-inside-another-range-query-in-elasticsearch/86981/3 "2017-05-26T10:05:01Z")

</div>

Hi @Clinton_Gormley,

Thank you for your response.

You are correct. RANGE\_QUERY\_2 is on a different field and the second clause(0...5) will also be different for every combination.  
So, there will be NxN combinations which makes the query too long.  
Is there any other way to optimize the above query from performance perspective?

And how will Elastic Search analyze the above query after applying filter context among two range queries?  
Because as far as I know, if there is a query context that is executed first, then filter is applied on the results of the query context.

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [May 26, 2017, 10:39am UTC](https://discuss.elastic.co/t/range-query-inside-another-range-query-in-elasticsearch/86981/4 "2017-05-26T10:39:24Z")

</div>

See [https://www.elastic.co/blog/elasticsearch-query-execution-order](https://www.elastic.co/blog/elasticsearch-query-execution-order)

---

<div class="post-metadata">

**Author:** ![raj.shah1593](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@raj.shah1593](https://discuss.elastic.co/u/raj.shah1593)\
**Post date:** [May 26, 2017, 11:49am UTC](https://discuss.elastic.co/t/range-query-inside-another-range-query-in-elasticsearch/86981/5 "2017-05-26T11:49:21Z")

</div>

Hi @Clinton_Gormley,

Thanks for the link. That clears up a lot of doubts 🙂

Regards,  
Raj Shah

---

<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:** [June 23, 2017, 11:49am UTC](https://discuss.elastic.co/t/range-query-inside-another-range-query-in-elasticsearch/86981/6 "2017-06-23T11:49:37Z")

</div>

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