Thinking Sphinx for Rails 3

Sphinx is the de facto choice for Rails developers looking for a search solution or a full text search server. I’ve been using it for MySQL projects but there are other and potentially better options like Solr.

Install Thinking Sphinx on OS X

curl -O http://sphinxsearch.com/downloads/sphinx-0.9.9.tar.gz
tar zxvf sphinx-0.9.9.tar.gz
cd sphinx-0.9.9
./configure --prefix=/usr/local --with-mysql=/usr/local/mysql
make
sudo make install

Your Gemfile

gem 'thinking-sphinx', git: "git://github.com/freelancing-god/thinking-sphinx.git", branch: "rails3"

Capistrano Tasks for Thinking Sphinx

namespace :deploy do

  desc "Config Search"
  task :search_config, :roles => :app do
    run "cd #{current_path} && rake ts:config RAILS_ENV=production"
  end

  desc "Start Search"
  task :search_start, :roles => :app do
    run "cd #{current_path} && rake ts:start RAILS_ENV=production"
  end

  desc "Stop Search"
  task :search_stop, :roles => :app do
    run "cd #{current_path} && rake ts:stop RAILS_ENV=production"
  end

  desc "Rebuild Search"
  task :search_rebuild, :roles => :app do
    run "cd #{current_path} && rake ts:stop RAILS_ENV=production"
    run "cd #{current_path} && rake ts:config RAILS_ENV=production"
    run "cd #{current_path} && rake ts:index RAILS_ENV=production"
    run "cd #{current_path} && rake ts:start RAILS_ENV=production"
  end

  desc "Index Search"
  task :search_index, :roles => :app do
    run "cd #{current_path} && rake ts:in RAILS_ENV=production"
  end

  desc "Re-establish symlinks"
  task :copy_sphinx do
    run <<-CMD
      rm -rf #{current_path}/db/sphinx &&
      ln -nfs #{shared_path}/db/sphinx #{current_path}/db/sphinx
    CMD
  end
end