# Elasticsearch Filter And Query

**URL:** <https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225>\
**Category:** Elasticsearch\
**Created:** [January 4, 2013, 6:48am UTC](https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225 "2013-01-04T06:48:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![shanmuthu83](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@shanmuthu83](https://discuss.elastic.co/u/shanmuthu83)\
**Post date:** [January 4, 2013, 6:48am UTC](https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225/1 "2013-01-04T06:48:18Z")

</div>

Hi All

Good Morning to All

In elastic search query have Filter and Query..

Which first execute filter or query..

--  
Regards

_Shanmugam.M_

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 4, 2013, 7:02am UTC](https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225/2 "2013-01-04T07:02:38Z")

</div>

AFAIK Filter first reduces the number of docs to query on.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 4 janv. 2013 à 07:48, Shanmugam Muthanandham [shanmuthu83@gmail.com](mailto:shanmuthu83@gmail.com) a écrit :

Hi All

Good Morning to All

In elastic search query have Filter and Query..

Which first execute filter or query..

--  
Regards

Shanmugam.M

--

--

---

<div class="post-metadata">

**Author:** ![shanmuthu83](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@shanmuthu83](https://discuss.elastic.co/u/shanmuthu83)\
**Post date:** [January 4, 2013, 7:25am UTC](https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225/3 "2013-01-04T07:25:06Z")

</div>

Okie Thanks David

On Fri, Jan 4, 2013 at 12:32 PM, David Pilato [david@pilato.fr](mailto:david@pilato.fr) wrote:

> AFAIK

--  
Regards

_Shanmugam.M_

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [January 4, 2013, 1:24pm UTC](https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225/4 "2013-01-04T13:24:10Z")

</div>

I would say it depends. If you are asking about top level filter[http://www.elasticsearch.org/guide/reference/api/search/filter.html](http://www.elasticsearch.org/guide/reference/api/search/filter.html)and top  
level query[http://www.elasticsearch.org/guide/reference/api/search/query.html](http://www.elasticsearch.org/guide/reference/api/search/query.html)then query is executed first and then results are filtered.

On Friday, January 4, 2013 2:25:06 AM UTC-5, shanmugam wrote:

> Okie Thanks David
> 
> On Fri, Jan 4, 2013 at 12:32 PM, David Pilato \<[da...@pilato.fr](mailto:da...@pilato.fr)\<javascript:\>
> 
> > wrote:
> 
> > AFAIK
> 
> --  
> Regards
> 
> _Shanmugam.M_

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 4, 2013, 1:46pm UTC](https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225/5 "2013-01-04T13:46:33Z")

</div>

Thanks Igor for correcting my mistake.  
So, in which case filter is applied before the query?

Le 4 janvier 2013 à 14:24, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) a écrit :

> I would say it depends. If you are asking about top level filter  
> [http://www.elasticsearch.org/guide/reference/api/search/filter.html](http://www.elasticsearch.org/guide/reference/api/search/filter.html) and top  
> level query  
> [http://www.elasticsearch.org/guide/reference/api/search/query.html](http://www.elasticsearch.org/guide/reference/api/search/query.html) then  
> query is executed first and then results are filtered.
> 
> On Friday, January 4, 2013 2:25:06 AM UTC-5, shanmugam wrote:
> 
> > > Okie Thanks David
> > 
> > On Fri, Jan 4, 2013 at 12:32 PM, David Pilato [da...@pilato.fr](mailto:da...@pilato.fr) wrote:  
> > \> \> \> AFAIK
> > 
> > > > >
> > 
> > --
> > 
> > Regards
> > 
> > Shanmugam.M
> > 
> > >
> 
> --

--  
David Pilato  
[http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
[http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [January 4, 2013, 3:32pm UTC](https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225/6 "2013-01-04T15:32:43Z")

</div>

In 0.20.x and below, it's probably valid to say that filter is applied  
before the query in the filtered query. Technically, both query and filter  
documents are advanced together, but since filter is consulted first, it  
can short-circuit the query execution. In 0.21.x it's more complicated and  
depends on types of queries and filters and can be specified[https://github.com/elasticsearch/elasticsearch/commit/2786e29a10ba2b92bfbff6d396dd2c401aa94193](https://github.com/elasticsearch/elasticsearch/commit/2786e29a10ba2b92bfbff6d396dd2c401aa94193) on  
the query.

On Friday, January 4, 2013 8:46:33 AM UTC-5, David Pilato wrote:

> Thanks Igor for correcting my mistake.  
> So, in which case filter is applied before the query?
> 
> Le 4 janvier 2013 à 14:24, Igor Motov \<[imo...@gmail.com](mailto:imo...@gmail.com) \<javascript:\>\> a  
> écrit :
> 
> I would say it depends. If you are asking about top level filter[http://www.elasticsearch.org/guide/reference/api/search/filter.html](http://www.elasticsearch.org/guide/reference/api/search/filter.html)and top  
> level query[http://www.elasticsearch.org/guide/reference/api/search/query.html](http://www.elasticsearch.org/guide/reference/api/search/query.html)then  
> query is executed first and then results are filtered.
> 
> On Friday, January 4, 2013 2:25:06 AM UTC-5, shanmugam wrote:
> 
> Okie Thanks David
> 
> On Fri, Jan 4, 2013 at 12:32 PM, David Pilato [da...@pilato.fr](mailto:da...@pilato.fr) wrote:
> 
> AFAIK
> 
> --
> 
> Regards
> 
> _Shanmugam.M_
> 
> --
> 
> --  
> David Pilato  
> [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 4, 2013, 3:40pm UTC](https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225/7 "2013-01-04T15:40:27Z")

</div>

Thanks Igor!

Le 4 janvier 2013 à 16:32, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) a écrit :

> In 0.20.x and below, it's probably valid to say that filter is applied before  
> the query in the filtered query. Technically, both query and filter documents  
> are advanced together, but since filter is consulted first, it can  
> short-circuit the query execution. In 0.21.x it's more complicated and depends  
> on types of queries and filters and can be specified  
> [https://github.com/elasticsearch/elasticsearch/commit/2786e29a10ba2b92bfbff6d396dd2c401aa94193](https://github.com/elasticsearch/elasticsearch/commit/2786e29a10ba2b92bfbff6d396dd2c401aa94193)  
> on the query.

--

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)\
**Post date:** [January 5, 2013, 9:06am UTC](https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225/8 "2013-01-05T09:06:39Z")

</div>

I assume by "short circuit" you mean that if a filter results in NO  
matches then the query is skipped.  
Otherwise, how does running the filter first help in a scenerio where a  
filter might reduce the total documents to 1% of all documents? My user  
view was that two "bitsets"of matches: one from the query & one from the  
filter where "and"ed together, so what matter would it make to do one  
or the other 1st?  
Apparently, such a simplistic view is incorrect. I'm interested to know  
how to make filters (which might be cached) feed into a query, so the  
query doesn't bother with as many docs and only processes that with has  
been pre-filtered.

-Paul

On 1/4/2013 7:32 AM, Igor Motov wrote:

> In 0.20.x and below, it's probably valid to say that filter is applied  
> before the query in the filtered query. Technically, both query and  
> filter documents are advanced together, but since filter is consulted  
> first, it can short-circuit the query execution. In 0.21.x it's more  
> complicated and depends on types of queries and filters and can be  
> specified  
> [https://github.com/elasticsearch/elasticsearch/commit/2786e29a10ba2b92bfbff6d396dd2c401aa94193](https://github.com/elasticsearch/elasticsearch/commit/2786e29a10ba2b92bfbff6d396dd2c401aa94193) on  
> the query.

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [January 8, 2013, 3:11am UTC](https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225/9 "2013-01-08T03:11:36Z")

</div>

Paul,

I think your "simplistic" view is correct in some cases. But there are also  
cases where "bitsets" don't exist. Consider this somewhat absurd query for  
example: [filter\_or\_query.sh · GitHub](https://gist.github.com/111db5810fadcbf5f4c8)

On Saturday, January 5, 2013 4:06:39 AM UTC-5, P Hill wrote:

> I assume by "short circuit" you mean that if a filter results in NO  
> matches then the query is skipped.  
> Otherwise, how does running the filter first help in a scenerio where a  
> filter might reduce the total documents to 1% of all documents? My user  
> view was that two "bitsets"of matches: one from the query & one from the  
> filter where "and"ed together, so what matter would it make to do one or  
> the other 1st?  
> Apparently, such a simplistic view is incorrect. I'm interested to know  
> how to make filters (which might be cached) feed into a query, so the query  
> doesn't bother with as many docs and only processes that with has been  
> pre-filtered.
> 
> -Paul
> 
> On 1/4/2013 7:32 AM, Igor Motov wrote:
> 
> In 0.20.x and below, it's probably valid to say that filter is applied  
> before the query in the filtered query. Technically, both query and filter  
> documents are advanced together, but since filter is consulted first, it  
> can short-circuit the query execution. In 0.21.x it's more complicated and  
> depends on types of queries and filters and can be specified[https://github.com/elasticsearch/elasticsearch/commit/2786e29a10ba2b92bfbff6d396dd2c401aa94193](https://github.com/elasticsearch/elasticsearch/commit/2786e29a10ba2b92bfbff6d396dd2c401aa94193) on  
> the query.

--

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)\
**Post date:** [January 8, 2013, 9:39am UTC](https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225/10 "2013-01-08T09:39:24Z")

</div>

My mental model does help me to see why this is a special case.

you inserted 10 docs each with a doc field 1 .. 10.  
The filter finds 5, 6 AND 7; The query only matches 5 OR 7.  
So why would each NOT be represented as a bitset (a compact list of docs  
that match each represented in order by a yes or no)? Is it because of  
the constant score? I'm not seeing it.

-Paul

On 1/7/2013 7:11 PM, Igor Motov wrote:

> Paul,
> 
> I think your "simplistic" view is correct in some cases. But there are  
> also cases where "bitsets" don't exist. Consider this somewhat absurd  
> query for example: [filter\_or\_query.sh · GitHub](https://gist.github.com/111db5810fadcbf5f4c8)
> 
> On Saturday, January 5, 2013 4:06:39 AM UTC-5, P Hill wrote:
> 
> ```
> I assume by "short circuit" you mean that if a filter results in
> NO matches then the query is skipped.
> Otherwise, how does running the filter first help in a scenerio
> where a filter might reduce the total documents to 1% of all
> documents? My user view was that two "bitsets"of matches: one
> from the query & one from the filter where "and"ed together, so
> what matter would it make to do one or the other 1st?
> Apparently, such a simplistic view is incorrect. I'm interested
> to know how to make filters (which might be cached) feed into a
> query, so the query doesn't bother with as many docs and only
> processes that with has been pre-filtered.
> 
> -Paul
> 
> On 1/4/2013 7:32 AM, Igor Motov wrote:
> 
> ```
> 
> > ```
> > In 0.20.x and below, it's probably valid to say that filter is
> > applied before the query in the filtered query. Technically, both
> > query and filter documents are advanced together, but since
> > filter is consulted first, it can short-circuit the query
> > execution. In 0.21.x it's more complicated and depends on types
> > of queries and filters and can be specified
> > <https://github.com/elasticsearch/elasticsearch/commit/2786e29a10ba2b92bfbff6d396dd2c401aa94193> on
> > the query.
> > 
> > ```
> 
> --

--

---

<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:57am UTC](https://discuss.elastic.co/t/elasticsearch-filter-and-query/10225/11 "2017-07-06T02:57:24Z")

</div>


