# Filtering \*before\* a query

**URL:** <https://discuss.elastic.co/t/filtering-before-a-query/17763>\
**Category:** Elasticsearch\
**Created:** [May 27, 2014, 7:55pm UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763 "2014-05-27T19:55:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shawn\_O\_Banion](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Shawn\_O\_Banion](https://discuss.elastic.co/u/Shawn_O_Banion)\
**Post date:** [May 27, 2014, 7:55pm UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/1 "2014-05-27T19:55:15Z")

</div>

Hello,

This question relates to the order of execute of filters and queries.

I have two types of search criterion:

1. A "terms" query with a few hundred terms. \<------- (this is obviously  
a very expensive query)
2. A "geo\_bounding\_box" filter. \<------------------------(this should be  
fast and greatly reduce the result set)

Ideally, I would like to, first, use the "geo\_bounding\_box" filter to  
reduce the result set, and then query the filtered documents with the  
"terms" query.

Unfortunately, the search time does not appear to be affected by the size  
of the geographic boundary (i.e., the number of documents that match the  
filter). This makes me believe that it is executing the "terms" filter on  
the entire index before filtering.

I've tried the following different queries:

_Query #1 (average search time: 2.2s)_

{  
'query' : {  
'terms' : { 'text' : [...]}  
},  
'filter' : {  
'geo\_bounding\_box' : { ... }  
}  
}

From what I have read, a filter will execute _before_ a query if it is  
nested in a 'filtered' query (source[https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ](https://groups.google.com/forum/#!searchin/elasticsearch/filter%2420query%2420execution%2420order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ)).  
However, in my case, the average search time for Query #2 actually \*doubles  
\*for some reason:

_Query #2 (average search time: 4.4s)_

{  
'query' : {  
'filtered' : {  
'query' : {  
'terms' : { 'text' : [...]}  
},  
'filter' : {  
'geo\_bounding\_box' : { ... }  
}  
}  
}  
}

Any suggestions are appreciated on how I might execute the fast filter  
before the expensive terms query. Thank you!

Shawn

--  
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/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [May 27, 2014, 9:31pm UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/2 "2014-05-27T21:31:55Z")

</div>

I have never used the geo features, so I could be wrong, but I believe that  
geo filters are expensive are should be used as post filters:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

One of the reasons is that geo filters are not cached by default since they  
tend to be more dynamic. If your use case allows it, trying using a cached  
geo filter with the filtered query.

Cheers,

Ivan

On Tue, May 27, 2014 at 12:55 PM, Shawn O'Banion [shawn.obanion@gmail.com](mailto:shawn.obanion@gmail.com)wrote:

> Hello,
> 
> This question relates to the order of execute of filters and queries.
> 
> I have two types of search criterion:
> 
> 1. A "terms" query with a few hundred terms. \<------- (this is  
> obviously a very expensive query)
> 2. A "geo\_bounding\_box" filter. \<------------------------(this should  
> be fast and greatly reduce the result set)
> 
> Ideally, I would like to, first, use the "geo\_bounding\_box" filter to  
> reduce the result set, and then query the filtered documents with the  
> "terms" query.
> 
> Unfortunately, the search time does not appear to be affected by the size  
> of the geographic boundary (i.e., the number of documents that match the  
> filter). This makes me believe that it is executing the "terms" filter on  
> the entire index before filtering.
> 
> I've tried the following different queries:
> 
> _Query #1 (average search time: 2.2s)_
> 
> {  
> 'query' : {  
> 'terms' : { 'text' : [...]}  
> },  
> 'filter' : {  
> 'geo\_bounding\_box' : { ... }  
> }  
> }
> 
> From what I have read, a filter will execute _before_ a query if it is  
> nested in a 'filtered' query (source[https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ](https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ)).  
> However, in my case, the average search time for Query #2 actually \*doubles  
> \*for some reason:
> 
> _Query #2 (average search time: 4.4s)_
> 
> {  
> 'query' : {  
> 'filtered' : {  
> 'query' : {  
> 'terms' : { 'text' : [...]}  
> },  
> 'filter' : {  
> 'geo\_bounding\_box' : { ... }  
> }  
> }  
> }  
> }
> 
> Any suggestions are appreciated on how I might execute the fast filter  
> before the expensive terms query. Thank you!
> 
> Shawn
> 
> --  
> 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/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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%3DcQC90WNa5-1WyPjo3zrQhvuRsjpruBGdprOkw0ijCx7JFw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQC90WNa5-1WyPjo3zrQhvuRsjpruBGdprOkw0ijCx7JFw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Shawn\_O\_Banion](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Shawn\_O\_Banion](https://discuss.elastic.co/u/Shawn_O_Banion)\
**Post date:** [May 27, 2014, 9:56pm UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/3 "2014-05-27T21:56:33Z")

</div>

Ivan,

Thanks for the response.

Even if the geo filter is expensive, it is _certainly_ cheaper than the  
large terms query that I would like to execute _after_ the filter. And I  
don't believe that cacheing the filter result would be helpful because it  
is dynamic, as you said.

Shawn

On Tuesday, May 27, 2014 4:32:08 PM UTC-5, Ivan Brusic wrote:

> I have never used the geo features, so I could be wrong, but I believe  
> that geo filters are expensive are should be used as post filters:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-post-filter.html)
> 
> One of the reasons is that geo filters are not cached by default since  
> they tend to be more dynamic. If your use case allows it, trying using a  
> cached geo filter with the filtered query.
> 
> Cheers,
> 
> Ivan
> 
> On Tue, May 27, 2014 at 12:55 PM, Shawn O'Banion \<[shawn....@gmail.com](mailto:shawn....@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hello,
> > 
> > This question relates to the order of execute of filters and queries.
> > 
> > I have two types of search criterion:
> > 
> > 1. A "terms" query with a few hundred terms. \<------- (this is  
> > obviously a very expensive query)
> > 2. A "geo\_bounding\_box" filter. \<------------------------(this should  
> > be fast and greatly reduce the result set)
> > 
> > Ideally, I would like to, first, use the "geo\_bounding\_box" filter to  
> > reduce the result set, and then query the filtered documents with the  
> > "terms" query.
> > 
> > Unfortunately, the search time does not appear to be affected by the size  
> > of the geographic boundary (i.e., the number of documents that match the  
> > filter). This makes me believe that it is executing the "terms" filter on  
> > the entire index before filtering.
> > 
> > I've tried the following different queries:
> > 
> > _Query #1 (average search time: 2.2s)_
> > 
> > {  
> > 'query' : {  
> > 'terms' : { 'text' : [...]}  
> > },  
> > 'filter' : {  
> > 'geo\_bounding\_box' : { ... }  
> > }  
> > }
> > 
> > From what I have read, a filter will execute _before_ a query if it is  
> > nested in a 'filtered' query (source[https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ](https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ)).  
> > However, in my case, the average search time for Query #2 actually \*doubles  
> > \*for some reason:
> > 
> > _Query #2 (average search time: 4.4s)_
> > 
> > {  
> > 'query' : {  
> > 'filtered' : {  
> > 'query' : {  
> > 'terms' : { 'text' : [...]}  
> > },  
> > 'filter' : {  
> > 'geo\_bounding\_box' : { ... }  
> > }  
> > }  
> > }  
> > }
> > 
> > Any suggestions are appreciated on how I might execute the fast filter  
> > before the expensive terms query. Thank you!
> > 
> > Shawn
> > 
> > --  
> > 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/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 28, 2014, 7:44pm UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/4 "2014-05-28T19:44:11Z")

</div>

Hi Shawn,

You can force the strategy to use in `filtered_query`:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Tue, May 27, 2014 at 11:56 PM, Shawn O'Banion [shawn.obanion@gmail.com](mailto:shawn.obanion@gmail.com)wrote:

> Ivan,
> 
> Thanks for the response.
> 
> Even if the geo filter is expensive, it is _certainly_ cheaper than the  
> large terms query that I would like to execute _after_ the filter. And I  
> don't believe that cacheing the filter result would be helpful because it  
> is dynamic, as you said.
> 
> Shawn
> 
> On Tuesday, May 27, 2014 4:32:08 PM UTC-5, Ivan Brusic wrote:
> 
> > I have never used the geo features, so I could be wrong, but I believe  
> > that geo filters are expensive are should be used as post filters:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/)  
> > current/search-request-post-filter.html
> > 
> > One of the reasons is that geo filters are not cached by default since  
> > they tend to be more dynamic. If your use case allows it, trying using a  
> > cached geo filter with the filtered query.
> > 
> > Cheers,
> > 
> > Ivan
> > 
> > On Tue, May 27, 2014 at 12:55 PM, Shawn O'Banion [shawn....@gmail.com](mailto:shawn....@gmail.com)wrote:
> > 
> > > Hello,
> > > 
> > > This question relates to the order of execute of filters and queries.
> > > 
> > > I have two types of search criterion:
> > > 
> > > 1. A "terms" query with a few hundred terms. \<------- (this is  
> > > obviously a very expensive query)
> > > 2. A "geo\_bounding\_box" filter. \<------------------------(this  
> > > should be fast and greatly reduce the result set)
> > > 
> > > Ideally, I would like to, first, use the "geo\_bounding\_box" filter to  
> > > reduce the result set, and then query the filtered documents with the  
> > > "terms" query.
> > > 
> > > Unfortunately, the search time does not appear to be affected by the  
> > > size of the geographic boundary (i.e., the number of documents that match  
> > > the filter). This makes me believe that it is executing the "terms" filter  
> > > on the entire index before filtering.
> > > 
> > > I've tried the following different queries:
> > > 
> > > _Query #1 (average search time: 2.2s)_
> > > 
> > > {  
> > > 'query' : {  
> > > 'terms' : { 'text' : [...]}  
> > > },  
> > > 'filter' : {  
> > > 'geo\_bounding\_box' : { ... }  
> > > }  
> > > }
> > > 
> > > From what I have read, a filter will execute _before_ a query if it is  
> > > nested in a 'filtered' query (source[https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ](https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ)).  
> > > However, in my case, the average search time for Query #2 actually \*doubles  
> > > \*for some reason:
> > > 
> > > _Query #2 (average search time: 4.4s)_
> > > 
> > > {  
> > > 'query' : {  
> > > 'filtered' : {  
> > > 'query' : {  
> > > 'terms' : { 'text' : [...]}  
> > > },  
> > > 'filter' : {  
> > > 'geo\_bounding\_box' : { ... }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > Any suggestions are appreciated on how I might execute the fast filter  
> > > before the expensive terms query. Thank you!
> > > 
> > > Shawn
> > > 
> > > --  
> > > 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/25520ad2-68c8-4f26-81fc-75cdf2430d77%  
> > > [40googlegroups.com](http://40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .  
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > 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/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
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/CAL6Z4j5%3D9PDMHTC%3DHusyAmmZBjeSh3-5mQvu1eOEACaFvKM1oA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5%3D9PDMHTC%3DHusyAmmZBjeSh3-5mQvu1eOEACaFvKM1oA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Shawn\_O\_Banion](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Shawn\_O\_Banion](https://discuss.elastic.co/u/Shawn_O_Banion)\
**Post date:** [May 28, 2014, 7:56pm UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/5 "2014-05-28T19:56:58Z")

</div>

Adrian,

If you look at my first post, you'll see that I tried a filtered query, and  
it actually resulted in _worse_ performance. Regardless, the average search  
time did not vary by the size of the geographic boundary, which I take as  
evidence that it is _not_ filtering the results before executing the query.

Shawn

On Wednesday, May 28, 2014 2:44:19 PM UTC-5, Adrien Grand wrote:

> Hi Shawn,
> 
> You can force the strategy to use in `filtered_query`:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html#_filter_strategy)
> 
> On Tue, May 27, 2014 at 11:56 PM, Shawn O'Banion \<[shawn....@gmail.com](mailto:shawn....@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Ivan,
> > 
> > Thanks for the response.
> > 
> > Even if the geo filter is expensive, it is _certainly_ cheaper than the  
> > large terms query that I would like to execute _after_ the filter. And I  
> > don't believe that cacheing the filter result would be helpful because it  
> > is dynamic, as you said.
> > 
> > Shawn
> > 
> > On Tuesday, May 27, 2014 4:32:08 PM UTC-5, Ivan Brusic wrote:
> > 
> > > I have never used the geo features, so I could be wrong, but I believe  
> > > that geo filters are expensive are should be used as post filters:  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/)  
> > > current/search-request-post-filter.html
> > > 
> > > One of the reasons is that geo filters are not cached by default since  
> > > they tend to be more dynamic. If your use case allows it, trying using a  
> > > cached geo filter with the filtered query.
> > > 
> > > Cheers,
> > > 
> > > Ivan
> > > 
> > > On Tue, May 27, 2014 at 12:55 PM, Shawn O'Banion [shawn....@gmail.com](mailto:shawn....@gmail.com)wrote:
> > > 
> > > > Hello,
> > > > 
> > > > This question relates to the order of execute of filters and queries.
> > > > 
> > > > I have two types of search criterion:
> > > > 
> > > > 1. A "terms" query with a few hundred terms. \<------- (this is  
> > > > obviously a very expensive query)
> > > > 2. A "geo\_bounding\_box" filter. \<------------------------(this  
> > > > should be fast and greatly reduce the result set)
> > > > 
> > > > Ideally, I would like to, first, use the "geo\_bounding\_box" filter to  
> > > > reduce the result set, and then query the filtered documents with the  
> > > > "terms" query.
> > > > 
> > > > Unfortunately, the search time does not appear to be affected by the  
> > > > size of the geographic boundary (i.e., the number of documents that match  
> > > > the filter). This makes me believe that it is executing the "terms" filter  
> > > > on the entire index before filtering.
> > > > 
> > > > I've tried the following different queries:
> > > > 
> > > > _Query #1 (average search time: 2.2s)_
> > > > 
> > > > {  
> > > > 'query' : {  
> > > > 'terms' : { 'text' : [...]}  
> > > > },  
> > > > 'filter' : {  
> > > > 'geo\_bounding\_box' : { ... }  
> > > > }  
> > > > }
> > > > 
> > > > From what I have read, a filter will execute _before_ a query if it is  
> > > > nested in a 'filtered' query (source[https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ](https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ)).  
> > > > However, in my case, the average search time for Query #2 actually \*doubles  
> > > > \*for some reason:
> > > > 
> > > > _Query #2 (average search time: 4.4s)_
> > > > 
> > > > {  
> > > > 'query' : {  
> > > > 'filtered' : {  
> > > > 'query' : {  
> > > > 'terms' : { 'text' : [...]}  
> > > > },  
> > > > 'filter' : {  
> > > > 'geo\_bounding\_box' : { ... }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > Any suggestions are appreciated on how I might execute the fast filter  
> > > > before the expensive terms query. Thank you!
> > > > 
> > > > Shawn
> > > > 
> > > > --  
> > > > 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/25520ad2-68c8-4f26-81fc-75cdf2430d77%  
> > > > [40googlegroups.com](http://40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > .  
> > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > 
> > > --  
> > > 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/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Adrien Grand

--  
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/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [May 28, 2014, 9:21pm UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/6 "2014-05-28T21:21:57Z")

</div>

Why do you think that geo\_bounding\_box should be fast? Since the filter is  
not cached, it needs to run on every document in the index.

--  
Ivan

On Wed, May 28, 2014 at 12:56 PM, Shawn O'Banion [shawn.obanion@gmail.com](mailto:shawn.obanion@gmail.com)wrote:

> Adrian,
> 
> If you look at my first post, you'll see that I tried a filtered query,  
> and it actually resulted in _worse_ performance. Regardless, the average  
> search time did not vary by the size of the geographic boundary, which I  
> take as evidence that it is _not_ filtering the results before executing  
> the query.
> 
> Shawn
> 
> On Wednesday, May 28, 2014 2:44:19 PM UTC-5, Adrien Grand wrote:
> 
> > Hi Shawn,
> > 
> > You can force the strategy to use in `filtered_query`:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/)  
> > reference/current/query-dsl-filtered-query.html#\_filter\_strategy
> > 
> > On Tue, May 27, 2014 at 11:56 PM, Shawn O'Banion [shawn....@gmail.com](mailto:shawn....@gmail.com)wrote:
> > 
> > > Ivan,
> > > 
> > > Thanks for the response.
> > > 
> > > Even if the geo filter is expensive, it is _certainly_ cheaper than the  
> > > large terms query that I would like to execute _after_ the filter. And  
> > > I don't believe that cacheing the filter result would be helpful because it  
> > > is dynamic, as you said.
> > > 
> > > Shawn
> > > 
> > > On Tuesday, May 27, 2014 4:32:08 PM UTC-5, Ivan Brusic wrote:
> > > 
> > > > I have never used the geo features, so I could be wrong, but I believe  
> > > > that geo filters are expensive are should be used as post filters:  
> > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/)  
> > > > current/search-request-post-filter.html
> > > > 
> > > > One of the reasons is that geo filters are not cached by default since  
> > > > they tend to be more dynamic. If your use case allows it, trying using a  
> > > > cached geo filter with the filtered query.
> > > > 
> > > > Cheers,
> > > > 
> > > > Ivan
> > > > 
> > > > On Tue, May 27, 2014 at 12:55 PM, Shawn O'Banion [shawn....@gmail.com](mailto:shawn....@gmail.com)wrote:
> > > > 
> > > > > Hello,
> > > > > 
> > > > > This question relates to the order of execute of filters and queries.
> > > > > 
> > > > > I have two types of search criterion:
> > > > > 
> > > > > 1. A "terms" query with a few hundred terms. \<------- (this is  
> > > > > obviously a very expensive query)
> > > > > 2. A "geo\_bounding\_box" filter. \<------------------------(this  
> > > > > should be fast and greatly reduce the result set)
> > > > > 
> > > > > Ideally, I would like to, first, use the "geo\_bounding\_box" filter to  
> > > > > reduce the result set, and then query the filtered documents with the  
> > > > > "terms" query.
> > > > > 
> > > > > Unfortunately, the search time does not appear to be affected by the  
> > > > > size of the geographic boundary (i.e., the number of documents that match  
> > > > > the filter). This makes me believe that it is executing the "terms" filter  
> > > > > on the entire index before filtering.
> > > > > 
> > > > > I've tried the following different queries:
> > > > > 
> > > > > _Query #1 (average search time: 2.2s)_
> > > > > 
> > > > > {  
> > > > > 'query' : {  
> > > > > 'terms' : { 'text' : [...]}  
> > > > > },  
> > > > > 'filter' : {  
> > > > > 'geo\_bounding\_box' : { ... }  
> > > > > }  
> > > > > }
> > > > > 
> > > > > From what I have read, a filter will execute _before_ a query if it  
> > > > > is nested in a 'filtered' query (source[https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ](https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ)).  
> > > > > However, in my case, the average search time for Query #2 actually \*doubles  
> > > > > \*for some reason:
> > > > > 
> > > > > _Query #2 (average search time: 4.4s)_
> > > > > 
> > > > > {  
> > > > > 'query' : {  
> > > > > 'filtered' : {  
> > > > > 'query' : {  
> > > > > 'terms' : { 'text' : [...]}  
> > > > > },  
> > > > > 'filter' : {  
> > > > > 'geo\_bounding\_box' : { ... }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }
> > > > > 
> > > > > Any suggestions are appreciated on how I might execute the fast filter  
> > > > > before the expensive terms query. Thank you!
> > > > > 
> > > > > Shawn
> > > > > 
> > > > > --  
> > > > > 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/25520ad2-68c8-4f26-81fc-75cdf2430d77%40goo  
> > > > > [glegroups.com](http://glegroups.com)[https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > .  
> > > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > > 
> > > > --  
> > > > 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/c44d9170-2be3-43b7-a765-44d953823219%  
> > > > [40googlegroups.com](http://40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > .
> > > 
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > Adrien Grand
> 
> --  
> 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/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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%3DcQBC2J\_C%3DY\_V0Go%2BgC0aabDs0RScyQ6RSEU%2Bs5XjNOXbAg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBC2J_C%3DY_V0Go%2BgC0aabDs0RScyQ6RSEU%2Bs5XjNOXbAg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Shawn\_O\_Banion](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Shawn\_O\_Banion](https://discuss.elastic.co/u/Shawn_O_Banion)\
**Post date:** [May 28, 2014, 9:29pm UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/7 "2014-05-28T21:29:28Z")

</div>

Because when I remove the terms query with just the geo\_bounding\_box filter  
remaining, the search time is around 400ms on average. If I add the terms  
query, it is around 2200ms on average.

Again, _relative_ to the terms query (which has 100's of terms), it is very  
cheap.

My question is: how do I execute the geo\_bounding\_box filter _before_ executing  
the terms query so that I reduce the number of documents that I have to  
query over.

On Wednesday, May 28, 2014 4:22:05 PM UTC-5, Ivan Brusic wrote:

> Why do you think that geo\_bounding\_box should be fast? Since the filter is  
> not cached, it needs to run on every document in the index.
> 
> --  
> Ivan
> 
> On Wed, May 28, 2014 at 12:56 PM, Shawn O'Banion \<[shawn....@gmail.com](mailto:shawn....@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Adrian,
> > 
> > If you look at my first post, you'll see that I tried a filtered query,  
> > and it actually resulted in _worse_ performance. Regardless, the average  
> > search time did not vary by the size of the geographic boundary, which I  
> > take as evidence that it is _not_ filtering the results before executing  
> > the query.
> > 
> > Shawn
> > 
> > On Wednesday, May 28, 2014 2:44:19 PM UTC-5, Adrien Grand wrote:
> > 
> > > Hi Shawn,
> > > 
> > > You can force the strategy to use in `filtered_query`:  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/)  
> > > reference/current/query-dsl-filtered-query.html#\_filter\_strategy
> > > 
> > > On Tue, May 27, 2014 at 11:56 PM, Shawn O'Banion [shawn....@gmail.com](mailto:shawn....@gmail.com)wrote:
> > > 
> > > > Ivan,
> > > > 
> > > > Thanks for the response.
> > > > 
> > > > Even if the geo filter is expensive, it is _certainly_ cheaper than  
> > > > the large terms query that I would like to execute _after_ the filter.  
> > > > And I don't believe that cacheing the filter result would be helpful  
> > > > because it is dynamic, as you said.
> > > > 
> > > > Shawn
> > > > 
> > > > On Tuesday, May 27, 2014 4:32:08 PM UTC-5, Ivan Brusic wrote:
> > > > 
> > > > > I have never used the geo features, so I could be wrong, but I believe  
> > > > > that geo filters are expensive are should be used as post filters:  
> > > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/)  
> > > > > current/search-request-post-filter.html
> > > > > 
> > > > > One of the reasons is that geo filters are not cached by default since  
> > > > > they tend to be more dynamic. If your use case allows it, trying using a  
> > > > > cached geo filter with the filtered query.
> > > > > 
> > > > > Cheers,
> > > > > 
> > > > > Ivan
> > > > > 
> > > > > On Tue, May 27, 2014 at 12:55 PM, Shawn O'Banion [shawn....@gmail.com](mailto:shawn....@gmail.com)wrote:
> > > > > 
> > > > > > Hello,
> > > > > > 
> > > > > > This question relates to the order of execute of filters and queries.
> > > > > > 
> > > > > > I have two types of search criterion:
> > > > > > 
> > > > > > 1. A "terms" query with a few hundred terms. \<------- (this is  
> > > > > > obviously a very expensive query)
> > > > > > 2. A "geo\_bounding\_box" filter. \<------------------------(this  
> > > > > > should be fast and greatly reduce the result set)
> > > > > > 
> > > > > > Ideally, I would like to, first, use the "geo\_bounding\_box" filter to  
> > > > > > reduce the result set, and then query the filtered documents with the  
> > > > > > "terms" query.
> > > > > > 
> > > > > > Unfortunately, the search time does not appear to be affected by the  
> > > > > > size of the geographic boundary (i.e., the number of documents that match  
> > > > > > the filter). This makes me believe that it is executing the "terms" filter  
> > > > > > on the entire index before filtering.
> > > > > > 
> > > > > > I've tried the following different queries:
> > > > > > 
> > > > > > _Query #1 (average search time: 2.2s)_
> > > > > > 
> > > > > > {  
> > > > > > 'query' : {  
> > > > > > 'terms' : { 'text' : [...]}  
> > > > > > },  
> > > > > > 'filter' : {  
> > > > > > 'geo\_bounding\_box' : { ... }  
> > > > > > }  
> > > > > > }
> > > > > > 
> > > > > > From what I have read, a filter will execute _before_ a query if it  
> > > > > > is nested in a 'filtered' query (source[https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ](https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ)).  
> > > > > > However, in my case, the average search time for Query #2 actually \*doubles  
> > > > > > \*for some reason:
> > > > > > 
> > > > > > _Query #2 (average search time: 4.4s)_
> > > > > > 
> > > > > > {  
> > > > > > 'query' : {  
> > > > > > 'filtered' : {  
> > > > > > 'query' : {  
> > > > > > 'terms' : { 'text' : [...]}  
> > > > > > },  
> > > > > > 'filter' : {  
> > > > > > 'geo\_bounding\_box' : { ... }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }
> > > > > > 
> > > > > > Any suggestions are appreciated on how I might execute the fast  
> > > > > > filter before the expensive terms query. Thank you!
> > > > > > 
> > > > > > Shawn
> > > > > > 
> > > > > > --  
> > > > > > 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/25520ad2-68c8-4f26-81fc-75cdf2430d77%40goo  
> > > > > > [glegroups.com](http://glegroups.com)[https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > > .  
> > > > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > > > 
> > > > > --  
> > > > > 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/c44d9170-2be3-43b7-a765-44d953823219%  
> > > > > [40googlegroups.com](http://40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > .
> > > > 
> > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > 
> > > --  
> > > Adrien Grand
> > 
> > --  
> > 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/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/dc53c86c-c747-4df7-b0ae-9df3857daad6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dc53c86c-c747-4df7-b0ae-9df3857daad6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [May 28, 2014, 9:56pm UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/8 "2014-05-28T21:56:02Z")

</div>

As I mentioned initially, I have no experience with the geo features, so I  
am just guessing regarding performance.

So a filtered query with a match all query and the geo filter is only  
400ms? What about just the term query? Not sure how you are executing  
tests, but you should make sure all the caches are warmed and ready to go.  
Initial caching can wreck havoc on queries. Are the field data and filter  
caches constant during the queries?

--  
Ivan

On Wed, May 28, 2014 at 2:29 PM, Shawn O'Banion [shawn.obanion@gmail.com](mailto:shawn.obanion@gmail.com)wrote:

> Because when I remove the terms query with just the geo\_bounding\_box  
> filter remaining, the search time is around 400ms on average. If I add the  
> terms query, it is around 2200ms on average.
> 
> Again, _relative_ to the terms query (which has 100's of terms), it is  
> very cheap.
> 
> My question is: how do I execute the geo\_bounding\_box filter _before_ executing  
> the terms query so that I reduce the number of documents that I have to  
> query over.
> 
> On Wednesday, May 28, 2014 4:22:05 PM UTC-5, Ivan Brusic wrote:
> 
> > Why do you think that geo\_bounding\_box should be fast? Since the filter  
> > is not cached, it needs to run on every document in the index.
> > 
> > --  
> > Ivan
> > 
> > On Wed, May 28, 2014 at 12:56 PM, Shawn O'Banion [shawn....@gmail.com](mailto:shawn....@gmail.com)wrote:
> > 
> > > Adrian,
> > > 
> > > If you look at my first post, you'll see that I tried a filtered query,  
> > > and it actually resulted in _worse_ performance. Regardless, the  
> > > average search time did not vary by the size of the geographic boundary,  
> > > which I take as evidence that it is _not_ filtering the results before  
> > > executing the query.
> > > 
> > > Shawn
> > > 
> > > On Wednesday, May 28, 2014 2:44:19 PM UTC-5, Adrien Grand wrote:
> > > 
> > > > Hi Shawn,
> > > > 
> > > > You can force the strategy to use in `filtered_query`:  
> > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/referenc)  
> > > > e/current/query-dsl-filtered-query.html#\_filter\_strategy
> > > > 
> > > > On Tue, May 27, 2014 at 11:56 PM, Shawn O'Banion [shawn....@gmail.com](mailto:shawn....@gmail.com)wrote:
> > > > 
> > > > > Ivan,
> > > > > 
> > > > > Thanks for the response.
> > > > > 
> > > > > Even if the geo filter is expensive, it is _certainly_ cheaper than  
> > > > > the large terms query that I would like to execute _after_ the  
> > > > > filter. And I don't believe that cacheing the filter result would be  
> > > > > helpful because it is dynamic, as you said.
> > > > > 
> > > > > Shawn
> > > > > 
> > > > > On Tuesday, May 27, 2014 4:32:08 PM UTC-5, Ivan Brusic wrote:
> > > > > 
> > > > > > I have never used the geo features, so I could be wrong, but I  
> > > > > > believe that geo filters are expensive are should be used as post filters:  
> > > > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/)  
> > > > > > current/search-request-post-filter.html
> > > > > > 
> > > > > > One of the reasons is that geo filters are not cached by default  
> > > > > > since they tend to be more dynamic. If your use case allows it, trying  
> > > > > > using a cached geo filter with the filtered query.
> > > > > > 
> > > > > > Cheers,
> > > > > > 
> > > > > > Ivan
> > > > > > 
> > > > > > On Tue, May 27, 2014 at 12:55 PM, Shawn O'Banion \<[shawn....@gmail.com](mailto:shawn....@gmail.com)
> > > > > > 
> > > > > > > wrote:
> > > > > > 
> > > > > > > Hello,
> > > > > > > 
> > > > > > > This question relates to the order of execute of filters and queries.
> > > > > > > 
> > > > > > > I have two types of search criterion:
> > > > > > > 
> > > > > > > 1. A "terms" query with a few hundred terms. \<------- (this is  
> > > > > > > obviously a very expensive query)
> > > > > > > 2. A "geo\_bounding\_box" filter. \<------------------------(this  
> > > > > > > should be fast and greatly reduce the result set)
> > > > > > > 
> > > > > > > Ideally, I would like to, first, use the "geo\_bounding\_box" filter  
> > > > > > > to reduce the result set, and then query the filtered documents with the  
> > > > > > > "terms" query.
> > > > > > > 
> > > > > > > Unfortunately, the search time does not appear to be affected by the  
> > > > > > > size of the geographic boundary (i.e., the number of documents that match  
> > > > > > > the filter). This makes me believe that it is executing the "terms" filter  
> > > > > > > on the entire index before filtering.
> > > > > > > 
> > > > > > > I've tried the following different queries:
> > > > > > > 
> > > > > > > _Query #1 (average search time: 2.2s)_
> > > > > > > 
> > > > > > > {  
> > > > > > > 'query' : {  
> > > > > > > 'terms' : { 'text' : [...]}  
> > > > > > > },  
> > > > > > > 'filter' : {  
> > > > > > > 'geo\_bounding\_box' : { ... }  
> > > > > > > }  
> > > > > > > }
> > > > > > > 
> > > > > > > From what I have read, a filter will execute _before_ a query if it  
> > > > > > > is nested in a 'filtered' query (source[https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ](https://groups.google.com/forum/#!searchin/elasticsearch/filter$20query$20execution$20order/elasticsearch/aL9fzeyTBKE/8vVKuJaNvWUJ)).  
> > > > > > > However, in my case, the average search time for Query #2 actually \*doubles  
> > > > > > > \*for some reason:
> > > > > > > 
> > > > > > > _Query #2 (average search time: 4.4s)_
> > > > > > > 
> > > > > > > {  
> > > > > > > 'query' : {  
> > > > > > > 'filtered' : {  
> > > > > > > 'query' : {  
> > > > > > > 'terms' : { 'text' : [...]}  
> > > > > > > },  
> > > > > > > 'filter' : {  
> > > > > > > 'geo\_bounding\_box' : { ... }  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }
> > > > > > > 
> > > > > > > Any suggestions are appreciated on how I might execute the fast  
> > > > > > > filter before the expensive terms query. Thank you!
> > > > > > > 
> > > > > > > Shawn
> > > > > > > 
> > > > > > > --  
> > > > > > > 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/25520ad2-68c](https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c)  
> > > > > > > 8-4f26-81fc-75cdf2430d77%[40googlegroups.com](http://40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/25520ad2-68c8-4f26-81fc-75cdf2430d77%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > > > .  
> > > > > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > > > > 
> > > > > > --  
> > > > > > 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/c44d9170-2be3-43b7-a765-44d953823219%40goo  
> > > > > > [glegroups.com](http://glegroups.com)[https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/c44d9170-2be3-43b7-a765-44d953823219%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > > .
> > > > > 
> > > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > > 
> > > > --  
> > > > Adrien Grand
> > > 
> > > --  
> > > 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/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%  
> > > [40googlegroups.com](http://40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/6d3d2819-7f0f-48b1-b1aa-b2f8e198a95d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .
> > > 
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > 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/dc53c86c-c747-4df7-b0ae-9df3857daad6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dc53c86c-c747-4df7-b0ae-9df3857daad6%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/dc53c86c-c747-4df7-b0ae-9df3857daad6%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/dc53c86c-c747-4df7-b0ae-9df3857daad6%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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%3DcQA-\_20UR1orPwP%2BgkCZ1DChLv5iKZA\_PzNwSKX8KN%2BWAA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQA-_20UR1orPwP%2BgkCZ1DChLv5iKZA_PzNwSKX8KN%2BWAA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 30, 2014, 6:05am UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/9 "2014-05-30T06:05:12Z")

</div>

Hi Shawn,

On Wed, May 28, 2014 at 11:29 PM, Shawn O'Banion [shawn.obanion@gmail.com](mailto:shawn.obanion@gmail.com)  
wrote:

> My question is: how do I execute the geo\_bounding\_box filter _before_ executing  
> the terms query so that I reduce the number of documents that I have to  
> query over.

This is why I pointed out the link about filter strategies: whether the  
query is applied before, at the same time or after the filter can be  
controlled using the `strategy` parameter of `filtered_query`:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

You can try them out to see how they influence response times.

You might also want to try out the `indexed` type for the geo bounding  
filter that might be faster than the default `memory` if your filter is  
very selective:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

--  
Adrien Grand

--  
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/CAL6Z4j61-at7r\_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j61-at7r_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Shawn\_O\_Banion](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Shawn\_O\_Banion](https://discuss.elastic.co/u/Shawn_O_Banion)\
**Post date:** [May 30, 2014, 4:37pm UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/10 "2014-05-30T16:37:59Z")

</div>

Hi Adrien,

Thanks. This sounds like what I need, however the page you link to does not  
discuss the 'strategy' parameter that you mention.

I see some documentation about a filter strategy with Lucene (  
[FilteredQuery.FilterStrategy (Lucene 4.1.0 API)](http://lucene.apache.org/core/4_1_0/core/org/apache/lucene/search/FilteredQuery.FilterStrategy.html))  
but I'm not sure how to specify the execution order or filter strategy in  
an Elasticsearch query DSL.

Thanks,  
Shawn

On Fri, May 30, 2014 at 1:05 AM, Adrien Grand \<  
[adrien.grand@elasticsearch.com](mailto:adrien.grand@elasticsearch.com)\> wrote:

> Hi Shawn,
> 
> On Wed, May 28, 2014 at 11:29 PM, Shawn O'Banion [shawn.obanion@gmail.com](mailto:shawn.obanion@gmail.com)  
> wrote:
> 
> > My question is: how do I execute the geo\_bounding\_box filter _before_ executing  
> > the terms query so that I reduce the number of documents that I have to  
> > query over.
> 
> This is why I pointed out the link about filter strategies: whether the  
> query is applied before, at the same time or after the filter can be  
> controlled using the `strategy` parameter of `filtered_query`:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html#_filter_strategy)
> 
> You can try them out to see how they influence response times.
> 
> You might also want to try out the `indexed` type for the geo bounding  
> filter that might be faster than the default `memory` if your filter is  
> very selective:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-filter.html#_type_2)
> 
> --  
> Adrien Grand
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/M34JeoP4r3o/unsubscribe](https://groups.google.com/d/topic/elasticsearch/M34JeoP4r3o/unsubscribe).  
> To unsubscribe from this group and all its topics, 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/CAL6Z4j61-at7r\_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j61-at7r_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j61-at7r\_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j61-at7r_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CADC-RJbhHqKT\_Ue25KEgMxBeGTfgSYQL1%3D8EViLm06PdxHa-eA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CADC-RJbhHqKT_Ue25KEgMxBeGTfgSYQL1%3D8EViLm06PdxHa-eA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [May 30, 2014, 4:45pm UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/11 "2014-05-30T16:45:10Z")

</div>

For some reason, when I viewed that page at work, I was not seeing the  
strategy section either, but I was able to at home. Try refreshing the  
page, worked for me.

Perhaps I should play around with those settings the next time I fine tune  
my queries. I use a combination of both pre and post filters.

--  
Ivan

On Fri, May 30, 2014 at 9:37 AM, Shawn O'Banion [shawn.obanion@gmail.com](mailto:shawn.obanion@gmail.com)  
wrote:

> Hi Adrien,
> 
> Thanks. This sounds like what I need, however the page you link to does  
> not discuss the 'strategy' parameter that you mention.
> 
> I see some documentation about a filter strategy with Lucene (  
> [FilteredQuery.FilterStrategy (Lucene 4.1.0 API)](http://lucene.apache.org/core/4_1_0/core/org/apache/lucene/search/FilteredQuery.FilterStrategy.html))  
> but I'm not sure how to specify the execution order or filter strategy in  
> an Elasticsearch query DSL.
> 
> Thanks,  
> Shawn
> 
> On Fri, May 30, 2014 at 1:05 AM, Adrien Grand \<  
> [adrien.grand@elasticsearch.com](mailto:adrien.grand@elasticsearch.com)\> wrote:
> 
> > Hi Shawn,
> > 
> > On Wed, May 28, 2014 at 11:29 PM, Shawn O'Banion \<[shawn.obanion@gmail.com](mailto:shawn.obanion@gmail.com)
> > 
> > > wrote:
> > 
> > > My question is: how do I execute the geo\_bounding\_box filter _before_ executing  
> > > the terms query so that I reduce the number of documents that I have to  
> > > query over.
> > 
> > This is why I pointed out the link about filter strategies: whether the  
> > query is applied before, at the same time or after the filter can be  
> > controlled using the `strategy` parameter of `filtered_query`:
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html#_filter_strategy)
> > 
> > You can try them out to see how they influence response times.
> > 
> > You might also want to try out the `indexed` type for the geo bounding  
> > filter that might be faster than the default `memory` if your filter is  
> > very selective:
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-filter.html#_type_2)
> > 
> > --  
> > Adrien Grand
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/M34JeoP4r3o/unsubscribe](https://groups.google.com/d/topic/elasticsearch/M34JeoP4r3o/unsubscribe).  
> > To unsubscribe from this group and all its topics, 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/CAL6Z4j61-at7r\_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j61-at7r_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j61-at7r\_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j61-at7r_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> 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/CADC-RJbhHqKT\_Ue25KEgMxBeGTfgSYQL1%3D8EViLm06PdxHa-eA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CADC-RJbhHqKT_Ue25KEgMxBeGTfgSYQL1%3D8EViLm06PdxHa-eA%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CADC-RJbhHqKT\_Ue25KEgMxBeGTfgSYQL1%3D8EViLm06PdxHa-eA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CADC-RJbhHqKT_Ue25KEgMxBeGTfgSYQL1%3D8EViLm06PdxHa-eA%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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%3DcQChpaOA3MmY58SzWgu%3Dqy92W\_xPb5C%3D7zS5cvEFPta4iw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQChpaOA3MmY58SzWgu%3Dqy92W_xPb5C%3D7zS5cvEFPta4iw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Shawn\_O\_Banion](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Shawn\_O\_Banion](https://discuss.elastic.co/u/Shawn_O_Banion)\
**Post date:** [May 30, 2014, 7:52pm UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/12 "2014-05-30T19:52:18Z")

</div>

Interesting, when I hit refresh I can see the strategy section.

Nevertheless, it seems that when I set the 'strategy' equal to  
'leap\_frog\_filter\_first' I achieve better performance. This seems to be  
what I needed. Thank you!

On Fri, May 30, 2014 at 11:45 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> For some reason, when I viewed that page at work, I was not seeing the  
> strategy section either, but I was able to at home. Try refreshing the  
> page, worked for me.
> 
> Perhaps I should play around with those settings the next time I fine tune  
> my queries. I use a combination of both pre and post filters.
> 
> --  
> Ivan
> 
> On Fri, May 30, 2014 at 9:37 AM, Shawn O'Banion [shawn.obanion@gmail.com](mailto:shawn.obanion@gmail.com)  
> wrote:
> 
> > Hi Adrien,
> > 
> > Thanks. This sounds like what I need, however the page you link to does  
> > not discuss the 'strategy' parameter that you mention.
> > 
> > I see some documentation about a filter strategy with Lucene (  
> > [FilteredQuery.FilterStrategy (Lucene 4.1.0 API)](http://lucene.apache.org/core/4_1_0/core/org/apache/lucene/search/FilteredQuery.FilterStrategy.html))  
> > but I'm not sure how to specify the execution order or filter strategy in  
> > an Elasticsearch query DSL.
> > 
> > Thanks,  
> > Shawn
> > 
> > On Fri, May 30, 2014 at 1:05 AM, Adrien Grand \<  
> > [adrien.grand@elasticsearch.com](mailto:adrien.grand@elasticsearch.com)\> wrote:
> > 
> > > Hi Shawn,
> > > 
> > > On Wed, May 28, 2014 at 11:29 PM, Shawn O'Banion \<  
> > > [shawn.obanion@gmail.com](mailto:shawn.obanion@gmail.com)\> wrote:
> > > 
> > > > My question is: how do I execute the geo\_bounding\_box filter _before_ executing  
> > > > the terms query so that I reduce the number of documents that I have to  
> > > > query over.
> > > 
> > > This is why I pointed out the link about filter strategies: whether the  
> > > query is applied before, at the same time or after the filter can be  
> > > controlled using the `strategy` parameter of `filtered_query`:
> > > 
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html#_filter_strategy)
> > > 
> > > You can try them out to see how they influence response times.
> > > 
> > > You might also want to try out the `indexed` type for the geo bounding  
> > > filter that might be faster than the default `memory` if your filter is  
> > > very selective:
> > > 
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-filter.html#_type_2)
> > > 
> > > --  
> > > Adrien Grand
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/M34JeoP4r3o/unsubscribe](https://groups.google.com/d/topic/elasticsearch/M34JeoP4r3o/unsubscribe).  
> > > To unsubscribe from this group and all its topics, 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/CAL6Z4j61-at7r\_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j61-at7r_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j61-at7r\_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j61-at7r_P2HRhnE473sZJzX5ZbWov76o6SYJ1n87C72g%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> > > .
> > > 
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > 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/CADC-RJbhHqKT\_Ue25KEgMxBeGTfgSYQL1%3D8EViLm06PdxHa-eA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CADC-RJbhHqKT_Ue25KEgMxBeGTfgSYQL1%3D8EViLm06PdxHa-eA%40mail.gmail.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/CADC-RJbhHqKT\_Ue25KEgMxBeGTfgSYQL1%3D8EViLm06PdxHa-eA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CADC-RJbhHqKT_Ue25KEgMxBeGTfgSYQL1%3D8EViLm06PdxHa-eA%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/M34JeoP4r3o/unsubscribe](https://groups.google.com/d/topic/elasticsearch/M34JeoP4r3o/unsubscribe).  
> To unsubscribe from this group and all its topics, 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%3DcQChpaOA3MmY58SzWgu%3Dqy92W\_xPb5C%3D7zS5cvEFPta4iw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQChpaOA3MmY58SzWgu%3Dqy92W_xPb5C%3D7zS5cvEFPta4iw%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQChpaOA3MmY58SzWgu%3Dqy92W\_xPb5C%3D7zS5cvEFPta4iw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQChpaOA3MmY58SzWgu%3Dqy92W_xPb5C%3D7zS5cvEFPta4iw%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CADC-RJazgs7ORnuuqVYkXqZuaYyAWLCVynkQkY0dtry7KfpJzg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CADC-RJazgs7ORnuuqVYkXqZuaYyAWLCVynkQkY0dtry7KfpJzg%40mail.gmail.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, 1:25am UTC](https://discuss.elastic.co/t/filtering-before-a-query/17763/13 "2017-07-06T01:25:39Z")

</div>


