Boolean Queries

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!

Any input on this? Thanks

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:

which uses the lucene query syntax:
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

Hi Clint,
Thanks for your response :slight_smile:
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

How is this different from QueryString query?

Thanks for your help.

On Wed, Oct 12, 2011 at 10:00 PM, ElasticUser lkodwaney@gmail.com wrote:

Hi Clint,
Thanks for your response :slight_smile:
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

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
Sent from the Elasticsearch Users mailing list archive at Nabble.com.

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?

Any inputs/suggestions on this?

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).

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)

The QueryStringBuilder supports the full Lucene query syntax (as Clinton
pointed out earlier):
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'.