# Filtered query + filter vs. filterd + filter only

**URL:** <https://discuss.elastic.co/t/filtered-query-filter-vs-filterd-filter-only/12206>\
**Category:** Elasticsearch\
**Created:** [May 31, 2013, 1:30pm UTC](https://discuss.elastic.co/t/filtered-query-filter-vs-filterd-filter-only/12206 "2013-05-31T13:30:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Kinsalb](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@Chris\_Kinsalb](https://discuss.elastic.co/u/Chris_Kinsalb)\
**Post date:** [May 31, 2013, 1:30pm UTC](https://discuss.elastic.co/t/filtered-query-filter-vs-filterd-filter-only/12206/1 "2013-05-31T13:30:10Z")

</div>

The subject might be a bit confusing, so let me get straight down to two  
different examples:  
This assumes a default field "message" [analyzed] is configured for the  
query\_string query, whereas the fields "name" and "term" are not analysed &  
\_all is disabled:

_A.) Query + filter:_

{  
"query": {  
"filtered": {  
"query": {  
"query\_string": {  
"query": "smith"  
}  
},  
"filter": {  
"and": [  
{  
"term": {  
"name": "foo"  
}  
},  
{  
"term": {  
"account": "bar"  
}  
}  
]  
}  
}  
}  
}

_B.) Filter only:_

{  
"query": {  
"filtered": {  
"filter": {  
"and": [  
{  
"term": {  
"name": "foo"  
}  
},  
{  
"term": {  
"account": "bar"  
}  
},  
{  
"query": {  
"query\_string": {  
"query": "smith"  
}  
}  
}  
]  
}  
}  
}  
}

My tests do not show a difference in the generated results.  
So, what is the difference?

My understanding is:

- A filter limits the set that a query runs on
- I then run a query on the filtered set which is lots faster, since the  
filter is cached.  
==\> this would correspond to _A.)_

When using filters only, as in "_B.)_", I would expect that this would be  
even faster, since the whole operation is just a reduction of a given set  
of documents.

Or does _B.)_ automagically add a "missing" _{ "query": "match\_all": {} }_  
?

Thanks for enlightening me,

- Chris

--  
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:** ![Chris\_Kinsalb](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@Chris\_Kinsalb](https://discuss.elastic.co/u/Chris_Kinsalb)\
**Post date:** [June 3, 2013, 6:53am UTC](https://discuss.elastic.co/t/filtered-query-filter-vs-filterd-filter-only/12206/2 "2013-06-03T06:53:31Z")

</div>

Anyone?

On Friday, May 31, 2013 3:30:10 PM UTC+2, Chris Kinsalb wrote:

> The subject might be a bit confusing, so let me get straight down to two  
> different examples:  
> This assumes a default field "message" [analyzed] is configured for the  
> query\_string query, whereas the fields "name" and "term" are not analysed &  
> \_all is disabled:
> 
> _A.) Query + filter:_
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "query\_string": {  
> "query": "smith"  
> }  
> },  
> "filter": {  
> "and": [  
> {  
> "term": {  
> "name": "foo"  
> }  
> },  
> {  
> "term": {  
> "account": "bar"  
> }  
> }  
> ]  
> }  
> }  
> }  
> }
> 
> _B.) Filter only:_
> 
> {  
> "query": {  
> "filtered": {  
> "filter": {  
> "and": [  
> {  
> "term": {  
> "name": "foo"  
> }  
> },  
> {  
> "term": {  
> "account": "bar"  
> }  
> },  
> {  
> "query": {  
> "query\_string": {  
> "query": "smith"  
> }  
> }  
> }  
> ]  
> }  
> }  
> }  
> }
> 
> My tests do not show a difference in the generated results.  
> So, what is the difference?
> 
> My understanding is:
> 
> - A filter limits the set that a query runs on
> - I then run a query on the filtered set which is lots faster, since the  
> filter is cached.  
> ==\> this would correspond to _A.)_
> 
> When using filters only, as in "_B.)_", I would expect that this would be  
> even faster, since the whole operation is just a reduction of a given set  
> of documents.
> 
> Or does _B.)_ automagically add a "missing" _{ "query": "match\_all": {} }_  
> ?
> 
> Thanks for enlightening me,
> 
> - Chris

--  
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:** ![Chris\_Kinsalb](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@Chris\_Kinsalb](https://discuss.elastic.co/u/Chris_Kinsalb)\
**Post date:** [June 5, 2013, 8:23am UTC](https://discuss.elastic.co/t/filtered-query-filter-vs-filterd-filter-only/12206/3 "2013-06-05T08:23:55Z")

</div>

Let me post my assumption here:

_B.)_ will be faster in general, not just because it is a set reduction,  
but also (or mainly) since it doesn't involve scoring.

However, my other question still remains: is a \*{ "query": "match\_all": {}  
} or "constant\_score" \*query automagically added to the filter-only version  
(_B.)_) ?

- Chris

On Monday, June 3, 2013 8:53:31 AM UTC+2, Chris Kinsalb wrote:

> Anyone?
> 
> On Friday, May 31, 2013 3:30:10 PM UTC+2, Chris Kinsalb wrote:
> 
> > The subject might be a bit confusing, so let me get straight down to two  
> > different examples:  
> > This assumes a default field "message" [analyzed] is configured for the  
> > query\_string query, whereas the fields "name" and "term" are not analysed &  
> > \_all is disabled:
> > 
> > _A.) Query + filter:_
> > 
> > {  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "query\_string": {  
> > "query": "smith"  
> > }  
> > },  
> > "filter": {  
> > "and": [  
> > {  
> > "term": {  
> > "name": "foo"  
> > }  
> > },  
> > {  
> > "term": {  
> > "account": "bar"  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }  
> > }
> > 
> > _B.) Filter only:_
> > 
> > {  
> > "query": {  
> > "filtered": {  
> > "filter": {  
> > "and": [  
> > {  
> > "term": {  
> > "name": "foo"  
> > }  
> > },  
> > {  
> > "term": {  
> > "account": "bar"  
> > }  
> > },  
> > {  
> > "query": {  
> > "query\_string": {  
> > "query": "smith"  
> > }  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }  
> > }
> > 
> > My tests do not show a difference in the generated results.  
> > So, what is the difference?
> > 
> > My understanding is:
> > 
> > - A filter limits the set that a query runs on
> > - I then run a query on the filtered set which is lots faster, since the  
> > filter is cached.  
> > ==\> this would correspond to _A.)_
> > 
> > When using filters only, as in "_B.)_", I would expect that this would  
> > be even faster, since the whole operation is just a reduction of a given  
> > set of documents.
> > 
> > Or does _B.)_ automagically add a "missing" \*{ "query": "match\_all": {} }
> > 
> > - ?
> > 
> > Thanks for enlightening me,
> > 
> > - Chris

--  
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:32am UTC](https://discuss.elastic.co/t/filtered-query-filter-vs-filterd-filter-only/12206/4 "2017-07-06T02:32:49Z")

</div>


