2010-06-18 76 views
0

我正在嘗試對think_sphinx搜索的搜索結果進行排序。我要排序的列在相關表格:Thinking_sphinx通過關聯排序

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 

然後我打電話這樣的代碼:

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

如果我不使用sort_by_name,然後我得到的返回正確的數值。一旦我添加排序方法,我沒有行。

我也嘗試將它傳遞給搜索方法,結果相同。

回答

1

1)進行排序的字段需要添加:排序=>真

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)需要排序方向添加到訂單選項

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