5

When you search with SOSL but do not specify an ORDER BY clause, does it sort by any particular field?

Example: FIND {test@example.com} IN EMAIL FIELDS RETURNING Contact(Id, Name), Lead(Id, Name)

What order are the contacts/leads sorted by?

I noticed it appears to be the same as the order of the results when you search on SFDC, which shows "Recent" records.

9

For all intents and purposes, the default order is indeed the same for both SOSL and Global Search, as they use the same search indexes and algorithms. This places the API and Global Search on equal footing; a developer can rely on their own app (e.g. a mobile app) returning the same types of results that would be expected in the UI.

Search ranking is explained in How Are Search Results Ordered?, which I'll summarize here.


Frequency: The more times the terms appear, the record is ranked higher.

Uniqueness: Prioritizes words with low cardinality (i.e. only appear in a few records).

Location: Name, Title, and Subject fields have higher priority when a term matches. In other words, given a Task with a Subject "Hello World", and another task with "Hello World" in the description, all other things equal, the first task would rank higher.

Proximity: Multi-word terms will prefer records where those words are closer together, and in the search order. For example, searching "hello world" will rank "Say This: Hello World" higher than "Everyone in the world says hello!"

Ownership: Records you own get a higher ranking than records other people own.

Length (Knowledge Only): Normalizes the search term count so that longer documents that may not be as relevant do not dominate search rankings.

Record Attachment (Knowledge Only): Attachments are not given priority when more copies of the search term are present.

Record Activity: Prefer records that are more recently viewed or updated, and records with more related records.


Note that the exact search algorithm is tweaked pretty regularly, and so it's not particularly published. The only public information we know are the broad strokes of what's involved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.