# How to specify execution order of filter and query?

**URL:** <https://discuss.elastic.co/t/how-to-specify-execution-order-of-filter-and-query/15880>\
**Category:** Elasticsearch\
**Created:** [February 18, 2014, 8:42pm UTC](https://discuss.elastic.co/t/how-to-specify-execution-order-of-filter-and-query/15880 "2014-02-18T20:42:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivan\_Hall](https://avatars.discourse-cdn.com/v4/letter/i/59ef9b/32.png) [@Ivan\_Hall](https://discuss.elastic.co/u/Ivan_Hall)\
**Post date:** [February 18, 2014, 8:42pm UTC](https://discuss.elastic.co/t/how-to-specify-execution-order-of-filter-and-query/15880/1 "2014-02-18T20:42:38Z")

</div>

I am brand new to ElasticSearch and have a few fundamental questions  
regarding queries and filters.

1. _How does one control ordering of filter vs query? Is it possible to  
filter first, then perform a query on those results?_

With a filter query[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html) the  
documentation reads:

A query that applies a filter to the results of another query.

How I interpret that statement is that the query will run first then the  
filter will be applied. Can this behavior be adjusted?

Also, I can execute a search with an independent filter and query which  
produces the same results as a filtered query. It appears the timing is  
about the same as well.

1. _Can I simply place the filter first within the JSON object then the  
query to control whether the filter is run before the query?_

Ultimately, I just really want to know the behavior for when filters are  
run before queries and if there is a possibility to control the execution  
precedence.

Thank you!

--  
Coyote Logistics, LLC is a licensed Property Broker (MC# 561135-B).

> **[Terms, Privacy Policy, and Agreements - Coyote](https://www.coyote.com/terms/)**
>
> TERMS 877-6-COYOTE Request a Quote Now  WEBSITE TERMS OF USE PRIVACY POLICY COYOTEGO END USER SOFTWARE LICENSE AGREEMENT MOBILE TRACKING DRIVER LOCATION SERVICE TERMS COOKIE POLICY MEXICO PRIVACY POLICY WEBSITE TERMS OF USE Last revised: 03/19/2017...

--  
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/7c36c760-3b47-41a8-89c9-1da3a22ac153%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7c36c760-3b47-41a8-89c9-1da3a22ac153%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [February 18, 2014, 9:02pm UTC](https://discuss.elastic.co/t/how-to-specify-execution-order-of-filter-and-query/15880/2 "2014-02-18T21:02:56Z")

</div>

Hi Ivan (awesome name BTW),

Read my recent reply about filters:  
[https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/xZGnyTI6lmo](https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/xZGnyTI6lmo)

Basically the filter in a filtered query is executed before the query. IMHO  
that documentation is misleading. Use post filters (simply 'filter' before  
1.0) to execute the filters after the query.

--  
Ivan

On Tue, Feb 18, 2014 at 12:42 PM, Ivan Hall [ivan.hall@coyote.com](mailto:ivan.hall@coyote.com) wrote:

> I am brand new to Elasticsearch and have a few fundamental questions  
> regarding queries and filters.
> 
> 1. _How does one control ordering of filter vs query? Is it possible to  
> filter first, then perform a query on those results?_
> 
> With a filter query[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html) the  
> documentation reads:
> 
> A query that applies a filter to the results of another query.
> 
> How I interpret that statement is that the query will run first then the  
> filter will be applied. Can this behavior be adjusted?
> 
> Also, I can execute a search with an independent filter and query which  
> produces the same results as a filtered query. It appears the timing is  
> about the same as well.
> 
> 1. _Can I simply place the filter first within the JSON object then the  
> query to control whether the filter is run before the query?_
> 
> Ultimately, I just really want to know the behavior for when filters are  
> run before queries and if there is a possibility to control the execution  
> precedence.
> 
> Thank you!
> 
> Coyote Logistics, LLC is a licensed Property Broker (MC# 561135-B).  
> [http://www.coyote.com/disclaimer/](http://www.coyote.com/disclaimer/)
> 
> --  
> 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/7c36c760-3b47-41a8-89c9-1da3a22ac153%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7c36c760-3b47-41a8-89c9-1da3a22ac153%40googlegroups.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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDiojU\_Rza%3D882\_4M8HV4LkOm0pBsr2XQLORyu-0eVVoQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDiojU_Rza%3D882_4M8HV4LkOm0pBsr2XQLORyu-0eVVoQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [February 18, 2014, 9:19pm UTC](https://discuss.elastic.co/t/how-to-specify-execution-order-of-filter-and-query/15880/3 "2014-02-18T21:19:50Z")

</div>

The documentation suddenly made me doubt if we I knew was wrong. 🙂

The default strategy for Elasticsearch's filtered query is a custom random  
access one. For each document, it will first check the docSet before  
executing the query.

[https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/index/query/FilteredQueryParser.java#L64](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/index/query/FilteredQueryParser.java#L64)

[https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/lucene/search/XFilteredQuery.java#L188-L200](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/lucene/search/XFilteredQuery.java#L188-L200)

The strategy is configurable, so it would be nice to be added to the  
documentation. The original description of the filtered query comes  
directly from Lucene:  
[http://lucene.apache.org/core/4\_6\_0/core/org/apache/lucene/search/FilteredQuery.html](http://lucene.apache.org/core/4_6_0/core/org/apache/lucene/search/FilteredQuery.html)

Cheers,

Ivan

On Tue, Feb 18, 2014 at 1:02 PM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> Hi Ivan (awesome name BTW),
> 
> Read my recent reply about filters:  
> [Redirecting to Google Groups](https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/xZGnyTI6lmo)
> 
> Basically the filter in a filtered query is executed before the query.  
> IMHO that documentation is misleading. Use post filters (simply 'filter'  
> before 1.0) to execute the filters after the query.
> 
> --  
> Ivan
> 
> On Tue, Feb 18, 2014 at 12:42 PM, Ivan Hall [ivan.hall@coyote.com](mailto:ivan.hall@coyote.com) wrote:
> 
> > I am brand new to Elasticsearch and have a few fundamental questions  
> > regarding queries and filters.
> > 
> > 1. _How does one control ordering of filter vs query? Is it possible  
> > to filter first, then perform a query on those results?_
> > 
> > With a filter query[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html) the  
> > documentation reads:
> > 
> > A query that applies a filter to the results of another query.
> > 
> > How I interpret that statement is that the query will run first then the  
> > filter will be applied. Can this behavior be adjusted?
> > 
> > Also, I can execute a search with an independent filter and query which  
> > produces the same results as a filtered query. It appears the timing is  
> > about the same as well.
> > 
> > 1. _Can I simply place the filter first within the JSON object then the  
> > query to control whether the filter is run before the query?_
> > 
> > Ultimately, I just really want to know the behavior for when filters are  
> > run before queries and if there is a possibility to control the execution  
> > precedence.
> > 
> > Thank you!
> > 
> > Coyote Logistics, LLC is a licensed Property Broker (MC# 561135-B).  
> > [http://www.coyote.com/disclaimer/](http://www.coyote.com/disclaimer/)
> > 
> > --  
> > 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/7c36c760-3b47-41a8-89c9-1da3a22ac153%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7c36c760-3b47-41a8-89c9-1da3a22ac153%40googlegroups.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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQD3LZDgjF54VQVp9aEf1imUy%3Dr3mO%2BR68PHA2TgvBzwuA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQD3LZDgjF54VQVp9aEf1imUy%3Dr3mO%2BR68PHA2TgvBzwuA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan\_Hall](https://avatars.discourse-cdn.com/v4/letter/i/59ef9b/32.png) [@Ivan\_Hall](https://discuss.elastic.co/u/Ivan_Hall)\
**Post date:** [February 19, 2014, 3:31pm UTC](https://discuss.elastic.co/t/how-to-specify-execution-order-of-filter-and-query/15880/4 "2014-02-19T15:31:11Z")

</div>

I think I screwed up the reply... sorry if you got multiple replies Ivan.

What I said in those replies is:

Looks like from the github links that a 'filter first' strategy has yet to  
be implemented.

[https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/lucene/search/XFilteredQuery.java#L183](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/lucene/search/XFilteredQuery.java#L183)

I guess I'll stick with the default strategy for now due to inexperience  
and lack of experimentation. I assume it should provide generally good  
performance.

As I learn more I might try to mess with strategy and hopefully a filter  
first strategy gets implemented. I would assume this would greatly  
increase string based queries by filtering to a subset of documents first!

On Tuesday, February 18, 2014 3:19:50 PM UTC-6, Ivan Brusic wrote:

> The documentation suddenly made me doubt if we I knew was wrong. 🙂
> 
> The default strategy for Elasticsearch's filtered query is a custom random  
> access one. For each document, it will first check the docSet before  
> executing the query.
> 
> [https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/index/query/FilteredQueryParser.java#L64](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/index/query/FilteredQueryParser.java#L64)
> 
> [https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/lucene/search/XFilteredQuery.java#L188-L200](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/lucene/search/XFilteredQuery.java#L188-L200)
> 
> The strategy is configurable, so it would be nice to be added to the  
> documentation. The original description of the filtered query comes  
> directly from Lucene:  
> [FilteredQuery (Lucene 4.6.0 API)](http://lucene.apache.org/core/4_6_0/core/org/apache/lucene/search/FilteredQuery.html)
> 
> Cheers,
> 
> Ivan
> 
> On Tue, Feb 18, 2014 at 1:02 PM, Ivan Brusic \<[iv...@brusic.com](mailto:iv...@brusic.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi Ivan (awesome name BTW),
> > 
> > Read my recent reply about filters:  
> > [Redirecting to Google Groups](https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/xZGnyTI6lmo)
> > 
> > Basically the filter in a filtered query is executed before the query.  
> > IMHO that documentation is misleading. Use post filters (simply 'filter'  
> > before 1.0) to execute the filters after the query.
> > 
> > --  
> > Ivan
> > 
> > On Tue, Feb 18, 2014 at 12:42 PM, Ivan Hall \<[ivan...@coyote.com](mailto:ivan...@coyote.com)\<javascript:\>
> > 
> > > wrote:
> > 
> > > I am brand new to Elasticsearch and have a few fundamental questions  
> > > regarding queries and filters.
> > > 
> > > 1. _How does one control ordering of filter vs query? Is it possible  
> > > to filter first, then perform a query on those results?_
> > > 
> > > With a filter query[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html) the  
> > > documentation reads:
> > > 
> > > A query that applies a filter to the results of another query.
> > > 
> > > How I interpret that statement is that the query will run first then the  
> > > filter will be applied. Can this behavior be adjusted?
> > > 
> > > Also, I can execute a search with an independent filter and query which  
> > > produces the same results as a filtered query. It appears the timing is  
> > > about the same as well.
> > > 
> > > 1. _Can I simply place the filter first within the JSON object then the  
> > > query to control whether the filter is run before the query?_
> > > 
> > > Ultimately, I just really want to know the behavior for when filters are  
> > > run before queries and if there is a possibility to control the execution  
> > > precedence.
> > > 
> > > Thank you!
> > > 
> > > Coyote Logistics, LLC is a licensed Property Broker (MC# 561135-B).  
> > > [http://www.coyote.com/disclaimer/](http://www.coyote.com/disclaimer/)
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/7c36c760-3b47-41a8-89c9-1da3a22ac153%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7c36c760-3b47-41a8-89c9-1da3a22ac153%40googlegroups.com)  
> > > .  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Coyote Logistics, LLC is a licensed Property Broker (MC# 561135-B).  
[http://www.coyote.com/disclaimer/](http://www.coyote.com/disclaimer/)

--  
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/bbc991c6-b6f8-43c0-82be-906ced299e37%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bbc991c6-b6f8-43c0-82be-906ced299e37%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan\_Hall](https://avatars.discourse-cdn.com/v4/letter/i/59ef9b/32.png) [@Ivan\_Hall](https://discuss.elastic.co/u/Ivan_Hall)\
**Post date:** [February 19, 2014, 3:48pm UTC](https://discuss.elastic.co/t/how-to-specify-execution-order-of-filter-and-query/15880/5 "2014-02-19T15:48:01Z")

</div>

Also, I had another question regarding the difference between these two  
queries:

1. 

{  
"query": { ... },  
"filter": { ... }  
}

1. 

{  
"filtered": {  
"query": { ... },  
"filter": { ... }  
}  
}

When I try these they both produce the same results. What is the technical  
difference between these two?

On Wednesday, February 19, 2014 9:31:11 AM UTC-6, Ivan Hall wrote:

> I think I screwed up the reply... sorry if you got multiple replies Ivan.
> 
> What I said in those replies is:
> 
> Looks like from the github links that a 'filter first' strategy has yet to  
> be implemented.
> 
> [https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/lucene/search/XFilteredQuery.java#L183](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/lucene/search/XFilteredQuery.java#L183)
> 
> I guess I'll stick with the default strategy for now due to inexperience  
> and lack of experimentation. I assume it should provide generally good  
> performance.
> 
> As I learn more I might try to mess with strategy and hopefully a filter  
> first strategy gets implemented. I would assume this would greatly  
> increase string based queries by filtering to a subset of documents first!
> 
> On Tuesday, February 18, 2014 3:19:50 PM UTC-6, Ivan Brusic wrote:
> 
> > The documentation suddenly made me doubt if we I knew was wrong. 🙂
> > 
> > The default strategy for Elasticsearch's filtered query is a custom  
> > random access one. For each document, it will first check the docSet before  
> > executing the query.
> > 
> > [https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/index/query/FilteredQueryParser.java#L64](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/index/query/FilteredQueryParser.java#L64)
> > 
> > [https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/lucene/search/XFilteredQuery.java#L188-L200](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/lucene/search/XFilteredQuery.java#L188-L200)
> > 
> > The strategy is configurable, so it would be nice to be added to the  
> > documentation. The original description of the filtered query comes  
> > directly from Lucene:  
> > [FilteredQuery (Lucene 4.6.0 API)](http://lucene.apache.org/core/4_6_0/core/org/apache/lucene/search/FilteredQuery.html)
> > 
> > Cheers,
> > 
> > Ivan
> > 
> > On Tue, Feb 18, 2014 at 1:02 PM, Ivan Brusic [iv...@brusic.com](mailto:iv...@brusic.com) wrote:
> > 
> > > Hi Ivan (awesome name BTW),
> > > 
> > > Read my recent reply about filters:  
> > > [Redirecting to Google Groups](https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/xZGnyTI6lmo)
> > > 
> > > Basically the filter in a filtered query is executed before the query.  
> > > IMHO that documentation is misleading. Use post filters (simply 'filter'  
> > > before 1.0) to execute the filters after the query.
> > > 
> > > --  
> > > Ivan
> > > 
> > > On Tue, Feb 18, 2014 at 12:42 PM, Ivan Hall [ivan...@coyote.com](mailto:ivan...@coyote.com) wrote:
> > > 
> > > > I am brand new to Elasticsearch and have a few fundamental questions  
> > > > regarding queries and filters.
> > > > 
> > > > 1. _How does one control ordering of filter vs query? Is it possible  
> > > > to filter first, then perform a query on those results?_
> > > > 
> > > > With a filter query[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html) the  
> > > > documentation reads:
> > > > 
> > > > A query that applies a filter to the results of another query.
> > > > 
> > > > How I interpret that statement is that the query will run first then  
> > > > the filter will be applied. Can this behavior be adjusted?
> > > > 
> > > > Also, I can execute a search with an independent filter and query which  
> > > > produces the same results as a filtered query. It appears the timing is  
> > > > about the same as well.
> > > > 
> > > > 1. _Can I simply place the filter first within the JSON object then  
> > > > the query to control whether the filter is run before the query?_
> > > > 
> > > > Ultimately, I just really want to know the behavior for when filters  
> > > > are run before queries and if there is a possibility to control the  
> > > > execution precedence.
> > > > 
> > > > Thank you!
> > > > 
> > > > Coyote Logistics, LLC is a licensed Property Broker (MC# 561135-B).  
> > > > [http://www.coyote.com/disclaimer/](http://www.coyote.com/disclaimer/)
> > > > 
> > > > --  
> > > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > > To view this discussion on the web visit  
> > > > [https://groups.google.com/d/msgid/elasticsearch/7c36c760-3b47-41a8-89c9-1da3a22ac153%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7c36c760-3b47-41a8-89c9-1da3a22ac153%40googlegroups.com)  
> > > > .  
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> Coyote Logistics, LLC is a licensed Property Broker (MC# 561135-B).  
> [http://www.coyote.com/disclaimer/](http://www.coyote.com/disclaimer/)

--  
Coyote Logistics, LLC is a licensed Property Broker (MC# 561135-B).  
[http://www.coyote.com/disclaimer/](http://www.coyote.com/disclaimer/)

--  
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/fa206889-d384-4daa-a17f-3077c7dba5d7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fa206889-d384-4daa-a17f-3077c7dba5d7%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [February 19, 2014, 4:37pm UTC](https://discuss.elastic.co/t/how-to-specify-execution-order-of-filter-and-query/15880/6 "2014-02-19T16:37:33Z")

</div>

Other than what Ivan Brusic already explained, there is also a difference  
in how the facets/aggregations are computed. In your first example, the  
aggregations are computed based on the results that match only the query  
part. In your second example, the aggregations are computed based on the  
results from both the query + filter.

--  
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/2d4c51d5-bb15-4e88-888b-c5b16dc108ec%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2d4c51d5-bb15-4e88-888b-c5b16dc108ec%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [February 20, 2014, 12:05am UTC](https://discuss.elastic.co/t/how-to-specify-execution-order-of-filter-and-query/15880/7 "2014-02-20T00:05:10Z")

</div>

If you trace the code more, you would see that it eventually uses  
Lucene's LEAP\_FROG\_FILTER\_FIRST\_STRATEGY which according to the docs:  
"Note: This strategy uses the filter to lead the iteration.". This strategy  
is used when the default threshold (-1) is set and the DocIdSet (basically  
bits) is "fast" (aka nothing expense like geo).

Binh answered your other question. Basically the outer filter is now called  
post\_filter to remove the ambiguity.

Cheers,

Ivan

On Wed, Feb 19, 2014 at 7:31 AM, Ivan Hall [ivan.hall@coyote.com](mailto:ivan.hall@coyote.com) wrote:

> I think I screwed up the reply... sorry if you got multiple replies Ivan.
> 
> What I said in those replies is:
> 
> Looks like from the github links that a 'filter first' strategy has yet to  
> be implemented.
> 
> [https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/lucene/search/XFilteredQuery.java#L183](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/lucene/search/XFilteredQuery.java#L183)
> 
> I guess I'll stick with the default strategy for now due to inexperience  
> and lack of experimentation. I assume it should provide generally good  
> performance.
> 
> As I learn more I might try to mess with strategy and hopefully a filter  
> first strategy gets implemented. I would assume this would greatly  
> increase string based queries by filtering to a subset of documents first!
> 
> On Tuesday, February 18, 2014 3:19:50 PM UTC-6, Ivan Brusic wrote:
> 
> > The documentation suddenly made me doubt if we I knew was wrong. 🙂
> > 
> > The default strategy for Elasticsearch's filtered query is a custom  
> > random access one. For each document, it will first check the docSet before  
> > executing the query.
> > 
> > [GitHub - elastic/elasticsearch: Free and Open, Distributed, RESTful Search Engine](https://github.com/elasticsearch/elasticsearch/)  
> > blob/master/src/main/java/org/elasticsearch/index/query/  
> > FilteredQueryParser.java#L64
> > 
> > [GitHub - elastic/elasticsearch: Free and Open, Distributed, RESTful Search Engine](https://github.com/elasticsearch/elasticsearch/)  
> > blob/master/src/main/java/org/elasticsearch/common/lucene/  
> > search/XFilteredQuery.java#L188-L200
> > 
> > The strategy is configurable, so it would be nice to be added to the  
> > documentation. The original description of the filtered query comes  
> > directly from Lucene: [Index of /\_\_root/docs.lucene.apache.org/core/4\_6\_0/core/org](http://lucene.apache.org/core/4_6_0/core/org/)  
> > apache/lucene/search/FilteredQuery.html
> > 
> > Cheers,
> > 
> > Ivan
> > 
> > On Tue, Feb 18, 2014 at 1:02 PM, Ivan Brusic [iv...@brusic.com](mailto:iv...@brusic.com) wrote:
> > 
> > > Hi Ivan (awesome name BTW),
> > > 
> > > Read my recent reply about filters: [https://groups](https://groups).  
> > > [google.com/forum/?fromgroups#!topic/elasticsearch/xZGnyTI6lmo](http://google.com/forum/?fromgroups#!topic/elasticsearch/xZGnyTI6lmo)
> > > 
> > > Basically the filter in a filtered query is executed before the query.  
> > > IMHO that documentation is misleading. Use post filters (simply 'filter'  
> > > before 1.0) to execute the filters after the query.
> > > 
> > > --  
> > > Ivan
> > > 
> > > On Tue, Feb 18, 2014 at 12:42 PM, Ivan Hall [ivan...@coyote.com](mailto:ivan...@coyote.com) wrote:
> > > 
> > > > I am brand new to Elasticsearch and have a few fundamental questions  
> > > > regarding queries and filters.
> > > > 
> > > > 1. _How does one control ordering of filter vs query? Is it possible  
> > > > to filter first, then perform a query on those results?_
> > > > 
> > > > With a filter query[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html) the  
> > > > documentation reads:
> > > > 
> > > > A query that applies a filter to the results of another query.
> > > > 
> > > > How I interpret that statement is that the query will run first then  
> > > > the filter will be applied. Can this behavior be adjusted?
> > > > 
> > > > Also, I can execute a search with an independent filter and query which  
> > > > produces the same results as a filtered query. It appears the timing is  
> > > > about the same as well.
> > > > 
> > > > 1. _Can I simply place the filter first within the JSON object then  
> > > > the query to control whether the filter is run before the query?_
> > > > 
> > > > Ultimately, I just really want to know the behavior for when filters  
> > > > are run before queries and if there is a possibility to control the  
> > > > execution precedence.
> > > > 
> > > > Thank you!
> > > > 
> > > > Coyote Logistics, LLC is a licensed Property Broker (MC# 561135-B).  
> > > > [http://www.coyote.com/disclaimer/](http://www.coyote.com/disclaimer/)
> > > > 
> > > > --  
> > > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > > > 
> > > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > > msgid/elasticsearch/7c36c760-3b47-41a8-89c9-1da3a22ac153%  
> > > > [40googlegroups.com](http://40googlegroups.com).  
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> Coyote Logistics, LLC is a licensed Property Broker (MC# 561135-B).  
> [http://www.coyote.com/disclaimer/](http://www.coyote.com/disclaimer/)
> 
> --  
> 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/bbc991c6-b6f8-43c0-82be-906ced299e37%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bbc991c6-b6f8-43c0-82be-906ced299e37%40googlegroups.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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBuCjxUt0ShT%2BkvreZocTMnzhNEDwYMUnaWyeenGBc0Fw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBuCjxUt0ShT%2BkvreZocTMnzhNEDwYMUnaWyeenGBc0Fw%40mail.gmail.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, 1:48am UTC](https://discuss.elastic.co/t/how-to-specify-execution-order-of-filter-and-query/15880/8 "2017-07-06T01:48:55Z")

</div>


