# ES FILTERED QUERY filers first with terms (cheap) and then match(expensive), or otherwise?

**URL:** <https://discuss.elastic.co/t/es-filtered-query-filers-first-with-terms-cheap-and-then-match-expensive-or-otherwise/21859>\
**Category:** Elasticsearch\
**Created:** [January 27, 2015, 11:46pm UTC](https://discuss.elastic.co/t/es-filtered-query-filers-first-with-terms-cheap-and-then-match-expensive-or-otherwise/21859 "2015-01-27T23:46:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabriel\_Gavilan\_gavi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_gavilan_gavi/32/961_2.png) [@Gabriel\_Gavilan\_gavi](https://discuss.elastic.co/u/Gabriel_Gavilan_gavi)\
**Post date:** [January 27, 2015, 11:46pm UTC](https://discuss.elastic.co/t/es-filtered-query-filers-first-with-terms-cheap-and-then-match-expensive-or-otherwise/21859/1 "2015-01-27T23:46:57Z")

</div>

So I suppose that when I run a filtered query like this one, ES filters all  
the documents in the database, and then performs the match query only to  
the documents that fit the filter, right? I just want to make sure that it  
doesn't perform the match query on all the documents and the drop the ones  
that don't fit the filter, because that would be a total waste of resources  
as I'm only interested on a small portion of the documents.

{  
"query" : {  
"filtered" : {  
"query": {  
"match": {  
"field1": "whatever"  
}  
},  
"filter" : {  
"term" : {  
"field2" : 10  
}  
}  
}  
}  
}

Thanks for clarifying this!

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/ee8f3294-9761-4d7b-b8b1-e49943d88ae7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ee8f3294-9761-4d7b-b8b1-e49943d88ae7%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Erwin\_Rijss](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erwin_rijss/32/955_2.png) [@Erwin\_Rijss](https://discuss.elastic.co/u/Erwin_Rijss)\
**Post date:** [January 28, 2015, 10:44am UTC](https://discuss.elastic.co/t/es-filtered-query-filers-first-with-terms-cheap-and-then-match-expensive-or-otherwise/21859/2 "2015-01-28T10:44:06Z")

</div>

Yup

On Wednesday, January 28, 2015 at 12:46:58 AM UTC+1, Gabriel Gavilan  
gavilán wrote:

> So I suppose that when I run a filtered query like this one, ES filters  
> all the documents in the database, and then performs the match query only  
> to the documents that fit the filter, right? I just want to make sure that  
> it doesn't perform the match query on all the documents and the drop the  
> ones that don't fit the filter, because that would be a total waste of  
> resources as I'm only interested on a small portion of the documents.
> 
> {  
> "query" : {  
> "filtered" : {  
> "query": {  
> "match": {  
> "field1": "whatever"  
> }  
> },  
> "filter" : {  
> "term" : {  
> "field2" : 10  
> }  
> }  
> }  
> }  
> }
> 
> Thanks for clarifying this!

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/3827c690-9a0b-43b0-9502-adaf020141bf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3827c690-9a0b-43b0-9502-adaf020141bf%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Erwin\_Rijss](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erwin_rijss/32/955_2.png) [@Erwin\_Rijss](https://discuss.elastic.co/u/Erwin_Rijss)\
**Post date:** [January 28, 2015, 10:49am UTC](https://discuss.elastic.co/t/es-filtered-query-filers-first-with-terms-cheap-and-then-match-expensive-or-otherwise/21859/3 "2015-01-28T10:49:24Z")

</div>

You're right.

And there is also the post\_filter (formerly known as "filter")  
([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-post-filter.html))  
which starts with the query and ends with the filtering.

On Wednesday, January 28, 2015 at 12:46:58 AM UTC+1, Gabriel Gavilan  
gavilán wrote:

> So I suppose that when I run a filtered query like this one, ES filters  
> all the documents in the database, and then performs the match query only  
> to the documents that fit the filter, right? I just want to make sure that  
> it doesn't perform the match query on all the documents and the drop the  
> ones that don't fit the filter, because that would be a total waste of  
> resources as I'm only interested on a small portion of the documents.
> 
> {  
> "query" : {  
> "filtered" : {  
> "query": {  
> "match": {  
> "field1": "whatever"  
> }  
> },  
> "filter" : {  
> "term" : {  
> "field2" : 10  
> }  
> }  
> }  
> }  
> }
> 
> Thanks for clarifying this!

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/ca46ccaa-dc6a-4827-b795-6ff23cad7b6d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ca46ccaa-dc6a-4827-b795-6ff23cad7b6d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:36am UTC](https://discuss.elastic.co/t/es-filtered-query-filers-first-with-terms-cheap-and-then-match-expensive-or-otherwise/21859/4 "2017-07-06T00:36:13Z")

</div>


