If you query from multiple objects the SOSL query returns a List<List<SObject>>, with each inner List sorted by relevance inside. I want to return a combined mixed list sorted by relevance between all objects.. one way to do it would be just combine all these results in apex and then sort by date (but date isn't exactly relevance) - is there a better way to do that, preferably in a single query?

Let's say I have these results:

  Case 1
  Case 2
  Task 1
  Task 2

I want them returned as (or whatever, based on relevance among all results combined):

Task 2
Case 1
Case 2
Task 1

SOSL format: Why does SOSL return a List of a List of sObjects in Apex?

Single object SOSL query: How to order SOSL results by relevance

  • 3
    I don't see anything like WITH RELEVANCE in the SOSL Syntax so that information may not be available. Will be interesting to see if there is an answer...
    – Keith C
    Commented Jul 10, 2016 at 11:52
  • Perhaps if you first show us how you intend to combine them into a single list, that might help someone come up with an idea of how to sort them using comparable. I'd expect you to first need to create a wraper class of some kind since SOSL doesn't return an AggregateResults Query that I'm aware of on Objects. It would seem the next step would be to execute your sort method within the wrapper.
    – crmprogdev
    Commented Jul 10, 2016 at 13:34
  • @crmprogdev, for instance you have a navigation menu displaying your search results, and you have a selection option for each object type to filter, and above each option you have "most popular" or "top results" or something, showing you a list of all of the most relevant items to the search term, and not split by groups.. but those items need to be from specific object types.
    – Poster
    Commented Jul 11, 2016 at 4:55
  • @Poster I understood that you'd be querying multiple objects. What I was saying is "let's see your code" on how you plan to combine your lists into a single list so you can begin to sort them. You first need to create some kind of wrapper class to hold your results. Show us your code and what you've tried. Don't ask us to write all of your code for you.
    – crmprogdev
    Commented Jul 11, 2016 at 13:02
  • I don't think your request is possible. You can assess the relative priority between items in a list. You can assess the relative priority between lists. But you cannot assess the relative priority between items in separate lists. Since the relevance indicator is not exposed, the only way is guesswork.
    – Adrian Larson
    Commented Jul 11, 2016 at 13:10


Browse other questions tagged .