# Bool Queries and SHOULD/MUST combinations

**URL:** <https://discuss.elastic.co/t/bool-queries-and-should-must-combinations/20530>\
**Category:** Elasticsearch\
**Created:** [November 1, 2014, 7:57pm UTC](https://discuss.elastic.co/t/bool-queries-and-should-must-combinations/20530 "2014-11-01T19:57:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kazoompa\_3](https://avatars.discourse-cdn.com/v4/letter/k/958977/32.png) [@kazoompa\_3](https://discuss.elastic.co/u/kazoompa_3)\
**Post date:** [November 1, 2014, 7:57pm UTC](https://discuss.elastic.co/t/bool-queries-and-should-must-combinations/20530/1 "2014-11-01T19:57:23Z")

</div>

Hi,

Below is my data and the two queries that I tested, first one failing and  
the latter working. I start to believe that if one wants to combine several  
SHOULD and MUST filters, the outer one must always be SHOULD. Is this a  
correct assumption? In our application, we have much more complex situation  
with several filters within each MUST and SHOULD. And lastly, where should  
place a MUST\_NOT in this case?

Many thanks.

Here is my data:

\_index,\_type,\_id,\_score,\_source.id,\_source.type,\_source.valueType,\_source.sentence,\_source.location  
"test","var","0","1","0","study","text","Lorem text is jumbled","spain"  
"test","var","1","1","1","study","text","bla bla bla","spain"  
"test","var","2","1","2","schema","decimal","ipsum","germany"  
"test","var","3","1","3","study","integer","lorem","france"

Here is my FAILING query:

{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"bool": {  
"must": {  
"terms": {  
"location": [  
"germany"  
]  
}  
},  
"should": {  
"terms": {  
"valueType": [  
"integer"  
]  
}  
}  
}  
}  
}  
}  
}

Here is my WORKING query returning IDs 2 and 3:

{  
"query": {  
"bool": {  
"should": [  
{  
"terms": {  
"location": [  
"germany"  
]  
}  
},  
{  
"bool": {  
"should": [  
{  
"terms": {  
"valueType": [  
"integer"  
]  
}  
}  
]  
}  
}  
]  
}  
}  
}

