Intermediate
25 min read
Search the Library
By the end of this lesson, you'll be able to:
- Search across Book__c by title or author using SOSL
- Explain why SOSL fits a "search box" scenario better than several separate SOQL queries
- Return search results in a usable form to the caller
Prerequisites: "Handle Checkouts and Returns"
A search method using SOSL
public List<Book__c> searchBooks(String searchTerm) {
List<List<sObject>> searchResults = [
FIND :searchTerm IN ALL FIELDS
RETURNING Book__c(Id, Title__c, Author__c, Is_Checked_Out__c)
];
return (List<Book__c>) searchResults[0];
}
public List<Book__c> searchBooks(String searchTerm) {— this is Module 21's SOSL search pattern, applied directly.List<List<sObject>> searchResults = [— opens the search.FIND :searchTerm IN ALL FIELDS— matches the term againstTitle__c,Author__c, and any other searchable text field onBook__cat once.RETURNING Book__c(Id, Title__c, Author__c, Is_Checked_Out__c)— one object, the fields to return.];— closes the search.- (blank line)
return (List<Book__c>) searchResults[0];— casts the first inner list back to a specific type, one search covering both title and author lookups a user might type.}— closes the method.
Using the search method
LibraryService library = new LibraryService();
List<Book__c> results = library.searchBooks('Bachman');
for (Book__c book : results) {
System.debug(book.Title__c + ' by ' + book.Author__c);
}
LibraryService library = new LibraryService();— creates the service.List<Book__c> results = library.searchBooks('Bachman');— a member typing "Bachman" into a search box finds every book by that author — even though the search term matchedAuthor__c, notTitle__c.- (blank line)
for (Book__c book : results) {— loops over the matches.System.debug(book.Title__c + ' by ' + book.Author__c);— the search code never needed to know in advance which field the match would come from.}— closes the loop.
Why SOSL fits this better than SOQL here
// The SOQL alternative would need two separate queries and merged results
List<Book__c> byTitle = [SELECT Id, Title__c FROM Book__c WHERE Title__c LIKE :('%' + searchTerm + '%')];
List<Book__c> byAuthor = [SELECT Id, Title__c FROM Book__c WHERE Author__c LIKE :('%' + searchTerm + '%')];
// ...then manually merge and deduplicate the two lists
// The SOQL alternative would need two separate queries and merged results— the label for the comparison.List<Book__c> byTitle = [SELECT Id, Title__c FROM Book__c WHERE Title__c LIKE :('%' + searchTerm + '%')];— one query, matching only the title.List<Book__c> byAuthor = [SELECT Id, Title__c FROM Book__c WHERE Author__c LIKE :('%' + searchTerm + '%')];— a second query, matching only the author.// ...then manually merge and deduplicate the two lists— the extra work a SOQL-only approach would require, that SOSL avoids entirely.
Exercise
Write a searchBooks call for the term "Long" and debug the title of the first result.
Show hint
library.searchBooks('Long')
Search the Library Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
A library search box needs to match a term against both title and author — this lesson uses SOSL from Module 21 to search both fields in one statement.