# Filters: odd behavior

**URL:** <https://discuss.elastic.co/t/filters-odd-behavior/20284>\
**Category:** Elasticsearch\
**Created:** [October 16, 2014, 12:16pm UTC](https://discuss.elastic.co/t/filters-odd-behavior/20284 "2014-10-16T12:16:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mromagnoli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mromagnoli/32/839_2.png) [@mromagnoli](https://discuss.elastic.co/u/mromagnoli)\
**Post date:** [October 16, 2014, 12:16pm UTC](https://discuss.elastic.co/t/filters-odd-behavior/20284/1 "2014-10-16T12:16:43Z")

</div>

Hi everyone,  
I have a doubt about Filters.

If I have more than one filter, in a filtered query, are they executed in  
the defined order? And, are they filtering in a 'chain' mode, i.e. using  
the results of the previous filters?

Thanks in advance as always.

--  
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/d528067f-5042-4667-bcbc-38dcde87010a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d528067f-5042-4667-bcbc-38dcde87010a%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:** [October 17, 2014, 8:27pm UTC](https://discuss.elastic.co/t/filters-odd-behavior/20284/2 "2014-10-17T20:27:06Z")

</div>

They are indeed executed in the defined order. Filters that are more  
specific should be placed early on and those that cannot be cached  
(geo/timebased) should be placed last.

Cheers,

Ivan

On Thu, Oct 16, 2014 at 5:16 AM, @mromagnoli [marce.romagnoli@gmail.com](mailto:marce.romagnoli@gmail.com)  
wrote:

> Hi everyone,  
> I have a doubt about Filters.
> 
> If I have more than one filter, in a filtered query, are they executed in  
> the defined order? And, are they filtering in a 'chain' mode, i.e. using  
> the results of the previous filters?
> 
> Thanks in advance as always.
> 
> --  
> 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/d528067f-5042-4667-bcbc-38dcde87010a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d528067f-5042-4667-bcbc-38dcde87010a%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/d528067f-5042-4667-bcbc-38dcde87010a%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/d528067f-5042-4667-bcbc-38dcde87010a%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%3DcQASJatGfPg2kP%3D8soiHvvxKDZKJ6qkK0FyfZT4B2x\_7Qw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQASJatGfPg2kP%3D8soiHvvxKDZKJ6qkK0FyfZT4B2x_7Qw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Alexandre\_Rafalovitc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexandre_rafalovitc/32/1124_2.png) [@Alexandre\_Rafalovitc](https://discuss.elastic.co/u/Alexandre_Rafalovitc)\
**Post date:** [October 17, 2014, 9:22pm UTC](https://discuss.elastic.co/t/filters-odd-behavior/20284/3 "2014-10-17T21:22:54Z")

</div>

And there is post-filter as well:

> **[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.

Regards,  
Alex.

Personal: [http://www.outerthoughts.com/](http://www.outerthoughts.com/) and @arafalov  
Solr resources and newsletter: [http://www.solr-start.com/](http://www.solr-start.com/) and @solrstart  
Solr popularizers community: [Sign Up | LinkedIn](https://www.linkedin.com/groups?gid=6713853)

On 17 October 2014 16:27, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> They are indeed executed in the defined order. Filters that are more  
> specific should be placed early on and those that cannot be cached  
> (geo/timebased) should be placed last.
> 
> Cheers,
> 
> Ivan
> 
> On Thu, Oct 16, 2014 at 5:16 AM, @mromagnoli [marce.romagnoli@gmail.com](mailto:marce.romagnoli@gmail.com)  
> wrote:
> 
> > Hi everyone,  
> > I have a doubt about Filters.
> > 
> > If I have more than one filter, in a filtered query, are they executed in  
> > the defined order? And, are they filtering in a 'chain' mode, i.e. using the  
> > results of the previous filters?
> > 
> > Thanks in advance as always.
> > 
> > --  
> > 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/d528067f-5042-4667-bcbc-38dcde87010a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d528067f-5042-4667-bcbc-38dcde87010a%40googlegroups.com).  
> > 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%3DcQASJatGfPg2kP%3D8soiHvvxKDZKJ6qkK0FyfZT4B2x\_7Qw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQASJatGfPg2kP%3D8soiHvvxKDZKJ6qkK0FyfZT4B2x_7Qw%40mail.gmail.com).
> 
> 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/CAEFAe-GLae4nZ7hFLxfGs9tQ%2BC2SD%2Bvr7CsXv97sFcwEcsnpTg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAEFAe-GLae4nZ7hFLxfGs9tQ%2BC2SD%2Bvr7CsXv97sFcwEcsnpTg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![mromagnoli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mromagnoli/32/839_2.png) [@mromagnoli](https://discuss.elastic.co/u/mromagnoli)\
**Post date:** [October 18, 2014, 2:04pm UTC](https://discuss.elastic.co/t/filters-odd-behavior/20284/4 "2014-10-18T14:04:14Z")

</div>

Thanks guys for your responses.

My question was due an strange behavior when using 'not' and 'geo\_distance'  
filters.

I want to filter some results that have undesirable words, such as 'sex',  
'xxx', etc... And then geo filter those good results, but if I place 'not'  
filter first, then when geo filter is applied, it retrieves results with  
that not wanted words in it. If I place geo filter first, and then 'not'  
filter, geo filter seems not to be executed or something, because results  
are not accurate for that filter, instead, they still being good results  
with no bad words.

I am using it like this:

{  
"query": {  
"filtered": {  
"query": {...},  
"filter": [  
[{  
"geo\_distance": {  
"distance": "30km",  
"Location.location": {  
"lat": "-32.890183",  
"lon": "-68.844050"  
}  
}  
}],  
[{  
"not": {  
"filter": {  
"query": {  
"terms": {  
"\_all": ["sex", "xxx", "sexshop"]  
}  
}  
}  
}  
}]  
]  
}  
},  
"from": 0,  
"size": "10",  
"sort": {  
"\_geo\_distance": {  
"Location.location": {  
"lat": "-32.890183",  
"lon": "-68.844050",  
"order": "desc"  
}  
},  
"\_score": "desc"  
}  
}

It seems like the last filter is executed correctly.

Thanks,

Marce

El jueves, 16 de octubre de 2014 09:16:43 UTC-3, @mromagnoli escribió:

> Hi everyone,  
> I have a doubt about Filters.
> 
> If I have more than one filter, in a filtered query, are they executed in  
> the defined order? And, are they filtering in a 'chain' mode, i.e. using  
> the results of the previous filters?
> 
> Thanks in advance as always.

--  
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/4ed1e0ad-9654-40a6-a48a-b753c102d0ae%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4ed1e0ad-9654-40a6-a48a-b753c102d0ae%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:** [October 18, 2014, 6:36pm UTC](https://discuss.elastic.co/t/filters-odd-behavior/20284/5 "2014-10-18T18:36:33Z")

</div>

The structure of your query is odd. Either it is some format that I am not  
aware of or the Elasticsearch parser is not doing a good job at determining  
it is invalid.

Your two filters should be joined via a bool filter. Sometime like (not  
tested):

{  
"query": {  
"filtered": {  
"query": {  
....  
},  
"filter": {  
"bool": {  
"must": [  
{  
"geo\_distance": {  
"distance": "30km",  
"Location.location": {  
"lat": "-32.890183",  
"lon": "-68.844050"  
}  
}  
},  
{  
"not": {  
"filter": {  
"query": {  
"terms": {  
"\_all": [  
"sex",  
"xxx",  
"sexshop"  
]  
}  
}  
}  
}  
}  
]  
}  
}  
}  
}  
}

--  
Ivan

On Sat, Oct 18, 2014 at 7:04 AM, @mromagnoli [marce.romagnoli@gmail.com](mailto:marce.romagnoli@gmail.com)  
wrote:

> Thanks guys for your responses.
> 
> My question was due an strange behavior when using 'not' and  
> 'geo\_distance' filters.
> 
> I want to filter some results that have undesirable words, such as 'sex',  
> 'xxx', etc... And then geo filter those good results, but if I place 'not'  
> filter first, then when geo filter is applied, it retrieves results with  
> that not wanted words in it. If I place geo filter first, and then 'not'  
> filter, geo filter seems not to be executed or something, because results  
> are not accurate for that filter, instead, they still being good results  
> with no bad words.
> 
> I am using it like this:
> 
> {  
> "query": {  
> "filtered": {  
> "query": {...},  
> "filter": [  
> [{  
> "geo\_distance": {  
> "distance": "30km",  
> "Location.location": {  
> "lat": "-32.890183",  
> "lon": "-68.844050"  
> }  
> }  
> }],  
> [{  
> "not": {  
> "filter": {  
> "query": {  
> "terms": {  
> "\_all": ["sex", "xxx", "sexshop"]  
> }  
> }  
> }  
> }  
> }]  
> ]  
> }  
> },  
> "from": 0,  
> "size": "10",  
> "sort": {  
> "\_geo\_distance": {  
> "Location.location": {  
> "lat": "-32.890183",  
> "lon": "-68.844050",  
> "order": "desc"  
> }  
> },  
> "\_score": "desc"  
> }  
> }
> 
> It seems like the last filter is executed correctly.
> 
> Thanks,
> 
> Marce
> 
> El jueves, 16 de octubre de 2014 09:16:43 UTC-3, @mromagnoli escribió:
> 
> > Hi everyone,  
> > I have a doubt about Filters.
> > 
> > If I have more than one filter, in a filtered query, are they executed in  
> > the defined order? And, are they filtering in a 'chain' mode, i.e. using  
> > the results of the previous filters?
> > 
> > Thanks in advance as always.
> 
> --  
> 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/4ed1e0ad-9654-40a6-a48a-b753c102d0ae%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4ed1e0ad-9654-40a6-a48a-b753c102d0ae%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/4ed1e0ad-9654-40a6-a48a-b753c102d0ae%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/4ed1e0ad-9654-40a6-a48a-b753c102d0ae%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%3DcQBZoCpy\_nbxVTM9v2%2BR7Y2jmAzGMtsRSZ8J-CVgm\_yawQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBZoCpy_nbxVTM9v2%2BR7Y2jmAzGMtsRSZ8J-CVgm_yawQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![mromagnoli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mromagnoli/32/839_2.png) [@mromagnoli](https://discuss.elastic.co/u/mromagnoli)\
**Post date:** [October 21, 2014, 3:05pm UTC](https://discuss.elastic.co/t/filters-odd-behavior/20284/6 "2014-10-21T15:05:02Z")

</div>

Thanks for answer, Ivan.

You were right, I wrapped all my filters inside bool filters and it seems  
everything is working correctly!

Thanks for your time!

Marce

El sábado, 18 de octubre de 2014 15:36:39 UTC-3, Ivan Brusic escribió:

> The structure of your query is odd. Either it is some format that I am not  
> aware of or the Elasticsearch parser is not doing a good job at determining  
> it is invalid.
> 
> Your two filters should be joined via a bool filter. Sometime like (not  
> tested):
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> ....  
> },  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "geo\_distance": {  
> "distance": "30km",  
> "Location.location": {  
> "lat": "-32.890183",  
> "lon": "-68.844050"  
> }  
> }  
> },  
> {  
> "not": {  
> "filter": {  
> "query": {  
> "terms": {  
> "\_all": [  
> "sex",  
> "xxx",  
> "sexshop"  
> ]  
> }  
> }  
> }  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }
> 
> --  
> Ivan
> 
> On Sat, Oct 18, 2014 at 7:04 AM, @mromagnoli \<[marce.r...@gmail.com](mailto:marce.r...@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > Thanks guys for your responses.
> > 
> > My question was due an strange behavior when using 'not' and  
> > 'geo\_distance' filters.
> > 
> > I want to filter some results that have undesirable words, such as 'sex',  
> > 'xxx', etc... And then geo filter those good results, but if I place 'not'  
> > filter first, then when geo filter is applied, it retrieves results with  
> > that not wanted words in it. If I place geo filter first, and then 'not'  
> > filter, geo filter seems not to be executed or something, because results  
> > are not accurate for that filter, instead, they still being good results  
> > with no bad words.
> > 
> > I am using it like this:
> > 
> > {  
> > "query": {  
> > "filtered": {  
> > "query": {...},  
> > "filter": [  
> > [{  
> > "geo\_distance": {  
> > "distance": "30km",  
> > "Location.location": {  
> > "lat": "-32.890183",  
> > "lon": "-68.844050"  
> > }  
> > }  
> > }],  
> > [{  
> > "not": {  
> > "filter": {  
> > "query": {  
> > "terms": {  
> > "\_all": ["sex", "xxx", "sexshop"]  
> > }  
> > }  
> > }  
> > }  
> > }]  
> > ]  
> > }  
> > },  
> > "from": 0,  
> > "size": "10",  
> > "sort": {  
> > "\_geo\_distance": {  
> > "Location.location": {  
> > "lat": "-32.890183",  
> > "lon": "-68.844050",  
> > "order": "desc"  
> > }  
> > },  
> > "\_score": "desc"  
> > }  
> > }
> > 
> > It seems like the last filter is executed correctly.
> > 
> > Thanks,
> > 
> > Marce
> > 
> > El jueves, 16 de octubre de 2014 09:16:43 UTC-3, @mromagnoli escribió:
> > 
> > > Hi everyone,  
> > > I have a doubt about Filters.
> > > 
> > > If I have more than one filter, in a filtered query, are they executed  
> > > in the defined order? And, are they filtering in a 'chain' mode, i.e. using  
> > > the results of the previous filters?
> > > 
> > > Thanks in advance as always.
> > 
> > --  
> > 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/4ed1e0ad-9654-40a6-a48a-b753c102d0ae%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4ed1e0ad-9654-40a6-a48a-b753c102d0ae%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/4ed1e0ad-9654-40a6-a48a-b753c102d0ae%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/4ed1e0ad-9654-40a6-a48a-b753c102d0ae%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/6499b6c2-379a-400d-bf8d-9087ea2aaa10%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6499b6c2-379a-400d-bf8d-9087ea2aaa10%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:54am UTC](https://discuss.elastic.co/t/filters-odd-behavior/20284/7 "2017-07-06T00:54:42Z")

</div>


