If you’ve been following our blog you may have noticed that we’re using Solr and ActsAsSolr plugin for our searching.
Getting started with acts_as_solr
acts_as_solr for development and production in one Tomcat instance
Optimizing Solr and Rails - Index in the background
Unfortunately being Java, Solr is a bit of a memory hog. We’ve attempted all sorts of optimizations, but we’re going to take a leap and switch to Sphinx. Sphinx is a free open-source SQL full-text search engine.
First step is to get Sphinx itself installed. For that have a look at Rob’s post:
http://www.notch8.com/articles/2007/10/15/sphinx-and-ultrasphinx-and-eye-on-search
Ultrasphinx works pretty much out of the box with will_paginate; however, a lot of our work is currently compatible with paginating_find. So to keep rails memory footprint down by using one plugin we’re sticking with paginating_find. Unfortunately Ultrasphinx doesn’t work out of the box with paginating_find, so time for a little hacking…
Install paginating_find plugin:
More information on paginating_find
Install ultrasphinx plugin:
More information on Ultrasphinx
Now for hacking Ultrasphinx plugin to work with Paginating Find plugin. Piston is great for managing plugins; however, it can easily get confused if you’ve hacked a plugin directly. The solution: use a method called “Evil Twin” as mentioned on the Err the Blog: Evil Twin Plugin
Create a directory for the hack:
Now edit the vendor/plugins/ultrasphinx_hacks/init.rb file with the hacks for paginating_find:
Now for adding a method to your controller for the search:
app/controllers/posts_controller.rb
app/views/posts/list.html.erb
Paginating End Result
Bonus : will_paginate styled links
Unfortunately paginating_find doesn’t automatically have the “style” that will_paginate automatically comes with. So to fix that just add a little css for the style and a partial for the pervious and next buttons.
And now for the partial:
app/views/shared/_paginate.html.erb
So in your code replace the code paginating_links call with a render partial:
Styled Paginating End Result
Resources:
ErrTheBlog: Evil Twin Plugin
Will Paginate Plugin
Paginating Find Plugin
Ultrasphinx
Sphinx












Recent Comments