About SOSL

Use the Salesforce Object Search Language (SOSL) to construct text-based search queries against the search index.

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

If your org has relationship queries enabled, SOSL supports SOQL relationship queries.

Note

Compare SOSL and SOQL

Use SOQL when:

  • You want to search against the org's database. Results from a database search include matches for the exact search string.
  • You know in which objects or fields the data resides.
  • You want to retrieve data from a single object or from multiple objects that are related to one another.
  • You want to count the number of records that meet specified criteria.
  • You want to sort results as part of the query.
  • You want to retrieve data from number, date, or checkbox fields.

Use SOSL when:

  • You don't know in which object or field the data resides, and you want to find it in the most efficient and fastest way possible.
  • You want to 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 off this, SOSL searches are faster and can return more relevant results. Depending on the search object, SOSL searches also take advantage of the advanced features of the search index, such as out-of-order matching, synonyms, lemmatization, and spell check.
  • You want to retrieve multiple objects and fields efficiently, and the objects might or might not be related to one another.
  • You want to retrieve data for a particular division in an org using the divisions feature, and you want to find it in the most efficient way possible.

Define Efficient SOSL Text Searches

If your searches are too general, they are slow and return too many results. Use the following clauses to write more targeted and useful 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.

Example: Update an Article’s Keyword Tracking with SOSL

The UPDATE TRACKING clause is used to report on Salesforce Knowledge article searches and views. It allows developers to track the keywords used in Salesforce Knowledge article searches. Also, the language attribute can be used to search by a specific language (locale). However, only one language can be specified in a single query. Make a separate query for each language that you want. Use the Java format, which uses the underscore (for example, fr_FR, jp_JP, and so on), to supply locales. Search the Web for “java locale codes” to get a list of supported locales.

You can use this syntax to track a keyword used in Salesforce Knowledge article search:

1FIND {Keyword}
2RETURNING KnowledgeArticleVersion (Title WHERE PublishStatus="Online" and language="en_US")
3UPDATE TRACKING

Example: Update an Article’s Viewstat with SOSL

The optional UPDATE VIEWSTAT clause is used to report on Salesforce Knowledge article searches and views. It allows developers to update an article’s view statistics. Also, the language attribute can be used to search by a specific language (locale). However, only one language can be specified in a single query. Make a separate query for each language that you want. Use the Java format, which uses the underscore (for example, fr_FR, jp_JP, and so on), to supply locales. Search the Web for “java locale codes” to get a list of supported locales.

You can use this syntax to increase the view count for every article you have access to online in US English:

1FIND {Title}
2RETURNING FAQ__kav (Title WHERE PublishStatus="Online" and
3language="en_US" and
4KnowledgeArticleVersion = 'ka230000000PCiy')
5UPDATE VIEWSTAT