Thinking_sphinx通过关联排序

时间:2010-06-18 18:57:49

标签: ruby-on-rails ruby sorting thinking-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,那么我会得到正确数量的返回值。一旦我添加了sort方法,我就没有行。

我也尝试将其传递给搜索方法,结果相同。

1 个答案:

答案 0 :(得分:1)

1)对您需要添加的字段进行排序:sortable =&gt;真

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