Search code examples
searchthinking-sphinx

Thinking Sphinx: Multiple indices for single model?


I'm searching in two different modes using Thinking Sphinx:

  1. Full search on a single model for normal search functionality
  2. Full search across all models for autocomplete dropdown functionality

For the sake of this question, let's say I have a Person and a Country model.

When performing regular searches, I want to fetch all people who's name of country name matches the search string. To achieve this, I have added an index on the countries name in the Person index. All well so far.

When searching to populate my autocomplete dropdown, I want to show all countries and all people matching my search string. Here the problem shows up. When doing an Application-Wide search, I now get:

  1. all countries whose name match my search string
  2. all doctors whose name match my search string, and unfortunately...
  3. all doctors who belongs to a country that matches the search string.

The last part makes for some really confusing autocomplete results for the user. Is there any simple way for me to avoid this by using built-in functionality, for example like having two indices on the Person model, and choose which one to use for each kind of search?


Solution

  • I supposed that your models are like the below:

    class Person < ActiveRecord::Base
      belongs_to :country
      define_index
        indexes :name
        indexes country(:name), :as => country_name
      end
    end
    
    class Country < ActiveRecord::Base
      has_many :people # has_many :persons # depending on your singular/plural case
      define_index
        indexes :name
      end
    end
    

    So, you can get the result without having 3(third condition) by executing the query:

    ThinkingSphinx.search :conditions => {:name => params[:q]}, :classes => [Person, Country]
    

    But, if you want to create multiple indexes on a model it can be done like the sample below:

    class Person < ActiveRecord::Base
      belongs_to :country
      define_index :my_first_in do
        indexes :name
        indexes country(:name)
      end
      define_index :my_second_in do
        indexes :name
      end
    end