# Filtered query vs query w/ filter

**URL:** <https://discuss.elastic.co/t/filtered-query-vs-query-w-filter/13415>\
**Category:** Elasticsearch\
**Created:** [August 30, 2013, 9:22pm UTC](https://discuss.elastic.co/t/filtered-query-vs-query-w-filter/13415 "2013-08-30T21:22:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![asanderson](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@asanderson](https://discuss.elastic.co/u/asanderson)\
**Post date:** [August 30, 2013, 9:22pm UTC](https://discuss.elastic.co/t/filtered-query-vs-query-w-filter/13415/1 "2013-08-30T21:22:34Z")

</div>

Is there any difference in performance (or otherwise) between using a  
filtered query vs. a query with a filter at the same level?

e.g.  
{  
"query": {  
"filtered" : {  
"query" : {  
"term" : { "foo" : "bar" }  
},  
"filter" : {  
"term" : { "foo2" : "bar2" }  
}  
}  
}  
}

vs.

{  
"query": {  
"term" : { "foo" : "bar" }  
},  
"filter" : {  
"term" : { "foo2" : "bar2" }  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jason\_Wee](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jason\_Wee](https://discuss.elastic.co/u/Jason_Wee)\
**Post date:** [August 31, 2013, 11:22am UTC](https://discuss.elastic.co/t/filtered-query-vs-query-w-filter/13415/2 "2013-08-31T11:22:45Z")

</div>

The hunch said the first example is probably faster but try execute and  
record the timespent? it should give an indication.

Jason

On Sat, Aug 31, 2013 at 5:22 AM, asanderson [a.steven.anderson@gmail.com](mailto:a.steven.anderson@gmail.com)wrote:

> Is there any difference in performance (or otherwise) between using a  
> filtered query vs. a query with a filter at the same level?
> 
> e.g.  
> {  
> "query": {  
> "filtered" : {  
> "query" : {  
> "term" : { "foo" : "bar" }  
> },  
> "filter" : {  
> "term" : { "foo2" : "bar2" }  
> }  
> }  
> }  
> }
> 
> vs.
> 
> {  
> "query": {  
> "term" : { "foo" : "bar" }  
> },  
> "filter" : {  
> "term" : { "foo2" : "bar2" }  
> }  
> }
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [August 31, 2013, 11:01pm UTC](https://discuss.elastic.co/t/filtered-query-vs-query-w-filter/13415/3 "2013-08-31T23:01:07Z")

</div>

A filtered query is faster than a top-level filter. Filtered queries first  
apply the filter, then perform query phase on remaining documents.  
Top-level filter first executes the query (e.g. on all documents), then  
applies the top-level filter to docs that matched the query.

The only time to use a top-level filter is when you need to filter search  
results but not facet results. Since top-level filters are applied after  
queries, and facets get their data from the query results, top-level  
filters don't affect the facet results at all. This can be useful in  
certain situations, usually when you need the facet results in the UI to  
not change.

So yeah, tl;dr: use filtered\_query unless you are doing facets and need  
specific behavior =)

-Zach

On Friday, August 30, 2013 5:22:34 PM UTC-4, asanderson wrote:

> Is there any difference in performance (or otherwise) between using a  
> filtered query vs. a query with a filter at the same level?
> 
> e.g.  
> {  
> "query": {  
> "filtered" : {  
> "query" : {  
> "term" : { "foo" : "bar" }  
> },  
> "filter" : {  
> "term" : { "foo2" : "bar2" }  
> }  
> }  
> }  
> }
> 
> vs.
> 
> {  
> "query": {  
> "term" : { "foo" : "bar" }  
> },  
> "filter" : {  
> "term" : { "foo2" : "bar2" }  
> }  
> }

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![asanderson](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@asanderson](https://discuss.elastic.co/u/asanderson)\
**Post date:** [September 3, 2013, 1:34pm UTC](https://discuss.elastic.co/t/filtered-query-vs-query-w-filter/13415/4 "2013-09-03T13:34:42Z")

</div>

> A filtered query is faster than a top-level filter. Filtered queries  
> first apply the filter, then perform query phase on remaining documents.  
> Top-level filter first executes the query (e.g. on all documents), then  
> applies the top-level filter to docs that matched the query.
> 
> The only time to use a top-level filter is when you need to filter search  
> results but not facet results. Since top-level filters are applied after  
> queries, and facets get their data from the query results, top-level  
> filters don't affect the facet results at all. This can be useful in  
> certain situations, usually when you need the facet results in the UI to  
> not change.
> 
> So yeah, tl;dr: use filtered\_query unless you are doing facets and need  
> specific behavior =)

Zach,

Thanks for the explanation. It would be great if the ES guide had this  
explanation. Also, it would be great if top level filters had some  
parameter that allowed one to specify before or after query phase.

Thanks!

Steve

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:18am UTC](https://discuss.elastic.co/t/filtered-query-vs-query-w-filter/13415/5 "2017-07-06T02:18:23Z")

</div>


