4

How can I order the results of a SOSL query by relevance, meaning the result with the most matches of my OR term first?

I have an Apex class that uses SOSL to find potential duplicates for a new bug ticket. It splits the new tickets title:

Killer function fails with limits

into single words, removes stop words and plural 's':

Killer function fail limit

converts it to a soql query term:

Killer* OR function* OR fail* OR limit*

and passes it to a SOSL query:

FIND :termString 
RETURNING Defect__c(Name, Details__c, Severity__c,   Release__r.Name, Sprint__r.Name, StageOfCompletion__c, FoundInRelease__c 
                    WHERE Project__c = :projId
                    ORDER BY LastModifiedDate DESC) 
                    LIMIT 20
5

In a Salesforce Community post, one user says that results "should" be sorted automatically by relevance. To do this in your SOSL query, remove the ORDER BY clause.

Try this:

FIND :termString 
RETURNING Defect__c(Name, Details__c, Severity__c,   Release__r.Name, Sprint__r.Name, 
    StageOfCompletion__c, FoundInRelease__c 
WHERE Project__c = :projId) 
LIMIT 20
| improve this answer | |

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.