| Topic 19: Use of Parentheses
Search services that support structured (Boolean) syntax do not always read from left to right like we do. Instead, they read "inside-out", in order of the nested levels of arguments set off by parentheses. Each bounded argument set off by parentheses is called a Boolean expression. (The entire query is also assumed to have parentheses around it, whether you put them in or not.) This is the same concept drummed home in high school math in how to evaluate an algebraic expression.
Learning how to construct this Boolean syntax structure is easy. You only need to remember four things:
- You define a Boolean expression through use of an open parenthesis ['('] to begin it, and a closed parenthesis [')'] to end it
- Make sure the first search concept you want evaluated is at the inner-most level of your Boolean expressions; followed by subsequent expressions in your desired order
- Make sure you have a balanced (equal) number of open and close parentheses in your entire query
- Expressions at the same "level" are read in order, from left to right.
It is really worth your time to master these simple rules. It adds immensely to your control over your queries and their ability to return the results you desire.
Though some search services support quite a few layers of nested Boolean expressions, in practice the amount of nesting you need or is even desirable is quite low, likely no more than three at most. To show a three-level example, consider the following dummy query:
THIRD expression (SECOND expression (FIRST expression evaluated) evaluated) evaluated
Note, you do not need to put parentheses around the entire query; the outermost layer is evaluated last in any case. But, even when you think the computer is going to do what you want, it is always safer to use parentheses if there is even a chance of confusion. Parentheses will also help you read your own searches.
![[Don't heavily nest your parentheses. Remember, keep it simple!]](images/srchtip11.gif)
In the absence of any nesting, or with expressions at equivalent levels, the order of query interpretation is from left to right. For example:
FIRST expression AND SECOND OR THIRD AND FOURTH
or,
(FIRST main subject) AND THIRD expression AND (SECOND expression)
AS A GENERAL RULE, YOU SHOULD ALWAYS PLACE YOUR MAIN SUBJECT TO BE EVALUATED FIRST. This is because many search engines determine the rank order of document results by relevance, with first query terms to be evaluated ranked higher. This rule can be a bit tricky until you get used to it. For example, taking the last query example above, but forgetting the initial set of parentheses shown, produces the following:
SECOND main subject AND THIRD expression AND (FIRST expression)
Using the form above, if you placed your main query subject first in your query expecting it to be evaluated first, you would get the unintended consequence of having it evaluated second.
Finally, Boolean operator precedence is enforced by most search engines with AND and AND NOT being evaluated before OR. If you have doubts of operator precedence, consult the help system for the search engine being used. Our recommendation: eliminate ambiguity as to how a given engine treats operator precedence by explicity putting your expressions into parentheses in the evaluation order you desire.
The OR operator should generally be used solely within nested expressions, and then mostly to capture synonyms.
For example, you may recall from our sample problem of Jan's mystery bird [Topic 5] that Jan wanted the concept of having seen the bird in the city as part of the query. Also recall there is a problem with picking up too many unwanted words when city is truncated as cit*. A good way to handle this problem is with a nested Boolean expression using OR. Thus, to capture both the singular and plural forms of city, Jan would write:
(city OR cities)
This expression now covers the singular and plural without inadvertently adding undesired words (such as 'citizen' or 'citrus') to the query term list.
Whenever you mix Boolean operators in a query you should always use parentheses to force the evaluation order you want. This helps avoid unintended consequences. For example, the following query without parentheses.
hawks AND eagles OR falcons AND owls OR vultures
Is actually evaluated as:
(hawks AND eagles) OR (falcons AND owls) OR vultures
The result of this expression is not very useful. The expression does not require any one term. You could end up with pages containing only vultures or only owls and falcons or only hawks and eagles. This is most likely not the way you intended it.
Lastly, there are times when parentheses are not needed. This is when all operators are either AND or OR in the query. For example,
hawks AND eagles AND falcons AND owls AND vultures
or,
hawks OR eagles OR falcons OR owls OR vultures
The former requires all five types of bird to be included in a successful document; the latter only one. Additional examples of possible pitfall query syntax is shown in Topic 29. |