Search code examples
ruby-on-railssolrsunspot

Order Solr results by degrees of friendship


I am currently using Solr 1.4 (soon to upgrade to 3.3). The friendship table is pretty standard:

id | follower_id | user_id 

I would like to perform a regular keyword solr search and order the results by degrees of separation as well as the standard score ordering. From the result set, given the keyword matched any of my immediate friends, they would show up first. Secondly would be the friends of my friends, and thirdly friends by 3rd degree of separation. All other results would come after.

I am pretty sure Solr doesn't offer any 'pre-baked' way of doing this therefore I would likely have to do a join on MySQL to properly order the results. Curious if anyone has done this before and/or has some insights.


Solution

  • I was able to achieve this by performing multiple queries and with the scope "with" to restrict to the id's of colleagues, 2nd and 3rd degree colleagues, using the id's and using mysql to do the select.

    @search_1 = perform_search(1, options)
    @search_2 = perform_search(2, options)
    
    if degree == 1
      with(:id).any_of(options[:colleague_ids])
    elsif degree == 2
      with(:id).any_of(options[:second_degree_colleagues])
    end
    

    It's kinda of a dirty solution as I have to perform multiple solr queries, but until I can use dynamic field sorting options (solr 3.3, not currently supported by sunspot) I really don't know any other way to achieve this.