# Bool query

**URL:** <https://discuss.elastic.co/t/bool-query/12892>\
**Category:** Elasticsearch\
**Created:** [July 23, 2013, 9:09am UTC](https://discuss.elastic.co/t/bool-query/12892 "2013-07-23T09:09:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oreno](https://avatars.discourse-cdn.com/v4/letter/o/b4bc9f/32.png) [@oreno](https://discuss.elastic.co/u/oreno)\
**Post date:** [July 23, 2013, 9:09am UTC](https://discuss.elastic.co/t/bool-query/12892/1 "2013-07-23T09:09:22Z")

</div>

Hi, I'm trying to write a bool query that will act such as (event\_time in range) AND (at least one of the should condition is correct)

I implemented it as:

{  
"query": {  
"bool": {  
"must":  
{  
"range": {  
"events.event\_time": {  
"from": "2013-06-01",  
"to": "2013-06-03",  
"include\_lower": true,  
"include\_upper": true  
}  
}  
},  
"should": {  
"term": {  
"events.products.product": "1772166"  
}  
}  
,"minimum\_should\_match" : 1  
}  
},  
"path": "events"  
}

But it seems like the condition between the MUST and the SHOULD conditions is an OR instead of an AND, so I get a lot more results than I should.

I read in the link below that:"In the query after this, a boolean query gets created, and in this query, "mushrooms" must be present, but "steak" should be present. If "steak" is not present but "mushrooms" is present for a document, i **t will be still returned as a hit**."  
[http://www.avajava.com/tutorials/lessons/how-do-i-combine-queries-with-a-boolean-query.html](http://www.avajava.com/tutorials/lessons/how-do-i-combine-queries-with-a-boolean-query.html)

So I understand that these 2 operations are having an OR between them. Any idea how can I make them combined under an AND condition?

Please not that the should condition should contain more then one product(but at least one should match).

---

<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:** [July 23, 2013, 4:44pm UTC](https://discuss.elastic.co/t/bool-query/12892/2 "2013-07-23T16:44:22Z")

</div>

Why not simply convert the should clause into a must clause? It appears  
that is what you want.

Here is another good writeup on boolean queries:  
[http://searchhub.org/2011/12/28/why-not-and-or-and-not/](http://searchhub.org/2011/12/28/why-not-and-or-and-not/)

--  
Ivan

On Tue, Jul 23, 2013 at 2:09 AM, oreno [oreno@exelate.com](mailto:oreno@exelate.com) wrote:

> Hi, I'm trying to write a bool query that will act such as (event\_time in  
> range) AND (at least one of the should condition is correct)
> 
> I implemented it as:
> 
> {  
> "query": {  
> "bool": {  
> "must":  
> {  
> "range": {
> 
> "events.event\_time": {
> 
> ```
> "from": "2013-06-01",
> 
> "to": "2013-06-03",
> 
> "include_lower": true,
> 
> "include_upper": true
> }
> }
> },
> "should": {
> "term": {
> 
> ```
> 
> "events.products.product": "1772166"  
> }  
> }
> 
> ,"minimum\_should\_match" : 1  
> }  
> },  
> "path": "events"  
> }
> 
> But it seems like the condition between the MUST and the SHOULD conditions  
> is an OR instead of an AND, so I get a lot more results than I should.
> 
> I read in the link below that:"In the query after this, a boolean query  
> gets  
> created, and in this query, "mushrooms" must be present, but "steak" should  
> be present. If "steak" is not present but "mushrooms" is present for a  
> document, i_t will be still returned as a hit_."
> 
> [How do I combine queries with a boolean query? - Web Tutorials - avajava.com](http://www.avajava.com/tutorials/lessons/how-do-i-combine-queries-with-a-boolean-query.html)
> 
> So I understand that these 2 operations are having an OR between them. Any  
> idea how can I make them combined under an AND condition?
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Bool-query-tp4038495.html](http://elasticsearch-users.115913.n3.nabble.com/Bool-query-tp4038495.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![oreno](https://avatars.discourse-cdn.com/v4/letter/o/b4bc9f/32.png) [@oreno](https://discuss.elastic.co/u/oreno)\
**Post date:** [July 24, 2013, 8:21am UTC](https://discuss.elastic.co/t/bool-query/12892/3 "2013-07-24T08:21:35Z")

</div>

Hi Ivan, I needed the range to be a must, but I needed another should  
condition that will have an AND relation with the range.  
I was able to solve this using the "terms" condition(since it's like  
"should" that can work inside a must).

"must" : [ {  
"range" : {  
"events.event\_time" : {  
"from" : "2013-06-01",  
"to" : "2013-06-03",  
"include\_lower" : true,  
"include\_upper" : true  
}  
}  
}, {  
"terms" : {  
"events.segments.segment" : ["80870","80871"],  
"minimum\_should\_match" : "1"  
}  
} ]

Thanks,

[image: Inline image 1]  
\*  
\*  
_Oren Orgad_ | Software Engineer | _eXelate [http://www.exelate.com/](http://www.exelate.com/)_

_p:_ +972- 72-2221329 | _e:_ [oreno@exelate.com](mailto:oreno@exelate.com)

On Tue, Jul 23, 2013 at 7:44 PM, Ivan Brusic [via Elasticsearch Users] \<  
[ml-node+s115913n4038549h54@n3.nabble.com](mailto:ml-node+s115913n4038549h54@n3.nabble.com)\> wrote:

> Why not simply convert the should clause into a must clause? It appears  
> that is what you want.
> 
> Here is another good writeup on boolean queries:  
> [http://searchhub.org/2011/12/28/why-not-and-or-and-not/](http://searchhub.org/2011/12/28/why-not-and-or-and-not/)
> 
> --  
> Ivan
> 
> On Tue, Jul 23, 2013 at 2:09 AM, oreno \<[hidden email][http://user/SendEmail.jtp?type=node&node=4038549&i=0](http://user/SendEmail.jtp?type=node&node=4038549&i=0)
> 
> > wrote:
> 
> > Hi, I'm trying to write a bool query that will act such as (event\_time in  
> > range) AND (at least one of the should condition is correct)
> > 
> > I implemented it as:
> > 
> > {  
> > "query": {  
> > "bool": {  
> > "must":  
> > {  
> > "range": {
> > 
> > "events.event\_time": {
> > 
> > ```
> > "from": "2013-06-01",
> > 
> > "to": "2013-06-03",
> > 
> > "include_lower": true,
> > 
> > "include_upper": true
> > }
> > }
> > },
> > "should": {
> > "term": {
> > 
> > ```
> > 
> > "events.products.product": "1772166"  
> > }  
> > }
> > 
> > ,"minimum\_should\_match" : 1  
> > }  
> > },  
> > "path": "events"  
> > }
> > 
> > But it seems like the condition between the MUST and the SHOULD conditions  
> > is an OR instead of an AND, so I get a lot more results than I should.
> > 
> > I read in the link below that:"In the query after this, a boolean query  
> > gets  
> > created, and in this query, "mushrooms" must be present, but "steak"  
> > should  
> > be present. If "steak" is not present but "mushrooms" is present for a  
> > document, i_t will be still returned as a hit_."
> > 
> > [How do I combine queries with a boolean query? - Web Tutorials - avajava.com](http://www.avajava.com/tutorials/lessons/how-do-i-combine-queries-with-a-boolean-query.html)
> > 
> > So I understand that these 2 operations are having an OR between them. Any  
> > idea how can I make them combined under an AND condition?
> > 
> > --  
> > View this message in context:  
> > [http://elasticsearch-users.115913.n3.nabble.com/Bool-query-tp4038495.html](http://elasticsearch-users.115913.n3.nabble.com/Bool-query-tp4038495.html)  
> > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> > 
> > --  
> > 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 [hidden email][http://user/SendEmail.jtp?type=node&node=4038549&i=1](http://user/SendEmail.jtp?type=node&node=4038549&i=1)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [hidden email][http://user/SendEmail.jtp?type=node&node=4038549&i=2](http://user/SendEmail.jtp?type=node&node=4038549&i=2)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> * * *
> 
> If you reply to this email, your message will be added to the discussion  
> below:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/Bool-query-tp4038495p4038549.html](http://elasticsearch-users.115913.n3.nabble.com/Bool-query-tp4038495p4038549.html)  
> To unsubscribe from Bool query, click here[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe\_by\_code&node=4038495&code=b3Jlbm9AZXhlbGF0ZS5jb218NDAzODQ5NXw4ODk1Mjk2Nzg=](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4038495&code=b3Jlbm9AZXhlbGF0ZS5jb218NDAzODQ5NXw4ODk1Mjk2Nzg=)  
> .  
> NAML[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro\_viewer&id=instant\_html!nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify\_subscribers!nabble%3Aemail.naml-instant\_emails!nabble%3Aemail.naml-send\_instant\_email!nabble%3Aemail.naml](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml)

---

<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, 2:24am UTC](https://discuss.elastic.co/t/bool-query/12892/4 "2017-07-06T02:24:50Z")

</div>


