# The difference between range query and range filter?

**URL:** <https://discuss.elastic.co/t/the-difference-between-range-query-and-range-filter/7542>\
**Category:** Elasticsearch\
**Created:** [May 2, 2012, 4:39pm UTC](https://discuss.elastic.co/t/the-difference-between-range-query-and-range-filter/7542 "2012-05-02T16:39:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mp2893](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mp2893](https://discuss.elastic.co/u/mp2893)\
**Post date:** [May 2, 2012, 4:39pm UTC](https://discuss.elastic.co/t/the-difference-between-range-query-and-range-filter/7542/1 "2012-05-02T16:39:44Z")

</div>

Hi,

I am currently making a lot of queries based on text and dates.

I use bool query that includes a text query and a range query.  
Text query and range query are combine with a "must".  
For example,  
{  
"query":{  
"bool":{  
"must":[  
{"text":{"title":"my sample query"}},  
{"range":{"date":{"gte":"2012-05-01", "lte":"2012-05-05"}}}  
]  
}  
}  
}

But I found out that the same goal could be achieve by a filtered  
query with a range filter.  
For example,  
{  
"query":{  
"filtered":{  
"filter":{  
"range":{"date":{"gte":"2012-05-01", "lte":"2012-05-05"}}  
},  
"query":{  
"text":{"title":"my sample query"}  
}  
}  
}  
}

Which one is the better way to go?  
I'd appreciate any kind of advice.  
Thanks.

Ed

---

<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 2, 2012, 4:52pm UTC](https://discuss.elastic.co/t/the-difference-between-range-query-and-range-filter/7542/2 "2012-05-02T16:52:24Z")

</div>

Usually, the filtered option is better. The filtered option will cache the  
results (in an optimized manner), so if another range filter repeats with  
the same ranges, the data will already be available from the cache. The  
cache is called the filter cache, and its LRU based with automatic memory  
management (by default, 20% from the heap). Node stats / Indices stats can  
return you its utilization.

On Wed, May 2, 2012 at 7:39 PM, mp2893 [mp2893@gmail.com](mailto:mp2893@gmail.com) wrote:

> Hi,
> 
> I am currently making a lot of queries based on text and dates.
> 
> I use bool query that includes a text query and a range query.  
> Text query and range query are combine with a "must".  
> For example,  
> {  
> "query":{  
> "bool":{  
> "must":[  
> {"text":{"title":"my sample query"}},  
> {"range":{"date":{"gte":"2012-05-01", "lte":"2012-05-05"}}}  
> ]  
> }  
> }  
> }
> 
> But I found out that the same goal could be achieve by a filtered  
> query with a range filter.  
> For example,  
> {  
> "query":{  
> "filtered":{  
> "filter":{  
> "range":{"date":{"gte":"2012-05-01", "lte":"2012-05-05"}}  
> },  
> "query":{  
> "text":{"title":"my sample query"}  
> }  
> }  
> }  
> }
> 
> Which one is the better way to go?  
> I'd appreciate any kind of advice.  
> Thanks.
> 
> Ed

---

<div class="post-metadata">

**Author:** ![mp2893](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mp2893](https://discuss.elastic.co/u/mp2893)\
**Post date:** [May 3, 2012, 1:41am UTC](https://discuss.elastic.co/t/the-difference-between-range-query-and-range-filter/7542/3 "2012-05-03T01:41:08Z")

</div>

Thanks, Shay for the detailed answer.  
So, if I understood correctly, basically the only difference between the  
two types of query is whether the results are cached or not?  
Then if queries are consecutively made with totally non-overlapping date  
ranges, the two types of query will give me the same performance?

Regards,  
Ed

2012/5/3 Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com)

> Usually, the filtered option is better. The filtered option will cache the  
> results (in an optimized manner), so if another range filter repeats with  
> the same ranges, the data will already be available from the cache. The  
> cache is called the filter cache, and its LRU based with automatic memory  
> management (by default, 20% from the heap). Node stats / Indices stats can  
> return you its utilization.
> 
> On Wed, May 2, 2012 at 7:39 PM, mp2893 [mp2893@gmail.com](mailto:mp2893@gmail.com) wrote:
> 
> > Hi,
> > 
> > I am currently making a lot of queries based on text and dates.
> > 
> > I use bool query that includes a text query and a range query.  
> > Text query and range query are combine with a "must".  
> > For example,  
> > {  
> > "query":{  
> > "bool":{  
> > "must":[  
> > {"text":{"title":"my sample query"}},  
> > {"range":{"date":{"gte":"2012-05-01", "lte":"2012-05-05"}}}  
> > ]  
> > }  
> > }  
> > }
> > 
> > But I found out that the same goal could be achieve by a filtered  
> > query with a range filter.  
> > For example,  
> > {  
> > "query":{  
> > "filtered":{  
> > "filter":{  
> > "range":{"date":{"gte":"2012-05-01", "lte":"2012-05-05"}}  
> > },  
> > "query":{  
> > "text":{"title":"my sample query"}  
> > }  
> > }  
> > }  
> > }
> > 
> > Which one is the better way to go?  
> > I'd appreciate any kind of advice.  
> > Thanks.
> > 
> > Ed

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [May 3, 2012, 6:59pm UTC](https://discuss.elastic.co/t/the-difference-between-range-query-and-range-filter/7542/4 "2012-05-03T18:59:01Z")

</div>

Filters also do not contribute to the scoring of a document, whereas  
additional clauses to a query will.

--  
Ivan

On Wed, May 2, 2012 at 6:41 PM, edward choi [mp2893@gmail.com](mailto:mp2893@gmail.com) wrote:

> Thanks, Shay for the detailed answer.  
> So, if I understood correctly, basically the only difference between the two  
> types of query is whether the results are cached or not?  
> Then if queries are consecutively made with totally non-overlapping date  
> ranges, the two types of query will give me the same performance?
> 
> Regards,  
> Ed
> 
> 2012/5/3 Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com)
> 
> > Usually, the filtered option is better. The filtered option will cache the  
> > results (in an optimized manner), so if another range filter repeats with  
> > the same ranges, the data will already be available from the cache. The  
> > cache is called the filter cache, and its LRU based with automatic memory  
> > management (by default, 20% from the heap). Node stats / Indices stats can  
> > return you its utilization.
> > 
> > On Wed, May 2, 2012 at 7:39 PM, mp2893 [mp2893@gmail.com](mailto:mp2893@gmail.com) wrote:
> > 
> > > Hi,
> > > 
> > > I am currently making a lot of queries based on text and dates.
> > > 
> > > I use bool query that includes a text query and a range query.  
> > > Text query and range query are combine with a "must".  
> > > For example,  
> > > {  
> > > "query":{  
> > > "bool":{  
> > > "must":[  
> > > {"text":{"title":"my sample query"}},  
> > > {"range":{"date":{"gte":"2012-05-01", "lte":"2012-05-05"}}}  
> > > ]  
> > > }  
> > > }  
> > > }
> > > 
> > > But I found out that the same goal could be achieve by a filtered  
> > > query with a range filter.  
> > > For example,  
> > > {  
> > > "query":{  
> > > "filtered":{  
> > > "filter":{  
> > > "range":{"date":{"gte":"2012-05-01", "lte":"2012-05-05"}}  
> > > },  
> > > "query":{  
> > > "text":{"title":"my sample query"}  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > Which one is the better way to go?  
> > > I'd appreciate any kind of advice.  
> > > Thanks.
> > > 
> > > Ed

---

<div class="post-metadata">

**Author:** ![mp2893](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mp2893](https://discuss.elastic.co/u/mp2893)\
**Post date:** [May 5, 2012, 6:16am UTC](https://discuss.elastic.co/t/the-difference-between-range-query-and-range-filter/7542/5 "2012-05-05T06:16:00Z")

</div>

So for strict filtering function, I should use filters rather than combined  
queries.  
Thanks for the info Ivan.

Best,  
Ed

2012/5/4 Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com)

> Filters also do not contribute to the scoring of a document, whereas  
> additional clauses to a query will.
> 
> --  
> Ivan
> 
> On Wed, May 2, 2012 at 6:41 PM, edward choi [mp2893@gmail.com](mailto:mp2893@gmail.com) wrote:
> 
> > Thanks, Shay for the detailed answer.  
> > So, if I understood correctly, basically the only difference between the  
> > two  
> > types of query is whether the results are cached or not?  
> > Then if queries are consecutively made with totally non-overlapping date  
> > ranges, the two types of query will give me the same performance?
> > 
> > Regards,  
> > Ed
> > 
> > 2012/5/3 Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com)
> > 
> > > Usually, the filtered option is better. The filtered option will cache  
> > > the  
> > > results (in an optimized manner), so if another range filter repeats  
> > > with  
> > > the same ranges, the data will already be available from the cache. The  
> > > cache is called the filter cache, and its LRU based with automatic  
> > > memory  
> > > management (by default, 20% from the heap). Node stats / Indices stats  
> > > can  
> > > return you its utilization.
> > > 
> > > On Wed, May 2, 2012 at 7:39 PM, mp2893 [mp2893@gmail.com](mailto:mp2893@gmail.com) wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I am currently making a lot of queries based on text and dates.
> > > > 
> > > > I use bool query that includes a text query and a range query.  
> > > > Text query and range query are combine with a "must".  
> > > > For example,  
> > > > {  
> > > > "query":{  
> > > > "bool":{  
> > > > "must":[  
> > > > {"text":{"title":"my sample query"}},  
> > > > {"range":{"date":{"gte":"2012-05-01", "lte":"2012-05-05"}}}  
> > > > ]  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > But I found out that the same goal could be achieve by a filtered  
> > > > query with a range filter.  
> > > > For example,  
> > > > {  
> > > > "query":{  
> > > > "filtered":{  
> > > > "filter":{  
> > > > "range":{"date":{"gte":"2012-05-01", "lte":"2012-05-05"}}  
> > > > },  
> > > > "query":{  
> > > > "text":{"title":"my sample query"}  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > Which one is the better way to go?  
> > > > I'd appreciate any kind of advice.  
> > > > Thanks.
> > > > 
> > > > Ed

---

<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, 3:30am UTC](https://discuss.elastic.co/t/the-difference-between-range-query-and-range-filter/7542/6 "2017-07-06T03:30:01Z")

</div>


