通过Thinking Sphinx进行计数排序

时间:2012-05-29 10:33:11

标签: ruby-on-rails sphinx

我希望我的搜索引擎能够根据某种案件类型的案件来命令律师。最多的律师已经确定某种类型的案件,他的排名越高。

lawyer.rb

has_many :cases    
has_many :case_types, :through => :cases

define_index do
        indexes case_types.name, :as => :case_types
        has case_types(:id), :as => :case_types_id
        has "SUM(case_types)", :as => :case_type_count #this line gives an error,  as my lawyer table does't have a case_type column, also, I need to count DISTINCT case_types
      end

在我的search_controller.rb中,我想做类似的事情,建议是个案类型的名称

@lawyers = Lawyer.search params[:suggestion], :order => "@case_type_count DESC"

我走错了路吗?我应该想一个更少的面向Sphinx的方法吗?问题是我需要在@lawyers上做一个each_with_geodist,所以我需要让我的律师通过Sphinx搜索。

1 个答案:

答案 0 :(得分:2)

将以下内容添加到您的define_index:

has "COUNT(case_types.id)", :as => :case_type_count, :type => :integer

join case_types

然后通过case_count检索:

Lawyer.search("", :order => "case_type_count desc")

我发现在development.sphinx.conf中读取sql代码很有用,它允许我查看正在生成的列名。