--  
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/30406d09-5b43-4fa1-b74e-3feaba44f67e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/30406d09-5b43-4fa1-b74e-3feaba44f67e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [November 1, 2014, 11:17pm UTC](https://discuss.elastic.co/t/bool-queries-and-should-must-combinations/20530/2 "2014-11-01T23:17:46Z")

</div>

I think you have other errors in your setup than what is visible in the  
query you show.

If you use this

> <https://gist.github.com/jprante/ed6a78a4cee6fdf33b7f>

both queries work fine, as expected.

For the question about boolean logic, you can think of "should" as an "or",  
and "must" as an "and" operation.

Jörg

On Sat, Nov 1, 2014 at 8:57 PM, kazoompa [rhaeri@p3g.org](mailto:rhaeri@p3g.org) wrote:

> Hi,
> 
> Below is my data and the two queries that I tested, first one failing and  
> the latter working. I start to believe that if one wants to combine several  
> SHOULD and MUST filters, the outer one must always be SHOULD. Is this a  
> correct assumption? In our application, we have much more complex situation  
> with several filters within each MUST and SHOULD. And lastly, where should  
> place a MUST\_NOT in this case?
> 
> Many thanks.
> 
> Here is my data:
> 
> \_index,\_type,\_id,\_score,\_source.id,\_source.type,\_source.valueType,\_source.sentence,\_source.location  
> "test","var","0","1","0","study","text","Lorem text is jumbled","spain"  
> "test","var","1","1","1","study","text","bla bla bla","spain"  
> "test","var","2","1","2","schema","decimal","ipsum","germany"  
> "test","var","3","1","3","study","integer","lorem","france"
> 
> Here is my FAILING query:
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "bool": {  
> "must": {  
> "terms": {  
> "location": [  
> "germany"  
> ]  
> }  
> },  
> "should": {  
> "terms": {  
> "valueType": [  
> "integer"  
> ]  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> Here is my WORKING query returning IDs 2 and 3:
> 
> {  
> "query": {  
> "bool": {  
> "should": [  
> {  
> "terms": {  
> "location": [  
> "germany"  
> ]  
> }  
> },  
> {  
> "bool": {  
> "should": [  
> {  
> "terms": {  
> "valueType": [  
> "integer"  
> ]  
> }  
> }  
> ]  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> --  
> 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/30406d09-5b43-4fa1-b74e-3feaba44f67e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/30406d09-5b43-4fa1-b74e-3feaba44f67e%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/30406d09-5b43-4fa1-b74e-3feaba44f67e%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/30406d09-5b43-4fa1-b74e-3feaba44f67e%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/CAKdsXoFNk3fqb0-BNNUj\_P2wxFPinZEow1MKf9pwso2Y41\_XoQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoFNk3fqb0-BNNUj_P2wxFPinZEow1MKf9pwso2Y41_XoQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![kazoompa\_3](https://avatars.discourse-cdn.com/v4/letter/k/958977/32.png) [@kazoompa\_3](https://discuss.elastic.co/u/kazoompa_3)\
**Post date:** [November 1, 2014, 11:36pm UTC](https://discuss.elastic.co/t/bool-queries-and-should-must-combinations/20530/3 "2014-11-01T23:36:42Z")

</div>

Thanks Jörg for the response, I had a mistake in my question and I had to  
delete and re-post it.

Here is how I did the indexing (never mind the data, it is randomly  
created) and I don't think there is any error in the method that I know of :

curl -XPUT '[http://localhost:9200/test/var/0](http://localhost:9200/test/var/0)' -d '{"id": "0", "type":  
"schema", "valueType": "integer", "sentence": "Lorem text is jumbled",  
"location": "germany"}'  
curl -XPUT '[http://localhost:9200/test/var/1](http://localhost:9200/test/var/1)' -d '{"id": "1", "type":  
"schema", "valueType": "decimal", "sentence": "bla bla bla", "location":  
"united states"}'  
curl -XPUT '[http://localhost:9200/test/var/2](http://localhost:9200/test/var/2)' -d '{"id": "2", "type":  
"study", "valueType": "decimal", "sentence": "ipsum is the dude",  
"location": "france"}'  
curl -XPUT '[http://localhost:9200/test/var/3](http://localhost:9200/test/var/3)' -d '{"id": "3", "type":  
"study", "valueType": "text", "sentence": "jumbled mama", "location":  
"france"}'

You can always have a look at the revised question and share your wisdom  
with me 🙂

Cheers.

On Saturday, November 1, 2014 7:18:11 PM UTC-4, Jörg Prante wrote:

> I think you have other errors in your setup than what is visible in the  
> query you show.
> 
> If you use this
> 
> [Must/should filter · GitHub](https://gist.github.com/jprante/ed6a78a4cee6fdf33b7f)
> 
> both queries work fine, as expected.
> 
> For the question about boolean logic, you can think of "should" as an  
> "or", and "must" as an "and" operation.
> 
> Jörg
> 
> On Sat, Nov 1, 2014 at 8:57 PM, kazoompa \<[rha...@p3g.org](mailto:rha...@p3g.org) \<javascript:\>\>  
> wrote:
> 
> > Hi,
> > 
> > Below is my data and the two queries that I tested, first one failing and  
> > the latter working. I start to believe that if one wants to combine several  
> > SHOULD and MUST filters, the outer one must always be SHOULD. Is this a  
> > correct assumption? In our application, we have much more complex situation  
> > with several filters within each MUST and SHOULD. And lastly, where should  
> > place a MUST\_NOT in this case?
> > 
> > Many thanks.
> > 
> > Here is my data:
> > 
> > \_index,\_type,\_id,\_score,\_source.id,\_source.type,\_source.valueType,\_source.sentence,\_source.location  
> > "test","var","0","1","0","study","text","Lorem text is jumbled","spain"  
> > "test","var","1","1","1","study","text","bla bla bla","spain"  
> > "test","var","2","1","2","schema","decimal","ipsum","germany"  
> > "test","var","3","1","3","study","integer","lorem","france"
> > 
> > Here is my FAILING query:
> > 
> > {  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "filter": {  
> > "bool": {  
> > "must": {  
> > "terms": {  
> > "location": [  
> > "germany"  
> > ]  
> > }  
> > },  
> > "should": {  
> > "terms": {  
> > "valueType": [  
> > "integer"  
> > ]  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > Here is my WORKING query returning IDs 2 and 3:
> > 
> > {  
> > "query": {  
> > "bool": {  
> > "should": [  
> > {  
> > "terms": {  
> > "location": [  
> > "germany"  
> > ]  
> > }  
> > },  
> > {  
> > "bool": {  
> > "should": [  
> > {  
> > "terms": {  
> > "valueType": [  
> > "integer"  
> > ]  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }
> > 
> > --  
> > 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/30406d09-5b43-4fa1-b74e-3feaba44f67e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/30406d09-5b43-4fa1-b74e-3feaba44f67e%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/30406d09-5b43-4fa1-b74e-3feaba44f67e%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/30406d09-5b43-4fa1-b74e-3feaba44f67e%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/244b5486-d5e8-4584-a429-609e2a3ce111%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/244b5486-d5e8-4584-a429-609e2a3ce111%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:52am UTC](https://discuss.elastic.co/t/bool-queries-and-should-must-combinations/20530/4 "2017-07-06T00:52:39Z")

</div>


