ruby on rails - Thinking Sphinx - Sort order -
i have requirement want results based on order of ids pass sphinx:
i did sorting , filtering ruby , find order of user_ids be
user_ids = [1, 3, 2]
now, want apply search on top of this:
user.search_for_ids("test", {:with => {:page => params[:page], :per_page => 25, :sphinx_internal_id => user_ids}, :sql => {:order => "field(id,#{user_ids.join(',')})"}})
i see can pass :sql => :order in options when need sort on sql. not give me result in expected sorting order.
user.where(:id => [1, 2, 3]).order("field(id,#{user_ids.join(',')})")
gives me proper results expected
please correct me if doing wrong here.is there way preserve order respect user_ids ? appreciated.
i don't believe there built-in mechanism predetermined sort order, can sort after search results returned. won't work if you're using sort of pagination:
user_ids = [1,3,2] results = user.search_for_ids #... results.sort_by {|result| user_ids.index(result.id) }
Comments
Post a Comment