Sub topic 4
Choosing where to search with search groups
This chapter assumes you can write a basic FIND clause, run SOSL in the Developer Console or anonymous Apex, and read a RETURNING clause. It covers the IN clause, which controls which fields SOSL matches your search term against.
What a search group does
A search group is a named set of fields. You add one to a query with IN <group> FIELDS, placed between the search expression and RETURNING. SOSL then looks for your term only in fields of that group, on every object listed in RETURNING.
| Group | Fields searched |
|---|---|
| `ALL FIELDS` | All searchable text-type fields: name, email, phone, and other text fields |
| `NAME FIELDS` | Name fields, such as `Name`, `FirstName`, and `LastName` |
| `EMAIL FIELDS` | Email fields only |
| `PHONE FIELDS` | Phone fields only |
| `SIDEBAR FIELDS` | The fields the Salesforce sidebar search covers |
If you leave out the IN clause, SOSL searches all fields, as if you had written IN ALL FIELDS.
Seeing the effect
The same term can give different results depending on the group. This query finds records whose email field starts with jane:
List<List<SObject>> results = [
FIND 'jane*' IN EMAIL FIELDS
RETURNING Contact(Id, Name, Email), Lead(Id, Name, Email)
];
List<Contact> contacts = (List<Contact>) results[0];
List<Lead> leads = (List<Lead>) results[1];
System.debug(contacts.size() + ' contacts, ' + leads.size() + ' leads');A contact named Jane Doe with the email [email protected] does not match here. Her name is not in an email field. Searching IN NAME FIELDS would find her, and IN ALL FIELDS would find her through either field.
The group decides what is matched, not what is returned. The fields you list inside RETURNING are the only fields that come back, whatever group you searched.
Picking a group
Use the diagram below as a decision guide.
A narrower group has two benefits. It returns fewer false matches, because a term like Smith will not match a description field that mentions a Smith. It also reduces the number of fields SOSL has to examine, so results are more predictable.
ALL FIELDS is the right choice when the user could be looking for anything, such as a single global search box. SIDEBAR FIELDS is useful when you want your results to resemble what the Salesforce sidebar search shows.
Combining a group with other clauses
The group works together with the rest of the query. Here a phone search is paired with a per-object filter:
String digits = '555-0100';
List<List<SObject>> results = [
FIND :digits IN PHONE FIELDS
RETURNING Account(Id, Name, Phone), Contact(Id, Name, Phone WHERE Email != null)
];The WHERE filter applies only to Contact. The PHONE FIELDS group applies to both objects.
Common mistakes
- Putting
INafterRETURNING. The order isFIND, thenIN, thenRETURNING. - Expecting
NAME FIELDSto find a record through its description or email. It looks only at name fields. - Using a narrow group and forgetting that objects without such fields contribute no matches. A group that does not apply to an object simply gives no results for it.
Exercise
A user pastes [email protected] into a search box that should find Contacts and Leads. Which search group fits best, and why might IN NAME FIELDS fail?
Show answer
IN EMAIL FIELDS fits best because the input is an email address. IN NAME FIELDS searches only name fields, so a record whose name is Maria but whose email is [email protected] would not be matched by the email text.