简体   繁体   中英

Is passing multiple params to thinking_sphinx search method absolutely impossible?

I have a page that when visted brings up the most recently active users. Above the users are some filtering options such as filtering by one or a combination of:

  • location
  • gender
  • sexual preference
  • age range
  • country

I'm using a form_tag helper for my form. My issue is passing each of these parameters to my controller:

class BrowsersController < ApplicationController
  def index
    @default_image = "/assets/default_avatar.jpg"
    @users = Profile.search params[:search], :page => params[:page], :per_page => 26

  end
end

If I was searching with one field with the param "Search" I would be fine but I have multiple fields, select menu's on my form. How am I suppose to pass that info to my controller in order to filter the search results?

I'm sure I'm not the first to use search filtering in ruby on rails

<%= form_tag browsers_path, :method => 'get' do %>
  <p>
    Location: <%= text_field_tag :location %><br />
    Gender: <%= select_tag :gender, 
               options_for_select([["Select", nil], 
                  ["Male", 1], 
                 ["Female", 2]]) %>
<br />
    <%= submit_tag "Search", :name => nil %>
  </p>
  <% end %>
<br />

Kind regards

update

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

I use :conditions_all to get my field params and in rails server logs I can see that they are being picked up.. now I just need to some how get them all seen by thinking sphinx

Update 2 i have has gender in the define_index block and indexes location because it seems i need at least 1 field.

this working to return genders:

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

I've tried to check for both location and gender and it seems to work but I'll double check in console because it's returning 1 female in united kingdom out of 1000 and that could be wrong but I'll double check in console and edit this update appropriately.

I'm not quite sure where you got :conditions_all from - if you're dealing with fields, then :conditions is what you're after:

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

But, it sounds like you've got gender as an attribute instead of a field - and so, you want to filter on it instead:

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

As I said here , I will try to explain a bit of Thinking Sphinx and my suggested approach to solve your problem.

Let's say you have the following Profile model:

# == 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

And you may have those models: class SexualPreference < ActiveRecord::Base has_and_belongs_to_many :profiles end

class AgeRange < ActiveRecord::Base
  has_and_belongs_to_many :profiles
end

class Country < ActiveRecord::Base
  has_many :profiles
end

Then you may define your ThinkingSphinx index in the following schema:

# 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

And you can create a class to build the following ThinkingSphinx query to handle all needed associations and attributes:

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"]} }

You can see above a Thinking Sphinx search query with :with and :with_all hashes included. There is also an important Sphinx's @geodist function called. I hope you have readable example and the best reference for Thinking Sphinx gem you can find here:

I hope you enjoy reading my example and very clear Thinking Sphinx reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM