View Source

Although the majority of queries can be constructed without resorting to explicit Boolean operators, enterprise users sometimes do need the extra power to form complex queries to narrow searches. The basic Boolean operators are AND, OR, and unary and binary NOT. Evaluation is left to right, but parenthesis can be used to control the evaluation order. For example,
* {{Cat OR dog AND pet NOT zebra}}
* {{cat OR NOT dog}}
* {{cat AND NOT dog}}
* {{cat NOT dog}}
* {{(cat OR dog) AND (table OR chair)}}
* {{hit AND run}}
* {{hot OR not}}
* {{NOT cat AND dog}}
* {{cat AND (NOT dog)}}

Binary 'NOT' is equivalent to 'AND NOT' unless preceded by 'OR'.

A Boolean operator is used to combine the results of two term lists or the results of another boolean operator. For example,
* {{Abraham Lincoln OR George Washington}}
* {{second-hand furniture AND (table OR chair)}}

Parentheses are not required around term lists (any sequence of terms without Boolean operators, also known as an _extended Boolean_ query). For example, the following are equivalent to the preceding examples:
* {{(Abraham Lincoln) OR (George Washington)}}
* {{(second-hand furniture) AND (table OR chair)}}

Although Boolean operators are normally written in all upper case, lower case '{{and}}' and '{{or}}' are also permitted by default. Sometimes that may cause a query to be misinterpreted, but usually with little or no harm. Lower case 'not' is not permitted since it would cause very wrong results if it happened to occur as a query term. For example:
* {{Abraham Lincoln and George Washington}}
* {{second-hand furniture and (table or chair)}}
* {{hit and run}}
* {{hot or not}}

The {{upOp}} configuration setting can be enabled to require all Boolean operators to be upper case-only. The {{notOp}} configuration setting can be disabled to allow lower case '{{not}}' as a Boolean operator. See [lweug20:Query Parser Customization] for more details on how to change these settings.

In addition to the Boolean keywords, non-keyword operator equivalents are available as substitutions for the keyword Boolean operators. A double ampersand ('&&') means 'AND', a double vertical bar ('\|\|') means 'OR' and a single exclamation point ('\!') means 'NOT'. So, the above examples can also be written as:
* {{Cat \|\| dog && pet \! zebra}}
* {{cat \|\| \! dog}}
* {{cat && \! dog}}
* {{cat \! dog}}
* {{(cat \|\| dog) && (table \|\| chair)}}
* {{Abraham Lincoln \|\| George Washington}}
* {{second-hand furniture && (table \|\| chair)}}
* {{(Abraham Lincoln) \|\| (George Washington)}}
* {{(second-hand furniture) && (table OR chair)}}
* {{hit && run}}
* {{hot \|\| not}}
* {{\! cat && dog}}
* {{cat && (\! dog)}}

Sticky field names or keyword options remain in effect only until either a new sticky field name (or keyword option) or a right parenthesis at the current parenthesis nesting level. For example,
|| User Entry || Equivalent ||
| {{title:cat nap OR dog bark}} | {{title:(cat nap) OR title:(dog bark)}} |
| {{title:cat nap OR body: dog bark}} | {{title:(cat nap) OR body:(dog bark)}} |
| {{(body:cat AND ((title:dog) OR fish)) AND bat}} | {{(body:cat AND ((title:dog) OR body:fish)) AND default:bat}} |

h2. Left to Right Boolean Evaluation Order

Unlike Lucene and Solr, the Lucid query parser assures that Boolean queries without parentheses will be evaluated from left to right. For example, the following are equivalent:
* {{cat OR dog OR fox AND pet}}
* {{(cat OR dog OR fox) AND pet}}
* {{((cat OR dog) OR fox) AND pet}}

h2. Implicit AND versus Implicit OR

If none of the terms of a term list have explicit operators (\+ or {{\-}}, the individual terms will be implicitly ANDed into the query. Lucene and Solr default to implicit ORing of terms, but implicit ANDing tends to produce better results in most applications for most users. For example:
|| User Input || Query Interpreted as ||
| {{heart attack}} | {{heart AND attack}} |
| {{George Washington}} | {{George AND Washington}} |
| {{Lincoln's Gettysburg Address}} | {{Lincoln's AND Gettysburg AND Address}} |
But if one or more of the terms in a term list has an explicit term operator (\+ or {{\-}} or relational operator) the rest of the terms will be treated as "nice to have." For example,
* {{cat \+dog \-fox}}

Selects documents which must contain "dog" and must not contain "fox". Documents will rank higher if "cat" is present, but it is not required.
* {{cat dog turtle \-zebra}}

Selects all documents that do not contain "zebra". Documents which contain any subset of "cat", "dog", and "turtle" will be ranked higher.

The {{defOp}} configuration setting in {{solrconfig.xml}} can be used to disable the implicit AND feature, but it is enabled by default.


h2. Strict vs. Loose Extended Boolean Queries

A _strict query_ or Boolean query is one in which explicit Boolean operators are used between all terms. A loose query, also known as an extended Boolean query, uses a combination of explicit Boolean operators and term lists in which the operators are implicit. Put simply, extended Boolean queries allow free-form term lists as operands for the Boolean operators, while strict Boolean queries permit only a single term or quoted phrase (or parenthesized sub-query.) Loose, extended Boolean queries provide every bit of the power of a strict Boolean query, but are more convenient to write and can be easier to read. In fact, queries written in more of a natural language format with fewer explicit Boolean operators facilitate relevancy boosting of adjacent terms.
|| Examples of strict Boolean queries || The equivalent loose, extended Boolean queries ||
| cat AND dog | cat dog |
| (cat OR dog) AND (food OR health) | (cat OR dog) AND (food OR health) |
| cat OR dog NOT pets | cat dog \-pets |
| (George AND Washington) OR (Abraham AND Lincoln) | George Washington OR Abraham Lincoln |
| "George Washington" OR ("Abraham" AND "Lincoln") | "George Washington" OR "Abraham Lincoln" \\ |

{scrollbar}