Search Solution Basics
These are technical notes I compiled while studying using Trailhead, Salesforce's free self-learning portal.
Choose the Right Search Solution
Explain when to create a customized search solution. Describe the difference between SOSL and SOQL. Identify which API protocols are available for search.- Search, The Salesforce Way
- Search is the number 1 most-used Salesforce feature
- All records are stored as data fields in the org’s database - the search engine makes a copy of the data and breaks up the content into smaller pieces called tokens that are stored in the search index, along with a link back to the original record
- Search index and tokens allow certain advanced features like spell correction, nicknames, lemmatization, and synonym groups
- Out-of-the-box Salesforce search works for most use cases, but in general, orgs will need a custom search solution when the org uses a custom UI instead of the standard Salesforce UI
- Custom UI examples: customer-facing knowledge base or internal product data site for employees
- This module focuses on creating a custom search and assumes the org has invested in such a custom UI, therefore needing the custom search solution
- Connect to Search with APIs
- Two main APIs:
- Salesforce Object Query Language (SOQL): use when you know where the data resides (which object and fields), and want to:
- Retrieve data from a single object or from multiple objects that are related to one another.
- Count the number of records that meet specified criteria.
- Sort results as part of the query.
- Retrieve data from number, date, or checkbox fields.
- Salesforce Object Search Language (SOSL): use when you don’t know where the data resides, and 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 from this, SOSL searches are faster and can return more relevant results.
- Retrieve multiple objects and fields efficiently, and the objects might or might not be related to one another.
- Retrieve data for a particular division in an organization using the divisions feature, and you want to find it in the most efficient way possible.
- Salesforce Object Query Language (SOQL): use when you know where the data resides (which object and fields), and want to:
- Salesforce Federated Search is a way for users to access records stored outside of Salesforce that users access for their jobs
- Makes the global search box an external search engine
- Built using the OpenSearch specification, so any search engine that conforms to that industry standard can be used
- Two main APIs:
- Send Queries with Protocols
- Search queries need to be sent using an API protocol like REST, SOAP, or Apex
- We use queries with SOQL, and searches with SOSL
Query
(REST) andquery()
(SOAP): Executes a SOQL query against the specified object and returns data that matches the specified criteriaSearch
(REST) andsearch()
(SOAP): Executes a SOSL text string search against your org’s data
- Good time for a customized search solution? Developing an external knowledge base for user support
- Differentiates SOSL from SOQL? Syntax, SOSL searches the search index instead of org database, SOSL searches more efficiently when you don’t know the object
- SOSL works with: REST, SOAP, and Apex
Build Search for Common Use Cases
Understand how to search within a single object. Understand how to search within multiple objects. Understand how to search within a custom object.- Example: Task of creating a customer-facing online knowledge base and an internal product website for employees of a company
- All data comes from the company Salesforce org
- Search Within a Single Object - one record type only
- To search within a single object using SOSL, specify the object in the request
- Example below is an example where marketing has requested a way to search only campaigns on the internal site
FIND {term} RETURNING ObjectTypeName
FIND {march 2016 email} RETURNING Campaign
- Search Within Multiple Objects
- Imagine the company wants to create a way for customers to search multiple types of information - use multiple object search - multiple record types
- Could return videos, articles, questions, PDFs, etc
- Imagine the company wants to create a way for customers to search multiple types of information - use multiple object search - multiple record types
FIND {term} RETURNING ObjectTypeName1, ObjectTypeName2, ObjectTypeNameYouGetTheIdea
FIND {recycled materials} RETURNING Product2, ContentVersion, FeedItem
- Search Within Custom Objects
- Just add the
__c
suffix
- Just add the
FIND {pink hi\-top} RETURNING Merchandise__c
- SOQL would not have been the ideal way to search for the preceding examples
Optimize Search Results
Describe how to create efficient text searches that return more targeted search results. Describe how to create auto-suggest functionality for searches. List more actions that admins can take to enhance search results for users.- Create Efficient Text Searches
- Searches can be expensive - two basic strategies to make them more efficient:
- Limit which data you’re searching through
- Limit which data you’re returning
- To limit which data is searched, use
IN SearchGroup
- you can search forname
,email
,phone
,sidebar
, orall fields
- Searches can be expensive - two basic strategies to make them more efficient:
FIND {[email protected]} IN EMAIL FIELDS RETURNING Contact
- If there are many records returned, based to limit it using the
RETURNING FieldSpec
:ObjectTypeName
: Specifies the object to return.FieldList
: Specifies the fields to return.ORDER By
: Specifies which fields to order the results by. You can also specify ascending or descending order.LIMIT n
: Sets the maximum number of records returned for the given object.OFFSET n
: Sets the starting row offset into the result set returned by your query.- Examples:
FIND {Cloud Kicks} RETURNING Account
FIND {Cloud Kicks} RETURNING Account(Name, Industry)
FIND {Cloud Kicks} RETURNING Account (Name, Industry ORDER BY Name)
FIND {Cloud Kicks} RETURNING Account (Name, Industry ORDER BY Name LIMIT 10)
FIND {Cloud Kicks} RETURNING Account (Name, Industry ORDER BY Name LIMIT 10 OFFSET 25)
WITH
statements fielter records by certain predefined fields, which returns fewer results and improves performance. A few types:WITH DIVISION
,WITH DATA CATEGORY
,WITH NETWORK
,WITH PRICEBOOK
, etc- Examples:
FIND {Cloud Kicks} RETURNING Account (Name, Industry)
WITH DIVISION = 'Global'
FIND {race} RETURNING KnowledgeArticleVersion
(Id, Title WHERE PublishStatus='online' and language='en_US')
WITH DATA CATEGORY Location__c AT America__c
FIND {first place} RETURNING User (Id, Name),
FeedItem (id, ParentId WHERE CreatedDate = THIS_YEAR Order by CreatedDate DESC)
WITH NETWORK = '00000000000001'
Find {shoe} RETURNING Product2 WITH PricebookId = '01sxx0000002MffAAE'
- SOQL works similarly to SOSL:
Example | SOSL | SOQL |
---|---|---|
Limit data searched | IN SearchGroup |
WHERE |
Specify data to be returned in the response | RETURNING FieldSpec |
SELECT |
Sort Results | ORDER BY , LIMIT , OFFSET |
ORDER BY , LIMIT , OFFSET |
Filter by data category | WITH DATA CATEGORY |
WITH DATA CATEGORY |
- Display Suggested Results
- You can use APIs so that suggestions appear as the user types into the search bar
- Suggestions return records that have a title that matches what the user is typing
- Here are the go-to REST resources - each has a similar syntax and parameters:
Search Suggested Records
: returns a list of suggested records whose names match the user’s search string - provides a shortcut to relevant records before performing a full searchSearch Suggested Article Title Matches
: returns a list of Salesforce Knowledge articles whose titles match the user’s searchSObject Suggest Articles for Case
: returns a list of suggested Salesforce Knowledge articles for a case
/vXX.X/search/suggestTitleMatches?q=search string&language=article language&publishStatus=article publication status
/vXX.X/search/suggestTitleMatches?q=race+tips&language=en_US&publishStatus=Online
JSON response:
{
"autoSuggestResults" : [ {
"attributes" : {
"type" : "KnowledgeArticleVersion",
"url" : "/services/data/v30.0/sobjects/KnowledgeArticleVersion/ka0D00000004CcQ"
},
"Id" : "ka0D00000004CcQ",
"UrlName" : "tips-for-your-first-trail-race",
"Title" : "race tips",
"KnowledgeArticleId" : "kA0D00000004Cfz",
"isMasterLanguage" : "1",
} ],
"hasMoreResults" : false
}
- Work with Admins
- Working with admins is kdey for setting up and optimizing synonym groups, which are words or phrases that are treated equally in searches
- Ex: USB, thumb drive, flash stick, memory stick
- Access via: Setup > Quick Find > “Synonyms” > Custom Synonym Groups
- Promoted Search Terms related list can be used to make some articles appear higher on the search results
- Working with admins is kdey for setting up and optimizing synonym groups, which are words or phrases that are treated equally in searches