iie labs

Thinking_Sphinx - Easy Setup Tutorial

Posted about 9 years ago by Philip Ingram

In my quest to find an easy peasy full text search tutorial, you often hear about Thinking Sphinx. Yeah it’s good and all, but the documentation left a bit to be desired, as i was running into issues that had to do with passenger and I had to search the net for steps and found snippets that weren’t in the site’s documents but were on email lists, etc… so I decided to write a quick guide.

Ok, this information is on the net somewhere I’m sure, but i just spent too long trying to understand how the heck to get my current Ultrasphinx setup into Thinking_Sphinx. So I set out to change that and write a “Just the Facts” , no clutter guide to setting up Thinking_Sphinx.


Quick Outline of setting up Thinking_Sphinx


So, here is what you do:



  1. Install Sphinx 0.9.9 (you can download it by clicking the link or in my code snippet, i use wget. whatever loser :-))
    wget http://www.sphinxsearch.com/downloads/sphinx-0.9.9-rc2.tar.gz
    tar -xzf sphinx-0.9.9-rc2.tar.gz
    ./configure
    make
    sudo make install

  2. add the gem.config to environment.rb
    config.gem 'thinking-sphinx-099', :lib => 'thinking_sphinx', :version => '1.2.13'

    Notice we’re using thinking-sphinx-099, which is the latest gem for the latest version of Sphinx.
  3. run the gems
    sudo rake gems:install

  4. Open your app’s Rakefile and Add
    require 'thinking_sphinx/tasks'
    as this will get you the rake tasks you’ll need later on and throughout the life of your app.
  5. I needed to be able to search ONE model, so open your model and the equivalent of this code in there:
    
    class User < ActiveRecord::Base
    ....
    	define_index do
    	  indexes name, description, country, status
          # Index only those Users who are Active
    	  where "status = 'Active'"
    	end
    end
  6. Create the Search Controller that you will use:

    
    class SearchController < ApplicationController
    	def search
    	  unless params[:q].blank?
    	    query = params[:q].strip  
    	    page  = params[:page] || 1  
    	    model = params[:model]  
    	    filters = {}
    	    @search = Shop.search(query, :page => page)
    	  end
    	end
    end

  7. Now create a Search View (such as):
    
    <h1>Search Results for ""</h1>
     :get do %>

    nil %>

    (Try using * as a wildcard search.

      Results – .

  8. Let’s add a route to routes.rb:
    map.resources :search, :collection => {:search => :get}

  9. Create this file and add this code to config/sphinx.yml:
    
    development:
      enable_star: 1
      min_infix_len: 1
      bin_path: '/usr/local/bin'
    test:
      enable_star: 1
      min_infix_len: 1
      bin_path: '/usr/local/bin'
    production:
      enable_star: 1
      min_infix_len: 1
      bin_path: '/usr/local/bin'

  10. Now let’s run the rake tasks in the following order:
    
    sudo RAILS_ENV=development rake ts:in
    rake ts:start

    If you are in production switch out development to production and make sure you add sudo RAILS_ENV=production instead

Hope this helps. Leave a comment to let me know if some steps are unclear.

Comments

blog comments powered by Disqus