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

1 Answer 1

6

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .