views:

20

answers:

1

I am trying to sort the search results of a thinking_sphinx search. The columns I want to sort are in associated tables:

class Membership < ActiveRecord::Base

  define_index do
   indexes :title
   indexes user.first_name, :as => :first_name
   indexes user.last_name, :as => :last_name

   has :organization_id, :active
   set_property :delta => true
 end

 # begin
 sphinx_scope(:by_organization) do |org|
   {:with => {:organization_id => org.id, :active => true}}
 end

 sphinx_scope(:sort_by_name) do 
   {:order => 'last_name, first_name'}
 end

end

Then I call the code like this:

search_results = Membership.by_organization(Organization.current).sort_by_name.search((search_value || ""), :page => (page || 1), :per_page => 10)

If I don't use the sort_by_name, then I get the correct number of returned values. Once I add the sort method, I get no rows.

I have also tried to pass it into the search method, with the same results.

+1  A: 

1) to sort on fields you need to add :sortable => true

define_index do
  indexes :title
  indexes user.first_name, :as => :first_name, :sortable => true
  indexes user.last_name, :as => :last_name, :sortable => true

  has :organization_id, :active
  set_property :delta => true
end

2) you need to add sort direction to the order option

sphinx_scope(:sort_by_name) do 
  {:order => 'last_name ASC, first_name ASC'}
end
James Healy