Sub topic 5
Wildcards, phrases, and AND, OR, AND NOT
This chapter assumes you can write a basic FIND ... RETURNING query and run it in anonymous Apex or the Developer Console. It covers how to shape the search expression, which is the text after FIND.
Wildcards
SOSL has two wildcards.
| Wildcard | Matches | Example | Finds |
|---|---|---|---|
| `*` | Zero or more characters | `Acme*` | Acme, Acme Corp, Acmeville |
| `?` | Exactly one character | `Jo?n` | John, Joan |
Place a wildcard in the middle or at the end of a term. A term cannot start with one. Searches are case-insensitive, so acme* and ACME* return the same records.
List<List<SObject>> results = [
FIND 'Gl?bex*' IN NAME FIELDS
RETURNING Account(Id, Name)
];Phrases
Words are searched individually unless you quote them. Wrap a phrase in double quotes to match the words together and in order.
In the braces form, the quotes go directly in the expression:
FIND {"Acme Corp"} IN NAME FIELDS RETURNING Account(Id, Name)Apex uses single quotes for the whole search string, so the double quotes go inside it:
List<List<SObject>> results = [
FIND '"Acme Corp"' IN NAME FIELDS
RETURNING Account(Id, Name)
];AND, OR, and AND NOT
Combine terms with three operators. Write them in uppercase.
ANDrequires both terms to match.ORrequires at least one term to match.AND NOTexcludes records that match the second term.
FIND {"Acme Corp" OR Globex* AND NOT Test}
IN NAME FIELDS
RETURNING Account(Id, Name), Opportunity(Id, Name)This query returns records whose name contains the phrase "Acme Corp" or a word starting with Globex, and drops any that also contain Test.
When you mix operators, group the terms with parentheses. Parentheses are evaluated first, so they make your intent explicit.
FIND {(Acme OR Globex) AND Corp}
IN NAME FIELDS
RETURNING Account(Id, Name)If you list terms with no operator between them, SOSL treats the space as OR. Write the operator yourself so the query says what you mean.
flowchart TD
A[Search expression] --> B{Operator}
B -->|AND| C[Both terms must match]
B -->|OR| D[Either term may match]
B -->|AND NOT| E[Second term must be absent]Reserved characters
Some characters have special meaning in a search expression: & | ! ( ) { } [ ] ^ " ' ~ * ? : \ - +. To search for one literally, put a backslash before it. In an Apex string literal the backslash must itself be doubled:
List<List<SObject>> results = [
FIND 'AT\\&T' IN NAME FIELDS
RETURNING Account(Id, Name)
];The Apex compiler turns \\ into a single \, and SOSL then reads \& as a literal ampersand.
Common errors
- A leading wildcard.
*corpis not valid. Search for the whole word, or use a trailing wildcard on the start of the word. - Missing quotes.
FIND {Acme Corp}searches for Acme and Corp as separate words. Quote the phrase if you want them together. - Unescaped special characters. A stray
-,(, or"in the term can cause a syntax error or change what the query means. This matters most when the term comes from user input, which a later chapter covers under dynamic SOSL. - Mixed operators without parentheses.
A OR B AND Cis easy to misread. Add parentheses.
Exercise
Write a SOSL query that searches NAME FIELDS for either "Blue Sky" (as an exact phrase) or any word starting with Horizon, but excludes records containing Demo. Return the Id and Name of Account records.
Show answer
FIND {("Blue Sky" OR Horizon*) AND NOT Demo} IN NAME FIELDS RETURNING Account(Id, Name)