how to rank records dynamically in a result set in rails using sunspot solr

244 Views Asked by At

I have users, cafes and their food_items(which have some ingredients listed). Until now i used solr to search for food_items via some ingredients that a user likes. This was accomplished using sunspot-solr search according to the sunspot docs

Also, i am able to gather a relative like-ness of a user to different cafes(based on how many times he has visited it, searched its menu etc)(this is a dynamic value that will be generated on the fly)

Problem: I want to show the same results(food_items) fetched via solr, ranked by cafes(result re-ranking)(based on the like-ness of the user to a cafe) using sunspot solr for rails
This app is hosted on heroku and uses websolr

i have found these:

https://cwiki.apache.org/confluence/display/solr/Query+Re-Ranking

https://cwiki.apache.org/confluence/display/solr/RankQuery+API

but i have no idea as to how i can create a QParserPlugin or generate a rank query in sunspot.

sunspot provides a way to write custom queries. so if i could get help in constructing a query to fetch the like-ness and rank each record (or) any other way to implement such logic, that would be great.
thanks!

1

There are 1 best solutions below

1
On

you can do something like:-

def build_query(where_conditions)
  condition_procs = where_conditions.map{|c| build_condition c}

  Sunspot.search(table_clazz) do
    condition_procs.each{|c| instance_eval &c}
    paginate(:page => page, :per_page => per_page)
  end
end

def build_condition(condition)
  Proc.new do
    # write this code as if it was inside the sunspot search block
    keywords condition['words'], :fields => condition[:field].to_sym
  end
end

conditions = [{words: "tasty pizza", field: "title"},
              {words: "cheap",       field: "description"}]

build_query conditions