# Boolean Queries

**URL:** <https://discuss.elastic.co/t/boolean-queries/5573>\
**Category:** Elasticsearch\
**Created:** [October 12, 2011, 6:39am UTC](https://discuss.elastic.co/t/boolean-queries/5573 "2011-10-12T06:39:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ElasticUser](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@ElasticUser](https://discuss.elastic.co/u/ElasticUser)\
**Post date:** [October 12, 2011, 6:39am UTC](https://discuss.elastic.co/t/boolean-queries/5573/1 "2011-10-12T06:39:34Z")

</div>

Hi,  
I am looking at parsing boolean query strings as below :

condition 1 AND conditon2 OR condition3 NOT (condition 4 AND condition5 NOT condition6) AND condition7

Can someone suggest if there is already a boolean query parser that would break down the above query into tokens? Or I will have to manually break the above string into tokens?

Also BoolQueryBuilder class has MUST, MUST NOT and Should.  
Can someone tell me the difference between MUST and SHOULD?

What could one use for OR condition?

Please advice. Thanks!

---

<div class="post-metadata">

**Author:** ![ElasticUser](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@ElasticUser](https://discuss.elastic.co/u/ElasticUser)\
**Post date:** [October 12, 2011, 5:47pm UTC](https://discuss.elastic.co/t/boolean-queries/5573/2 "2011-10-12T17:47:01Z")

</div>

Any input on this? Thanks

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [October 12, 2011, 6:02pm UTC](https://discuss.elastic.co/t/boolean-queries/5573/3 "2011-10-12T18:02:14Z")

</div>

On Tue, 2011-10-11 at 23:39 -0700, ElasticUser wrote:

> Hi,  
> I am looking at parsing boolean query strings as below :
> 
> condition 1 AND conditon2 OR condition3 NOT (condition 4 AND condition5 NOT  
> condition6) AND condition7

Have a look at the query\_string 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.

which uses the lucene query syntax:  
[http://lucene.apache.org/java/3\_4\_0/queryparsersyntax.html](http://lucene.apache.org/java/3_4_0/queryparsersyntax.html)

> Also BoolQueryBuilder class has MUST, MUST NOT and Should.  
> Can someone tell me the difference between MUST and SHOULD?

Must = must be true  
Should = at least one of these should be true

> What could one use for OR condition?

Should

clint

---

<div class="post-metadata">

**Author:** ![ElasticUser](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@ElasticUser](https://discuss.elastic.co/u/ElasticUser)\
**Post date:** [October 12, 2011, 8:00pm UTC](https://discuss.elastic.co/t/boolean-queries/5573/4 "2011-10-12T20:00:29Z")

</div>

Hi Clint,  
Thanks for your response 🙂  
QueryStringQueryBuilder.Operator just has AND and OR operators. Would you know how can one use NOT operator?

Also, there is a BoolQuery class.  
[http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)

How is this different from QueryString query?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [October 12, 2011, 9:29pm UTC](https://discuss.elastic.co/t/boolean-queries/5573/5 "2011-10-12T21:29:35Z")

</div>

On Wed, Oct 12, 2011 at 10:00 PM, ElasticUser [lkodwaney@gmail.com](mailto:lkodwaney@gmail.com) wrote:

> Hi Clint,  
> Thanks for your response 🙂  
> QueryStringQueryBuilder.Operator just has AND and OR operators. Would you  
> know how can one use NOT operator?

Thats the default operator for text analyzed in the query string. For  
example: "green blue", will be broken down into "green AND blue" with AND  
default operator, "green OR blue" with OR default operator. NOT does not  
really make sense here.

> Also, there is a BoolQuery class.  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)
> 
> How is this different from QueryString query?

It gives you a more structured option to combine your query.

> Thanks for your help.
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Boolean-Queries-tp3414951p3416747.html](http://elasticsearch-users.115913.n3.nabble.com/Boolean-Queries-tp3414951p3416747.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![ElasticUser](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@ElasticUser](https://discuss.elastic.co/u/ElasticUser)\
**Post date:** [October 12, 2011, 10:12pm UTC](https://discuss.elastic.co/t/boolean-queries/5573/6 "2011-10-12T22:12:23Z")

</div>

Hi Kimchy,  
Thanks for response.  
We are looking at a scenario where user could enter the below sample query string :  
condition 1 AND conditon2 OR condition3 NOT (condition 4 AND condition5 NOT condition6) AND condition7

In this case, do you recommend using QueryStringQuery or BoolQuery?

---

<div class="post-metadata">

**Author:** ![ElasticUser](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@ElasticUser](https://discuss.elastic.co/u/ElasticUser)\
**Post date:** [October 13, 2011, 5:53pm UTC](https://discuss.elastic.co/t/boolean-queries/5573/7 "2011-10-13T17:53:37Z")

</div>

Any inputs/suggestions on this?

---

<div class="post-metadata">

**Author:** ![Jan\_Fiedler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_fiedler/32/2518_2.png) [@Jan\_Fiedler](https://discuss.elastic.co/u/Jan_Fiedler)\
**Post date:** [October 13, 2011, 7:03pm UTC](https://discuss.elastic.co/t/boolean-queries/5573/8 "2011-10-13T19:03:14Z")

</div>

I depends a little on what your condition1-x actually are but I would think  
you are best off using the QueryStringQuery. With that you do not have to  
implement your own parsing (that would be required if you wanted to use the  
lower level BoolQuery).

---

<div class="post-metadata">

**Author:** ![ElasticUser](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@ElasticUser](https://discuss.elastic.co/u/ElasticUser)\
**Post date:** [October 13, 2011, 8:07pm UTC](https://discuss.elastic.co/t/boolean-queries/5573/9 "2011-10-13T20:07:18Z")

</div>

Thanks for the suggestion Jan. The condition could be something like field1=value1 AND field2=value2 OR field3=value3 NOT field4=value4  
Also the QueryStringQueryBuilder.Operator class just has AND and OR. Not sure how we could implement the NOT condition (BoolQuery has MUST NOT)

---

<div class="post-metadata">

**Author:** ![Jan\_Fiedler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_fiedler/32/2518_2.png) [@Jan\_Fiedler](https://discuss.elastic.co/u/Jan_Fiedler)\
**Post date:** [October 14, 2011, 6:25am UTC](https://discuss.elastic.co/t/boolean-queries/5573/10 "2011-10-14T06:25:00Z")

</div>

The QueryStringBuilder supports the full Lucene query syntax (as Clinton  
pointed out earlier):  
[http://lucene.apache.org/java/3\_4\_0/queryparsersyntax.html](http://lucene.apache.org/java/3_4_0/queryparsersyntax.html)  
It therefore does support the NOT operator and also grouping logical  
expressions via parenthesis. The only difference to the notation you are  
looking for is how fields are accessed. In Lucene notation you would refer  
to fields via something like 'field1:value1'.

---

<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, 3:52am UTC](https://discuss.elastic.co/t/boolean-queries/5573/11 "2017-07-06T03:52:00Z")

</div>


