# Multiple range filters

**URL:** <https://discuss.elastic.co/t/multiple-range-filters/4483>\
**Category:** Elasticsearch\
**Created:** [May 26, 2011, 9:28pm UTC](https://discuss.elastic.co/t/multiple-range-filters/4483 "2011-05-26T21:28:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinny](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@vinny](https://discuss.elastic.co/u/vinny)\
**Post date:** [May 26, 2011, 9:28pm UTC](https://discuss.elastic.co/t/multiple-range-filters/4483/1 "2011-05-26T21:28:04Z")

</div>

Hi,

I am trying to get a query similar to the one below working. The  
ranges may not necessarily be consecutive so I need to be able to  
retrieve all of the documents that satisify this requirement. Also,  
potentially I will need to add other filters so this should work with  
the "AND" filter or something equivalent.

{  
"query": {"match\_all":{} },  
"filter" : {  
"range" : {  
"file\_size" : [  
{ "from" : 100, "to" : 10000 },  
{ "from" : 1000001, "to" : 10000000 } ]  
}  
}  
}

## What I have tried is below:

1. bool filter with two must clauses with the range to search

## { "query": {"match\_all":{} }, "filter" : { "bool" : { "must" : {"range" : { "file\_size" : { "from" : 10, "to" : 10000 }}}, "must" : {"range" : { "file\_size" : { "from" : 10001, "to" : 1000000 } }}} } }

1. two separate "AND" queries in a single "FILTER"

{  
"query": {"match\_all":{} },  
"filter" : {  
"and" : [{"range" : {  
"file\_size" :  
{ "from" : 10, "to" : 10000 }}}],  
"and" : [{"range" : {  
"file\_size" :  
{ "from" : 10001, "to" : 1000000 }}}]  
}  
}

* * *

1. Combo "AND" and "OR" in a single filter element

{  
"query": {"match\_all":{} },  
"filter" : {  
"and" : [{"term" :  
{"file\_stages\_filetype\_result\_extension":"htm"}}],  
"or" : [{"range" : {  
"file\_size" :  
{ "from" : 10, "to" : 10000 }}},  
{"range" : {  
"file\_size" :  
{ "from" : 10001, "to" : 1000000 }}}]  
}

}

* * *

1. Combo "AND" and "OR" with two different filters, the result seems  
to OR both filters

{  
"query": {"match\_all":{} },  
"filter" : {  
"or" : [{"range" : {  
"file\_size" :  
{ "from" : 10, "to" : 10000 }}},  
{"range" : {  
"file\_size" :  
{ "from" : 120001, "to" : 1000000 }}}]  
} ,  
"filter" : {  
"and" : [{"term" :  
{"file\_stages\_filetype\_result\_extension":"htm"}}]  
}  
}

* * *

1. Single "AND" query with multiple "RANGE" queries on the same field

{  
"query": {"match\_all":{} },  
"filter" : {  
"and" : [{"range" : {  
"file\_size" :  
{ "from" : 10, "to" : 10000 }}},  
{"range" : {  
"file\_size" :  
{ "from" : 10001, "to" : 1000000 }}}  
],  
}  
}

Nothing seems to work for what I want it to do. Is there a workaround  
I can do to get the results I expect?

Vinny

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 27, 2011, 11:07am UTC](https://discuss.elastic.co/t/multiple-range-filters/4483/2 "2011-05-27T11:07:11Z")

</div>

Try this: [gist:995045 · GitHub](https://gist.github.com/995045).

On Friday, May 27, 2011 at 12:28 AM, vinny wrote:

> Hi,
> 
> I am trying to get a query similar to the one below working. The  
> ranges may not necessarily be consecutive so I need to be able to  
> retrieve all of the documents that satisify this requirement. Also,  
> potentially I will need to add other filters so this should work with  
> the "AND" filter or something equivalent.
> 
> {  
> "query": {"match\_all":{} },  
> "filter" : {  
> "range" : {  
> "file\_size" : [  
> { "from" : 100, "to" : 10000 },  
> { "from" : 1000001, "to" : 10000000 } ]  
> }  
> }  
> }
> 
> ## What I have tried is below:
> 
> 1. bool filter with two must clauses with the range to search
> 
> ## { "query": {"match\_all":{} }, "filter" : { "bool" : { "must" : {"range" : { "file\_size" : { "from" : 10, "to" : 10000 }}}, "must" : {"range" : { "file\_size" : { "from" : 10001, "to" : 1000000 } }}} } }
> 
> 1. two separate "AND" queries in a single "FILTER"
> 
> {  
> "query": {"match\_all":{} },  
> "filter" : {  
> "and" : [{"range" : {  
> "file\_size" :  
> { "from" : 10, "to" : 10000 }}}],  
> "and" : [{"range" : {  
> "file\_size" :  
> { "from" : 10001, "to" : 1000000 }}}]  
> }  
> }
> 
> * * *
> 
> 1. Combo "AND" and "OR" in a single filter element
> 
> {  
> "query": {"match\_all":{} },  
> "filter" : {  
> "and" : [{"term" :  
> {"file\_stages\_filetype\_result\_extension":"htm"}}],  
> "or" : [{"range" : {  
> "file\_size" :  
> { "from" : 10, "to" : 10000 }}},  
> {"range" : {  
> "file\_size" :  
> { "from" : 10001, "to" : 1000000 }}}]  
> }
> 
> }
> 
> * * *
> 
> 1. Combo "AND" and "OR" with two different filters, the result seems  
> to OR both filters
> 
> {  
> "query": {"match\_all":{} },  
> "filter" : {  
> "or" : [{"range" : {  
> "file\_size" :  
> { "from" : 10, "to" : 10000 }}},  
> {"range" : {  
> "file\_size" :  
> { "from" : 120001, "to" : 1000000 }}}]  
> } ,  
> "filter" : {  
> "and" : [{"term" :  
> {"file\_stages\_filetype\_result\_extension":"htm"}}]  
> }  
> }
> 
> * * *
> 
> 1. Single "AND" query with multiple "RANGE" queries on the same field
> 
> {  
> "query": {"match\_all":{} },  
> "filter" : {  
> "and" : [{"range" : {  
> "file\_size" :  
> { "from" : 10, "to" : 10000 }}},  
> {"range" : {  
> "file\_size" :  
> { "from" : 10001, "to" : 1000000 }}}  
> ],  
> }  
> }
> 
> Nothing seems to work for what I want it to do. Is there a workaround  
> I can do to get the results I expect?
> 
> Vinny

---

<div class="post-metadata">

**Author:** ![vinny](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@vinny](https://discuss.elastic.co/u/vinny)\
**Post date:** [May 27, 2011, 8:44pm UTC](https://discuss.elastic.co/t/multiple-range-filters/4483/3 "2011-05-27T20:44:07Z")

</div>

Hi Shay,

I tried the gist and it doesn't work, here is the output:  
{"took":3,"timed\_out":false,"\_shards":{"total":5,"successful":  
5,"failed":0},"hits":{"total":0,"max\_score":null,"hits":}}

As a sanity check I removed one of the range filters and it returns  
the correct result. It just doesn't want to work when I give it  
multiple ranges to filter by.

thanks for looking into this!

Vinny

On May 27, 7:07 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Try this:[gist:995045 · GitHub](https://gist.github.com/995045).
> 
> On Friday, May 27, 2011 at 12:28 AM, vinny wrote:
> 
> > Hi,
> 
> > I am trying to get a query similar to the one below working. The  
> > ranges may not necessarily be consecutive so I need to be able to  
> > retrieve all of the documents that satisify this requirement. Also,  
> > potentially I will need to add other filters so this should work with  
> > the "AND" filter or something equivalent.
> 
> > {  
> > "query": {"match\_all":{} },  
> > "filter" : {  
> > "range" : {  
> > "file\_size" : [  
> > { "from" : 100, "to" : 10000 },  
> > { "from" : 1000001, "to" : 10000000 } ]  
> > }  
> > }  
> > }
> 
> > ## What I have tried is below:
> > 
> > 1. bool filter with two must clauses with the range to search
> 
> > ## { "query": {"match\_all":{} }, "filter" : { "bool" : { "must" : {"range" : { "file\_size" : { "from" : 10, "to" : 10000 }}}, "must" : {"range" : { "file\_size" : { "from" : 10001, "to" : 1000000 } }}} } }
> > 
> > 1. two separate "AND" queries in a single "FILTER"
> 
> > {  
> > "query": {"match\_all":{} },  
> > "filter" : {  
> > "and" : [{"range" : {  
> > "file\_size" :  
> > { "from" : 10, "to" : 10000 }}}],  
> > "and" : [{"range" : {  
> > "file\_size" :  
> > { "from" : 10001, "to" : 1000000 }}}]  
> > }  
> > }
> 
> > * * *
> > 
> > 1. Combo "AND" and "OR" in a single filter element
> 
> > {  
> > "query": {"match\_all":{} },  
> > "filter" : {  
> > "and" : [{"term" :  
> > {"file\_stages\_filetype\_result\_extension":"htm"}}],  
> > "or" : [{"range" : {  
> > "file\_size" :  
> > { "from" : 10, "to" : 10000 }}},  
> > {"range" : {  
> > "file\_size" :  
> > { "from" : 10001, "to" : 1000000 }}}]  
> > }
> 
> > }
> 
> > * * *
> > 
> > 1. Combo "AND" and "OR" with two different filters, the result seems  
> > to OR both filters
> 
> > {  
> > "query": {"match\_all":{} },  
> > "filter" : {  
> > "or" : [{"range" : {  
> > "file\_size" :  
> > { "from" : 10, "to" : 10000 }}},  
> > {"range" : {  
> > "file\_size" :  
> > { "from" : 120001, "to" : 1000000 }}}]  
> > } ,  
> > "filter" : {  
> > "and" : [{"term" :  
> > {"file\_stages\_filetype\_result\_extension":"htm"}}]  
> > }  
> > }
> 
> > * * *
> > 
> > 1. Single "AND" query with multiple "RANGE" queries on the same field
> 
> > {  
> > "query": {"match\_all":{} },  
> > "filter" : {  
> > "and" : [{"range" : {  
> > "file\_size" :  
> > { "from" : 10, "to" : 10000 }}},  
> > {"range" : {  
> > "file\_size" :  
> > { "from" : 10001, "to" : 1000000 }}}  
> > ],  
> > }  
> > }
> 
> > Nothing seems to work for what I want it to do. Is there a workaround  
> > I can do to get the results I expect?
> 
> > Vinny

---

<div class="post-metadata">

**Author:** ![plaflamme](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@plaflamme](https://discuss.elastic.co/u/plaflamme)\
**Post date:** [May 27, 2011, 11:44pm UTC](https://discuss.elastic.co/t/multiple-range-filters/4483/4 "2011-05-27T23:44:37Z")

</div>

Isn't the filter saying "keep documents that have file\_size between 10 to  
10000 and file\_size between 10001 to 100001" which, unless a file has two  
different sizes will never happen?

What is it you are trying to achieve? It's not clear to me why you need to  
filter on different ranges.

Philippe

On Fri, May 27, 2011 at 16:44, vinny [agnellvj@gmail.com](mailto:agnellvj@gmail.com) wrote:

> Hi Shay,
> 
> I tried the gist and it doesn't work, here is the output:  
> {"took":3,"timed\_out":false,"\_shards":{"total":5,"successful":  
> 5,"failed":0},"hits":{"total":0,"max\_score":null,"hits":}}
> 
> As a sanity check I removed one of the range filters and it returns  
> the correct result. It just doesn't want to work when I give it  
> multiple ranges to filter by.
> 
> thanks for looking into this!
> 
> Vinny
> 
> On May 27, 7:07 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Try this:[gist:995045 · GitHub](https://gist.github.com/995045).
> > 
> > On Friday, May 27, 2011 at 12:28 AM, vinny wrote:
> > 
> > > Hi,
> > 
> > > I am trying to get a query similar to the one below working. The  
> > > ranges may not necessarily be consecutive so I need to be able to  
> > > retrieve all of the documents that satisify this requirement. Also,  
> > > potentially I will need to add other filters so this should work with  
> > > the "AND" filter or something equivalent.
> > 
> > > {  
> > > "query": {"match\_all":{} },  
> > > "filter" : {  
> > > "range" : {  
> > > "file\_size" : [  
> > > { "from" : 100, "to" : 10000 },  
> > > { "from" : 1000001, "to" : 10000000 } ]  
> > > }  
> > > }  
> > > }
> > 
> > > ## What I have tried is below:
> > > 
> > > 1. bool filter with two must clauses with the range to search
> > 
> > > ## { "query": {"match\_all":{} }, "filter" : { "bool" : { "must" : {"range" : { "file\_size" : { "from" : 10, "to" : 10000 }}}, "must" : {"range" : { "file\_size" : { "from" : 10001, "to" : 1000000 } }}} } }
> > > 
> > > 1. two separate "AND" queries in a single "FILTER"
> > 
> > > {  
> > > "query": {"match\_all":{} },  
> > > "filter" : {  
> > > "and" : [{"range" : {  
> > > "file\_size" :  
> > > { "from" : 10, "to" : 10000 }}}],  
> > > "and" : [{"range" : {  
> > > "file\_size" :  
> > > { "from" : 10001, "to" : 1000000 }}}]  
> > > }  
> > > }
> > 
> > > * * *
> > > 
> > > 1. Combo "AND" and "OR" in a single filter element
> > 
> > > {  
> > > "query": {"match\_all":{} },  
> > > "filter" : {  
> > > "and" : [{"term" :  
> > > {"file\_stages\_filetype\_result\_extension":"htm"}}],  
> > > "or" : [{"range" : {  
> > > "file\_size" :  
> > > { "from" : 10, "to" : 10000 }}},  
> > > {"range" : {  
> > > "file\_size" :  
> > > { "from" : 10001, "to" : 1000000 }}}]  
> > > }
> > 
> > > }
> > 
> > > * * *
> > > 
> > > 1. Combo "AND" and "OR" with two different filters, the result seems  
> > > to OR both filters
> > 
> > > {  
> > > "query": {"match\_all":{} },  
> > > "filter" : {  
> > > "or" : [{"range" : {  
> > > "file\_size" :  
> > > { "from" : 10, "to" : 10000 }}},  
> > > {"range" : {  
> > > "file\_size" :  
> > > { "from" : 120001, "to" : 1000000 }}}]  
> > > } ,  
> > > "filter" : {  
> > > "and" : [{"term" :  
> > > {"file\_stages\_filetype\_result\_extension":"htm"}}]  
> > > }  
> > > }
> > 
> > > * * *
> > > 
> > > 1. Single "AND" query with multiple "RANGE" queries on the same field
> > 
> > > {  
> > > "query": {"match\_all":{} },  
> > > "filter" : {  
> > > "and" : [{"range" : {  
> > > "file\_size" :  
> > > { "from" : 10, "to" : 10000 }}},  
> > > {"range" : {  
> > > "file\_size" :  
> > > { "from" : 10001, "to" : 1000000 }}}  
> > > ],  
> > > }  
> > > }
> > 
> > > Nothing seems to work for what I want it to do. Is there a workaround  
> > > I can do to get the results I expect?
> > 
> > > Vinny

---

<div class="post-metadata">

**Author:** ![vinny](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@vinny](https://discuss.elastic.co/u/vinny)\
**Post date:** [May 28, 2011, 4:36am UTC](https://discuss.elastic.co/t/multiple-range-filters/4483/5 "2011-05-28T04:36:19Z")

</div>

I can create the facet for the ranges with no problem. What I want to  
be able to do is if I select more than one facet I want to be able to  
filter on it which doesn't seem to work properly. This what you would  
do for pivoting on a data point. It doesn't necessarily have to be  
file size, it could be monetary amounts like 1 to 50 dollars and 50.01  
to 100.00 dollars. I want to be able to select the facets so that I  
filter on those two or more terms / ranges. This is how you would do  
a pivot for a facet.

On May 27, 7:44 pm, Philippe Laflamme [philippe.lafla...@obiba.org](mailto:philippe.lafla...@obiba.org)  
wrote:

> Isn't the filter saying "keep documents that have file\_size between 10 to  
> 10000 and file\_size between 10001 to 100001" which, unless a file has two  
> different sizes will never happen?
> 
> What is it you are trying to achieve? It's not clear to me why you need to  
> filter on different ranges.
> 
> Philippe

---

<div class="post-metadata">

**Author:** ![Ludovic\_Levesque](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ludovic_levesque/32/3031_2.png) [@Ludovic\_Levesque](https://discuss.elastic.co/u/Ludovic_Levesque)\
**Post date:** [May 28, 2011, 8:14am UTC](https://discuss.elastic.co/t/multiple-range-filters/4483/6 "2011-05-28T08:14:48Z")

</div>

Hi, so it's more a OR filter than a AND ?

On Sat, May 28, 2011 at 6:36 AM, vinny [agnellvj@gmail.com](mailto:agnellvj@gmail.com) wrote:

> I can create the facet for the ranges with no problem. What I want to  
> be able to do is if I select more than one facet I want to be able to  
> filter on it which doesn't seem to work properly. This what you would  
> do for pivoting on a data point. It doesn't necessarily have to be  
> file size, it could be monetary amounts like 1 to 50 dollars and 50.01  
> to 100.00 dollars. I want to be able to select the facets so that I  
> filter on those two or more terms / ranges. This is how you would do  
> a pivot for a facet.
> 
> On May 27, 7:44 pm, Philippe Laflamme [philippe.lafla...@obiba.org](mailto:philippe.lafla...@obiba.org)  
> wrote:
> 
> > Isn't the filter saying "keep documents that have file\_size between 10 to  
> > 10000 and file\_size between 10001 to 100001" which, unless a file has two  
> > different sizes will never happen?
> > 
> > What is it you are trying to achieve? It's not clear to me why you need to  
> > filter on different ranges.
> > 
> > Philippe

---

<div class="post-metadata">

**Author:** ![plaflamme](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@plaflamme](https://discuss.elastic.co/u/plaflamme)\
**Post date:** [May 28, 2011, 1:03pm UTC](https://discuss.elastic.co/t/multiple-range-filters/4483/7 "2011-05-28T13:03:21Z")

</div>

I would think that you need to combine filters of the same facet using an OR  
and filters of different facets with an AND.

(file\_size: [10, 1000[ OR file\_size: [10000, 100000[) AND (price: [0, 50[ OR  
price [50, 100[)

etc.

Here's the modified gist:

> <https://gist.github.com/plaflamme/996843>

You "should" get results with this one...

Philippe

On Sat, May 28, 2011 at 00:36, vinny [agnellvj@gmail.com](mailto:agnellvj@gmail.com) wrote:

> I can create the facet for the ranges with no problem. What I want to  
> be able to do is if I select more than one facet I want to be able to  
> filter on it which doesn't seem to work properly. This what you would  
> do for pivoting on a data point. It doesn't necessarily have to be  
> file size, it could be monetary amounts like 1 to 50 dollars and 50.01  
> to 100.00 dollars. I want to be able to select the facets so that I  
> filter on those two or more terms / ranges. This is how you would do  
> a pivot for a facet.
> 
> On May 27, 7:44 pm, Philippe Laflamme [philippe.lafla...@obiba.org](mailto:philippe.lafla...@obiba.org)  
> wrote:
> 
> > Isn't the filter saying "keep documents that have file\_size between 10 to  
> > 10000 and file\_size between 10001 to 100001" which, unless a file has two  
> > different sizes will never happen?
> > 
> > What is it you are trying to achieve? It's not clear to me why you need  
> > to  
> > filter on different ranges.
> > 
> > Philippe

---

<div class="post-metadata">

**Author:** ![fashionalwallet](https://avatars.discourse-cdn.com/v4/letter/f/839c29/32.png) [@fashionalwallet](https://discuss.elastic.co/u/fashionalwallet)\
**Post date:** [June 10, 2011, 3:43am UTC](https://discuss.elastic.co/t/multiple-range-filters/4483/8 "2011-06-10T03:43:20Z")

</div>

- deleted -

---

<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:** [July 6, 2017, 4:03am UTC](https://discuss.elastic.co/t/multiple-range-filters/4483/9 "2017-07-06T04:03:58Z")

</div>


