10

In Solr/Lucene is it possible to order first by relevance, and then by a second attribute?

As far as I can tell if I set an ordering parameter, it totally overrides relevance, and sorts by the ordering parameter(s).

How can I have results sorted first by relevance, and then in the case of two entries with exactly the same relevance, giving the nod to the item that, say, comes first alphabetically.

If it makes any difference I'm using Solr through Sunspot in Ruby on Rails.

William Jones
  • 18,089
  • 17
  • 63
  • 98
  • I am wondering if this would work well, since relevance score would vary greatly. I wonder if solr can segment relevance results into many levels, and then sort each of these by the second column. – lulalala Aug 21 '12 at 07:22

1 Answers1

8

Solved my own problem!

The keyword score can be passed to order the result by relevancy.

So in Rails Sunspot terms:

Article.search do
  keywords params[:query]
  order_by :score, :desc
  order_by :name, :asc
end
William Jones
  • 18,089
  • 17
  • 63
  • 98
  • 1
    This might be late catchup, but I tried your same solution and it's not working for me. Whenever I use score for sorting, it uses the second sort option only. If I use score only, it works fine. If I combine two other sort criteria, that also works. is there anything else to do? Are you aware of gem version issues? Appreciate help – Bashar Abdullah Dec 24 '11 at 15:50