传递多个参数到thinking_sphinx搜索方法绝对不可能吗?

我有一个页面,当访问时会显示最近活跃的用户。 用户上方是一些过滤选项,例如过滤一个或以下组合:

  • 地点
  • 性别
  • 性取向
  • 年龄范围
  • 国家

我正在为表单使用form_tag帮助器。 我的问题是将这些参数传递给我的控制器:

class BrowsersController  params[:page], :per_page => 26 end end 

如果我在一个字段中搜索“搜索”参数,我会很好,但我有多个字段,在我的表单上选择菜单。 我想如何将该信息传递给我的控制器以过滤搜索结果?

我确定我不是第一个在ruby on rails上使用搜索过滤的人

  'get' do %> 

Location:
Gender:
nil %>


亲切的问候

更新

 @users = Profile.search 'HERE IS WHERE THE POWER LIES', :page => params[:page], :per_page => 20, :conditions_all => { :gender => params[:gender], :location => params[:location]} 

我使用:conditions_all来获取我的字段参数并在rails服务器日志中我可以看到它们被拾取…现在我只需要一些如何通过思考sphinx看到它们

更新2我在define_index块中有性别并索引位置,因为它似乎我需要至少1个字段。

这有助于回归性别:

 @users = Profile.search params[:location], :page => params[:page], :per_page => 40, :with => { :gender => [params[:gender]]} 

我试图检查位置和性别,它似乎工作但我会双重检查控制台,因为它在1000英国的英国返回1女性,这可能是错的但我会在控制台和编辑双重检查这个更新得当。

我不太清楚你到底在哪里:conditions_all from – 如果你正在处理字段,那么:conditions就是你所追求的:

 @users = Profile.search 'HERE IS WHERE THE POWER LIES', :page => params[:page], :per_page => 20, :conditions => {:gender => params[:gender], :location => params[:location]} 

但是,听起来你有性别作为属性而不是字段 – 所以,你想要过滤它:

 @users = Profile.search 'HERE IS WHERE THE POWER LIES', :page => params[:page], :per_page => 20, :conditions => {:location => params[:location]}, :with => {:gender => params[:gender]} 

正如我在这里所说的那样,我将尝试解释一下Thinking Sphinx以及我建议的解决问题的方法。

假设您有以下Profile模型:

 # == Schema Information # # Table name: access_number_campaigns # # id :integer # latitude :float # longitude :float # created_at :datetime # updated_at :datetime class Profile < ActiveRecord::Base GENDER = {1 => "Male", 2 => "Female"} belongs_to :country has_and_belongs_to_many :sexual_preferences has_and_belongs_to_many :age_ranges end 

你可能有这些模型:class SexualPreference

 class AgeRange < ActiveRecord::Base has_and_belongs_to_many :profiles end class Country < ActiveRecord::Base has_many :profiles end 

然后,您可以在以下模式中定义ThinkingSphinx索引:

 # within model Profile define_index has "RADIANS(latitude)", :as => :latitude, :type => :float has "RADIANS(longitude)", :as => :longitude, :type => :float has sexual_preferences(:id), :as => :sexual_preference_ids has age_ranges(:id), :as => :age_range_ids has country_id, :type => :integer has gender, :type => :integer end 

您可以创建一个类来构建以下ThinkingSphinx查询来处理所有需要的关联和属性:

 Profile.search "your keywords", :page => 1, :per_page => 10, :with => {"@geodist" => 0.0..NUMBER_OF_METERS, :with_all=>{:sexual_preference_ids=>["1", "2", "3"], :age_range_ids=>["1", "2", "3"], :country_id => ["123"], :gender => ["1"]} } 

您可以在上面看到一个Thinking Sphinx搜索查询,其中包含:with和:with_all哈希。 还有一个重要的Sphinx @geodist函数叫做。 我希望你有一个可读的例子和思考Sphinx gem的最佳参考,你可以在这里找到:

  • 思考狮身人面像参考
    • 关于索引的部分
    • 关于searchin的部分
    • 关于geodist的部分

我希望你喜欢阅读我的例子和非常清晰的思考Sphinx参考。