0

I'm doing an application search that might filter on several different fields - some of which are not present in every index.

At the moment sorting out the correct classes to search on in the controller, using a complicated if elsif elsif thing.

Is there a way to get thinking sphinx to automatically not search a model if one a condition field isn't present, rather than ignoring the condition as seems to happen at the moment.

Eg

 ThinkingSphinx.search(@query, :conditions => {:genres =>"classical-music"}, :match_mode => :extended, :classes => [Performer, Promoter, Tour, Venue, User], :order => :name_sort, :sort_mode => :asc)

User doesn't have genres, but is included in the search results


update

It definitely doesn't work as I'd like. My user model doesn't have the genres field in the index, so I'd like it to be excluded when I search on genres.

Example searches

User.search("anne")
  Sphinx Query (3.6ms)  anne
  Sphinx  Found 1 result
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` IN (80)
 => [#<User id: 80, first_name: "Anne", last_name: "Bowers", bio: nil, email: "anne@bowers.com", phone: nil, created_at: "2012-11-20 09:36:05", updated_at: "2012-11-20 09:36:05", role: nil, promoter_id: nil, performer_id: nil, job_title: nil>] 


ThinkingSphinx.search("anne", :conditions => {:genres => "music"}, :match_mode => :extended, :classes => [User], :order => :name_sort, :sort_mode => :asc)
  Sphinx Query (3.2ms)  anne @genres music
  Sphinx  Found 1 result
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` IN (80)
 => [#<User id: 80, first_name: "Anne", last_name: "Bowers", bio: nil, email: "anne@bowers.com", phone: nil, created_at: "2012-11-20 09:36:05", updated_at: "2012-11-20 09:36:05", role: nil, promoter_id: nil, performer_id: nil, job_title: nil>] 

It'd be really good if the user was excluded.

Edward
  • 3,429
  • 2
  • 27
  • 43

1 Answers1

0

You can try to pass classes to search in params.

But your way should work too i think.

For me

ThinkingSphinx.search(conditions: { type: "text" })

it just writes an error

Sphinx Daemon returned warning: index bulletin_core,bulletin_delta,user_delta ...: query error: no field 'type' found in schema

but it doesnt crash. It returns only instances of classes which have indexed field type

You also can try a way which is written here https://groups.google.com/forum/?fromgroups=#!topic/thinking-sphinx/rrAPXtxUMjg but I haven't tryed it yet

Pavel
  • 3,900
  • 6
  • 32
  • 41
  • If you add a string query as well as the condition, the conditions get ignored - see my update. – Edward Nov 22 '12 at 12:32
  • it's strange, because when i try to ```ThinkingSphinx.search('Savanna', conditions: { type: "Text" }, classes: [User]) ``` it writes an error ```Sphinx Daemon returned error: index user_core,user_delta: query error: no field 'type' found in schema``` And do not return any results – Pavel Nov 22 '12 at 13:11
  • so I think that passing classes with params it's good idea, for example, you can store this parameter at hidden field, and change it via javascript on some javascript events – Pavel Nov 22 '12 at 13:15