Confused about search() method

19 views
Skip to first unread message

K.M.

unread,
Jun 3, 2012, 11:02:18 PM6/3/12
to thinkin...@googlegroups.com
hi there,
  I am in the midst of moving an existing app built in rails 2.3.8 to rails 3.

My searching functionality with thinking sphinx does not work anymore.

 When I search with params[:search] by itself, it works as in, I get my results but when I pass the flags for page and sorting in, it results no results.

 To illustrate,

a) this works

        @search =   Part.search( params[:search])


Started POST "/search" for 130.194.80.159 at 2012-06-03 19:54:08 -0700
Processing by SearchController#perform_search as HTML
  Parameters: {"utf8"=>"â", "fit"=>{"make_id"=>"", "vehicle_id"=>""}, "search"=>{"category_id"=>"1", "sub_category_id"=>""}, "commit"=>"Search"}
  Sphinx Query (3.0ms)  {"category_id"=>"1", "sub_category_id"=>"", "fits_make_id_equals"=>"", "fits_vehicle_id_equals"=>""}
  Sphinx  Found 0 results
  Rendered search/results.html.erb within layouts/application (0.7ms)
Completed 200 OK in 83ms (Views: 7.5ms | ActiveRecord: 2.3ms | Sphinx: 3.0ms)


b) this does not work

        @search =   Part.search( params[:search] , :page => param[:page]  ,  'sort_by' => 'updated_at' , 'sort_mode' => 'desc' )



Started POST "/search" for ...   at 2012-06-03 19:54:08 -0700
Processing by SearchController#perform_search as HTML
  Parameters: {"utf8"=>"â", "fit"=>{"make_id"=>"", "vehicle_id"=>""}, "search"=>{"category_id"=>"1", "sub_category_id"=>""}, "commit"=>"Search"}
  Sphinx Query (3.0ms)  {"category_id"=>"1", "sub_category_id"=>"", "fits_make_id_equals"=>"", "fits_vehicle_id_equals"=>""}
  Sphinx  Found 0 results
  Rendered search/results.html.erb within layouts/application (0.7ms)
Completed 200 OK in 83ms (Views: 7.5ms | ActiveRecord: 2.3ms | Sphinx: 3.0ms)

This is very puzzling and any help will be appreciated.

Thank you, all :)

K.M.

unread,
Jun 4, 2012, 2:21:49 AM6/4/12
to thinkin...@googlegroups.com
Figured this out.
Also, sorry for the illustrations above.
The one in "a) this works" should have read:

Started POST "/search" for ... at 2012-06-03 19:54:44 -0700
Processing by SearchController#perform_search as HTML
  Parameters: {"utf8"=>"â", "fit"=>{"make_id"=>"", "vehicle_id"=>""}, "search"=>{"category_id"=>"1", "sub_category_id"=>""}, "commit"=>"Search"}
  Sphinx Query (3.0ms)
  Sphinx  Found 3 results
  Part Load (0.4ms)  SELECT `parts`.* FROM `parts` WHERE `parts`.`id` IN (2, 3, 4)
  Rendered search/results.html.erb within layouts/application (0.7ms)
Completed 200 OK in 93ms (Views: 7.2ms | ActiveRecord: 3.2ms | Sphinx: 3.0ms)

Anyway, from my observations,
  1. when the query is successful, I observed the Sphinx Query does not have any parameters.
    • successful query:  Sphinx Query (3.0ms) 
    • unsuccessful query: Sphinx Query (3.0ms)  {"category_id"=>"1", "sub_category_id"=>"" 
  2. I then dug deeper and read field conditions in the documentation of thinking sphinx. I changed the syntax to use :condition and it works as required.

        # Thinking sphinx's search() method freaks out when elements are not
        # populated (ie. is ""). We sanitize the search parameters handed to
        # ts' search() method before anything is done.
        params[:search].each do |key, value|
            if value.nil? or value == ""  then
                params[:search].delete(key)
            end
        end
        # Perform search in descending order of the update timestamp
        @search = Part.search :conditions => params[:search],
            :page       => params[:page],
            :sort_by    => 'updated_at',
            :sort_mode  => 'desc'
      end


Thank you, all :)
Reply all
Reply to author
Forward
0 new messages