Sub topic 13
Optional refinements: highlighting, snippets, and WITH clauses
This chapter assumes you can write a FIND query with a RETURNING clause and read the List<List<SObject>> result in Apex. The refinements here are optional. They change how results are scoped or presented, not which search terms match.
Where WITH clauses go
A WITH clause comes after RETURNING and before the final LIMIT. A query can carry several of them.
FIND {Acme} IN ALL FIELDS
RETURNING Account(Id, Name)
WITH DIVISION = 'Global'
LIMIT 20Common clauses
| Clause | What it does | Notes |
|---|---|---|
| `WITH DIVISION = 'name'` | Returns only records in that division | Needs divisions enabled in the org |
| `WITH DATA CATEGORY` | Filters Knowledge articles and questions by category | Uses a category filter expression |
| `WITH SPELL_CORRECTION = true` | Searches again with a corrected spelling when the term looks misspelled | Set it to `false` to turn it off |
| `WITH HIGHLIGHT` | Wraps matching terms in `<mark>` tags in returned text | Meant for display |
| `WITH SNIPPET (target_length=n)` | Returns a short excerpt around the match | Not available for every object |
| `WITH USER_MODE` | Runs the search with the user's field and object permissions | Placed after `RETURNING` |
Check the SOSL reference for your API version before relying on a clause. Support differs by object and by API version.
Highlighting
WITH HIGHLIGHT marks the matched words in the text fields that come back. A search for renewal might return a Subject of Contract <mark>renewal</mark> due. The tags are part of the field value. If you put that value on a page, decide whether to render the tags or strip them. Don't copy highlighted text back into a record, or the tags become stored data.
Snippets
A snippet is a short excerpt of the matching text, so a result list can show context without loading a long field. target_length sets the approximate size. The documented range is 100 to 500 characters, and the default is 300. Snippets apply to a limited set of objects, including Case and Knowledge articles.
Snippets don't fit in the List<List<SObject>> shape. Use Search.find, which returns a Search.SearchResults object.
Search.SearchResults found = Search.find(
'FIND \'refund\' IN ALL FIELDS ' +
'RETURNING Case(Id, Subject) ' +
'WITH SNIPPET (target_length=120)'
);
List<Search.SearchResult> caseHits = found.get('Case');
for (Search.SearchResult hit : caseHits) {
Case c = (Case) hit.getSObject();
System.debug(c.Subject + ' -> ' + hit.getSnippet());
}found.get('Case') returns the hits for one object, in relevance order. Each SearchResult holds the record from getSObject() and its excerpt from getSnippet().
Common errors
- Using a clause on an unsupported object. The query fails or the clause is ignored. Check the object's support first.
- Expecting snippets from bracket syntax. Only
Search.findexposesgetSnippet(). - Putting
WITHbeforeRETURNING. The parser rejects it. Keep the order:FIND,IN,RETURNING,WITH,LIMIT. - Using
WITH DIVISIONin an org without divisions. The query fails. - Treating spell correction as a filter. It can broaden results beyond what the user typed. Turn it off when exact matching matters.
Exercise
You want a support search page to show each matching Case with a short text excerpt of about 200 characters. Which Apex method do you call, and how do you read the excerpt?
Show answer
Call Search.find with a query that ends in WITH SNIPPET (target_length=200). Get the hits with get('Case') on the returned Search.SearchResults. Then call getSObject() for the record and getSnippet() for the excerpt on each Search.SearchResult.