Newer Version Available
Salesforce Object Search Language (SOSL)
When building efficient SOSL queries, create filters that are selective. By default, SOSL queries scan all entities. The search engine looks for matches to the search term across a maximum of 2,000 records. Sharing is applied after the result set is returned from the search stack. If your filters are not selective and cause search term matches of more than 2000 records, there is a possibility of running into search crowding.
You can search text, email, and phone fields for multiple objects, including custom objects, that you have access to in a single query in the following environments.
- SOAP or REST calls
- Apex statements
- Visualforce controllers and getter methods
- Schema Explorer of the Eclipse Toolkit
When to Use SOSL
- Retrieve data for a specific term that you know exists within a field. Because SOSL can tokenize multiple terms within a field and build a search index from this, SOSL searches are faster and can return more relevant results.
- Retrieve multiple objects and fields efficiently where the objects might or might not be related to one another.
- Retrieve data for a particular division in an organization using the divisions feature.
- Retrieve data that’s in Chinese, Japanese, Korean, or Thai. Morphological tokenization for CJKT terms helps ensure accurate results.
Performance Considerations
If your searches are too general, they are slow and return too many results. Use the following clauses to define efficient text searches.
- IN: Limits the types of fields to search, including email, name, or phone.
- LIMIT: Specifies the maximum number of rows to return.
- OFFSET: Displays the search results on multiple pages.
- RETURNING: Limits the objects and fields to return.
- WITH DATA CATEGORY: Specifies the data categories to return.
- WITH DivisionFilter: Specifies the division field to return.
- WITH NETWORK: Specifies the Experience Cloud site ID to return.
- WITH PricebookId: Specifies the price book ID to return.
Navigating This Document
- To see a list of available resources, see SOSL Syntax.
- To get started working with SOSL, see Example Text Searches.