Search code examples
ruby-on-railsthinking-sphinx

Combining free text and object ids with thinking sphinx


The following index will allow free text searches on the given model's attributes:

ThinkingSphinx::Index.define :firm, :with => :active_record do
  indexes activity
  indexes city
end

for a form that allows input in <%= text_field :firm_search, :terms, :size => 35 %>

However, this class belongs_to :province, thus has a province_id column and class Province has a :name attribute that could be composed of more than one, hopefully, searchable word. Can province.name be integrated into this free text search?


Solution

  • Yes, you can reference associations within your index definition:

    ThinkingSphinx::Index.define :firm, :with => :active_record do
      indexes activity
      indexes city
      indexes province.name, :as => :province_name
    end
    

    Once you've added this, you'll need to run the ts:rebuild rake task to have the data included in your Sphinx indices.