Spotlight boolean Operators

All words passed in a query string to mdfind are implicitly ANDed together. That is, "invoice apress" means both words must appear. Spotlight allows other Boolean operators as well:

* |, the pipe character, means Boolean OR * - means to exclude a term * () create groups

Working with these operators can be tricky. Whitespace is significant when building queries. To get all documents with "invoice" or "o'reilly", I write

mdfind "invoice|o'reilly"

with no spaces between the terms. If I want to find all documents with "invoice" but not "apress", it's:

mdfind "invoice(-apress)"

with no intervening spaces, and parentheses around the term I want to exclude. To get a list of invoices or contracts from O'Reilly, I'd use:

mdfind "(invoice|contract) o'reilly"

Note that in all these examples that have more than a single word, I'm using double quotes around the search term. This makes the Unix shell pass our multiple words as a single parameter. Otherwise, mdfind uses only the last word, so that:

mdfind invoice contract

is the same as

mdfind contract

It also prevents the shell from intercepting characters that it would use as special, like the parentheses, and passes them unmolested to mdfind. This is especially important if I try to search for "O'Reilly". Without quotes, I get this:

mdfind O'Reilly >

The angle bracket is the shell telling me "You started a quoted string, and now I'm waiting for you to finish it." It will sit and wait for input until it sees another single quote, or I type Ctrl-C to cancel. The shell has interpreted the single quote in "O'Reilly" as the start of a quoted string. Instead, I want

mdfind "O'Reilly"