If you ever have developed search solutions for a web application you probably are familiar with the hazzle attached to it. There are lots of stuff to think about and performance is often an issue when dealing with large sets of data. One of the key benefits with a dedicated search engine such as Sphinx is that it is rapidly fast and can easily be customized to fit the needs for the application.
We were using a gem called ActsAsIndexed in one of our projects, however, when the datasets started to get bigger we noticed some really poor performance (we had request times in the 10s range) and had to look for other solutions to solve the problem. After some research I stumbled upon Sphinx and the gem Thinking Sphinx. I played around with it for a while and absolutely fell in love with it! So, to make it more easy next time i’ll post some notes here to freshen up my memory when needed 🙂 The notes are for Unix but should work perfectly for OSx too.
Installing and setting up Sphinx with Thinking Sphinx with Rails (version 3 and up)
When installing Sphinx, it is highly recommended NOT to use symlinks
- Download the latest Sphinx version tarball from here
wget http://sphinxsearch.com/files/sphinx-x.x.x-release.tar.gz - Navigate to the folder with the tarball and run
tar xzvf sphinx-x.x.x-release.tar.gz - cd sphinx-x.x.x
- ./configure
- make
- sudo make install
- Next up is to install Thinking Sphinx, it’s easy! Just put gem “thinking-sphinx” in your Gemfile and run bundle install
- After this you need to setup your indexes, I won’t go into this here since it is pretty straight forward but have a look here for more information
- rake ts:index
- rake ts:start
- DONE!
Model.search("query", :with => { :published => true })
Model.search("query", :without => { :draft => true})
:field_weights => { "name" => 100, "nickname" => 50 }
set_property :field_weights => {...}
:sort_mode => :expr, :sort_by => "@weight * ranking"
define_index "my_own_nifty_index" do indexes translations.title where "post_translations.locale = 'en'" end
Model.search("query", :index => :my_own_nifty_index)
Model.search "query" Model.search_count "query" Model.search_for_ids "query"
Model.search "query", :page => 1, :per_page => 5