Sub topic 1
What SOSL Is and How It Searches
Prerequisites
This chapter assumes you know the basic Salesforce data model (objects, fields, records) and basic SOQL (SELECT, WHERE, ORDER BY, LIMIT). You should also be comfortable with simple Apex: variables, collections, and loops. You can run the examples in anonymous Apex or in the Developer Console.
What SOSL is
Salesforce Object Search Language (SOSL) is a query language for searching text across several objects and fields in one request. SOQL reads rows from one object, or from related objects, and filters them by field values. SOSL takes a search term and looks for it in many objects at once.
A SOSL query has two main parts:
FINDholds the search term and, optionally, the group of fields to search.RETURNINGlists the objects, and the fields on each, that you want back.
List<List<SObject>> results = [
FIND 'Acme*' IN NAME FIELDS
RETURNING Account(Id, Name), Contact(Id, FirstName, LastName)
];This query searches the name fields of Account and Contact for terms that start with Acme. The result is a list of lists. Each inner list holds the records for one object, in the same order as in RETURNING.
How SOSL searches: the text index
SOSL does not scan your tables. Salesforce keeps a separate search index of text for searchable objects and fields. A SOSL query looks up your term in that index, finds matching records, and then loads the fields you asked for.
This design has several consequences:
- Matching is by word (token), not by raw substring. A search for
Acmematches a record namedAcme Corp, but a search forcmedoes not match it. - Searches are not case-sensitive.
- Only indexed text fields can match. Many field types, such as numbers and checkboxes, are not searched by text.
- The index is updated shortly after a record changes, not at the moment of the DML statement. A record you just inserted may not appear in a search right away. This matters for tests, which a later chapter covers.
Reading the result
The result type is List<List<SObject>>. The position of each inner list follows the order of objects in RETURNING. Cast each inner list to the concrete type before you use it.
List<List<SObject>> results = [
FIND 'Acme*' IN NAME FIELDS
RETURNING Account(Id, Name), Contact(Id, FirstName, LastName)
];
List<Account> accounts = (List<Account>) results[0];
List<Contact> contacts = (List<Contact>) results[1];
for (Account a : accounts) {
System.debug('Account: ' + a.Name);
}If an object has no matches, its inner list is empty. The outer list still has one entry per object, so results[1] is safe to read.
Common errors
- Searching for part of a word. A term like
cmefinds nothing. Use a wildcard at the end of a word, such asAcme*. - Expecting freshly inserted records to appear. The index lags behind DML, so a query right after an insert can return nothing.
- Casting the wrong index. The result order is the
RETURNINGorder, not alphabetical order. - Searching a single short term. A search term must have at least two characters, apart from wildcards. A one-character term is rejected.
Exercise
A SOSL query has RETURNING Contact(Id, Name), Account(Id, Name). Which index in the result holds the Account records, and why can a search for cme fail to find an account named Acme Corp?
Show answer
The Account records are at index 1, because the inner lists follow the order of objects in RETURNING and Contact comes first. The search for cme fails because SOSL matches whole words from a text index, not substrings. A search for Acme* would match.