Keepin' Sphinx Indexes Fresh
<infomercial-voice>Stale indexes got you down? Embarrassed that your users’ searches are coming up empty? Act now and you can avoid stale indexes with NEW and IMPROVED delayed delta indexing</infomercial-voice>
Ok, maybe it’s not new and improved. It’s actually been around since January, but it’s still awesome. Thinking Sphinx can use delayed_job to keep indexes fresh.
I was slow at jumping on the Sphinx bandwagon for one reason: the index has to be rebuilt to incorporate new data. Delta indexing alleviated some of this by storing frequent changes in a small separate index, but it still had to be occasionally reindexed. It also seemed to only index existing records in my trials with it. New records didn’t ever seem to show up until I rebuilt the whole index.
From what I can tell, delayed delta indexing makes everything Just Work™, and here’s how to use it…
After you’ve setup ThinkingSphinx, set the :delayed
property to :delta
in your index:
The delayed delta support depends on delayed_job, but if you’re using the gem version, it’s already bundled in. I’m using delayed job for some other things in my project, so I still have it installed separately and that seems to be working just fine.
delayed_job uses a table to keep track of the jobs that need run, so create a migration containing:
And lastly, all you need to do is fire up the worker process:
$ rake thinking_sphinx:delayed_delta
Now whenever changes are made to your models, the index will be updated moments later. And that’s how you keep it fresh!