# Multiple range filters for same field

**URL:** <https://discuss.elastic.co/t/multiple-range-filters-for-same-field/73898>\
**Category:** Elasticsearch\
**Created:** [February 3, 2017, 6:10pm UTC](https://discuss.elastic.co/t/multiple-range-filters-for-same-field/73898 "2017-02-03T18:10:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tillkruss](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tillkruss/32/15193_2.png) [@tillkruss](https://discuss.elastic.co/u/tillkruss)\
**Post date:** [February 3, 2017, 6:10pm UTC](https://discuss.elastic.co/t/multiple-range-filters-for-same-field/73898/1 "2017-02-03T18:10:54Z")

</div>

Hey!

We're currently filtering our search with `terms` and `range` filters. It seems like filters are combined using `AND` which is what we need.

```
{
    body: {
        bool: {
            must: {
                query_string: {
                    query: '...'
                }
            },            
            filter: [
                { terms: { 'foobar': ['foo', 'bar'] } },
                { range: { 'fubaz': { "gte": 100, "lte": 200 } } },
            ]
        }
    }
}

```

Now we're wondering how we could add two ranges for the same field. For example:

- `foobar` is `foo` **or** `bar`
- AND
- `fubaz` is `(gte: 10, lte: 20)` **or** `(gte: 50, lte: 75)`

How would that look? Any links/tips appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [February 3, 2017, 6:25pm UTC](https://discuss.elastic.co/t/multiple-range-filters-for-same-field/73898/2 "2017-02-03T18:25:06Z")

</div>

Yep, totally doable! You have to start combining multiple boolean queries to do it. So it'd be something like:

```json
{
    body: {
        bool: {
            must: {
                query_string: {
                    query: '...'
                }
            },            
            filter: [
              { terms: { 'foobar': ['foo', 'bar'] } },
              {
                bool: {
                  should: [
                    { range: { 'fubaz': { "gte": 10, "lte": 20 } } },
                    { range: { 'fubaz': { "gte": 50, "lte": 75 } } },
                  ]
                }
              }
            ]
        }
    }
}

```

Everything in a `filter` is required, so that takes care of the AND between the terms and the ranges. A `terms` filter is naturally an OR, so that handles that. And for the ranges, we setup a boolean with a `should` containing each range, which satisfy the OR there.

It gets a bit complicated with deep boolean logic due to the nesting, but not too terrible once you get used to it 🙂 You can keep embedding these boolean queries as much as you want/need to express the appropriate boolean logic.

---

<div class="post-metadata">

**Author:** ![tillkruss](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tillkruss/32/15193_2.png) [@tillkruss](https://discuss.elastic.co/u/tillkruss)\
**Post date:** [February 3, 2017, 9:13pm UTC](https://discuss.elastic.co/t/multiple-range-filters-for-same-field/73898/3 "2017-02-03T21:13:24Z")

</div>

Awesome, that did the trick. Thanks a lot, Zachary!

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [February 3, 2017, 9:37pm UTC](https://discuss.elastic.co/t/multiple-range-filters-for-same-field/73898/4 "2017-02-03T21:37:20Z")

</div>

Great! Happy to help 🙂

---

<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:** [March 3, 2017, 9:37pm UTC](https://discuss.elastic.co/t/multiple-range-filters-for-same-field/73898/5 "2017-03-03T21:37:39Z")

</div>

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