# Bool vs. And/Or/Not filters

**URL:** https://discuss.elastic.co/t/bool-vs-and-or-not-filters/17701
**Category:** Elasticsearch
**Created:** [May 23, 2014, 8:26pm UTC](https://discuss.elastic.co/t/bool-vs-and-or-not-filters/17701 "2014-05-23T20:26:37Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Valery\_Ayala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/valery_ayala/32/75433_2.png) [@Valery\_Ayala](https://discuss.elastic.co/u/Valery_Ayala)
#### Post date: [May 23, 2014, 8:26pm UTC](https://discuss.elastic.co/t/bool-vs-and-or-not-filters/17701/1 "2014-05-23T20:26:37Z")

</div>

I have a filtered query, and within the filter I want to accomplish  
something like this

(X AND Y AND ...) AND (A OR B OR ...) AND (C OR D OR ...)

where X, Y, A, B, C, D are all filter expressions and each set of  
expressions within the params can be arbitrary in length

Originally, before the requirement for that extra C or D term at the end, I  
was constructing a query that looked like this:

{  
"query": {  
"filtered": {  
"filter": {  
"bool": {  
"should": ["A" , "B", ...],  
"must": ["X", "Y", ...]  
},  
"query": {...}  
}  
}  
}  
}

Now, to take into account the third set of filters I'm thinking I need to  
move to something like this:

{  
"query": {  
"filtered": {  
"filter": {  
"bool": {  
"must": [  
{  
X,  
Y,  
...  
{  
"or": [A,B,...]  
},  
{  
"or": [C,D,...]  
}  
}  
]  
}  
},  
"query": {...}  
}  
}  
}

( X, Y, A, B, C, D are all separate filter expressions themselves)

I read this helpful post (section regarding bool vs and/or/not):  
[https://groups.google.com/d/msg/elasticsearch/PS12RcyNSWc/I1PX1r0RfFcJ](https://groups.google.com/d/msg/elasticsearch/PS12RcyNSWc/I1PX1r0RfFcJ)  
I'm not using any geo or similar filters in the underlying filter  
expressions, just term filters and term filters with lookup mechanism, so  
should I be trying to avoid introducing the OR filter?  
Is there a better way to construct the set of filters I'm constructing  
below?  
In the thread, Clint writes  
"and/or/not filters don't demand bitsets. They work doc-by-doc, so  
they're a good fit for geo filters. They also short-circuit. If a doc  
has already been excluded by an earlier filter, it won't run the later  
filters."

But the fact that they don't "demand" bitsets, does that mean they don't  
take advantage of bitsets if the underlying filters DO provide them? So is  
there an inherent (performance) disadvantage to using them?

Thanks for your help!  
Valery

--  
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/140494bd-d10c-4d74-a068-014163089029%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/140494bd-d10c-4d74-a068-014163089029%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, 1:27am UTC](https://discuss.elastic.co/t/bool-vs-and-or-not-filters/17701/2 "2017-07-06T01:27:19Z")

</div>


