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];
}
  1. public List<Book__c> searchBooks(String searchTerm) { — this is Module 21's SOSL search pattern, applied directly.
  2. List<List<sObject>> searchResults = [ — opens the search.
  3. FIND :searchTerm IN ALL FIELDS — matches the term against Title__c, Author__c, and any other searchable text field on Book__c at once.
  4. RETURNING Book__c(Id, Title__c, Author__c, Is_Checked_Out__c) — one object, the fields to return.
  5. ]; — closes the search.
  6. (blank line)
  7. 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.
  8. } — 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);
}
  1. LibraryService library = new LibraryService(); — creates the service.
  2. 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 matched Author__c, not Title__c.
  3. (blank line)
  4. for (Book__c book : results) { — loops over the matches.
  5. 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.
  6. } — 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
  1. // The SOQL alternative would need two separate queries and merged results — the label for the comparison.
  2. List<Book__c> byTitle = [SELECT Id, Title__c FROM Book__c WHERE Title__c LIKE :('%' + searchTerm + '%')]; — one query, matching only the title.
  3. List<Book__c> byAuthor = [SELECT Id, Title__c FROM Book__c WHERE Author__c LIKE :('%' + searchTerm + '%')]; — a second query, matching only the author.
  4. // ...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')

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Search the Library Quiz

1. Why does SOSL suit a "search box" matching title or author better than two separate SOQL queries?

Log in to submit the quiz and save your score.

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.