A Quick Guide to Getting Setup with Thinking Sphinx

Firstly, you’ll need to install both Sphinx and Thinking Sphinx. While Sphinx is compiling, go read what the difference is between fields and attributes for Sphinx is. It’s important stuff.

Once that’s all done, it’s time to set up an index on your model. In the example below, we’re assuming the model is the Article class - and so we’re going to put this index in app/indices/article_index.rb (the path matters, the file name is arbitrary, but should not be just the model’s name).

ThinkingSphinx::Index.define :article, :with => :real_time do
  # fields
  indexes subject, :sortable => true
  indexes content
  indexes author.name, :as => :author, :sortable => true

  # attributes
  has author_id,  :type => :integer
  has created_at, :type => :timestamp
  has updated_at, :type => :timestamp

The above definition is for a real-time index, and so fields and attributes refer to your model’s methods.

You’ll want to add a callback to your model to ensure any changes flow through to Sphinx:

# if your model is app/models/article.rb:
after_save ThinkingSphinx::RealTime.callback_for(:article)

The next step is to process your data (this task stops the Sphinx daemon if it is running, deletes existing Sphinx data, rewrites the Sphinx configuration file, starts the Sphinx daemon again, and then creates Sphinx documents for each indexed model instance):

rake ts:rebuild

And now we can search!

Article.search "topical issue"
Article.search "something", :order => :created_at,
  :sort_mode => :desc
Article.search "everything", :with => {:author_id => 5}
Article.search :conditions => {:subject => "Sphinx"}

Of course, that’s an extremely simple overview. It’s definitely worth reading some more for a better understanding of the best ways to index and search.