<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - Keepin' Sphinx Indexes Fresh Changes</title>
  <id>tag:opensoul.org,2009:/2009/4/30/keepin-sphinx-indexes-fresh-2/changes</id>
  <generator version="0.8.0" uri="http://mephistoblog.com">Mephisto Drax</generator>
  <link href="http://opensoul.org/2009/4/30/keepin-sphinx-indexes-fresh-2/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2009/4/30/keepin-sphinx-indexes-fresh-2" rel="alternate" type="text/html"/>
  <updated>2009-04-30T01:28:26Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2009-04-30:531:1</id>
    <updated>2009-04-30T01:28:26Z</updated>
    <link href="http://opensoul.org/2009/9/17/keepin-sphinx-indexes-fresh" rel="alternate" type="text/html"/>
    <title>Keepin' Sphinx Indexes Fresh</title>
<content type="html">Stale indexes got you down? Embarrassed that your users&#8217; searches are coming up empty? Act now and you can avoid stale indexes with &lt;span class=&quot;caps&quot;&gt;NEW&lt;/span&gt; and &lt;span class=&quot;caps&quot;&gt;IMPROVED&lt;/span&gt; delayed delta indexing!!

	&lt;p&gt;Ok, maybe it&#8217;s not new and improved. It&#8217;s actually &lt;a href=&quot;http://groups.google.com/group/thinking-sphinx/browse_thread/thread/af6723f764329a41&quot;&gt;been around since January&lt;/a&gt;, but it&#8217;s still awesome. &lt;a href=&quot;http://ts.freelancing-gods.com/&quot;&gt;Thinking Sphinx&lt;/a&gt; can use &lt;a href=&quot;http://github.com/tobi/delayed_job/tree/master&quot;&gt;delayed_job&lt;/a&gt; to keep indexes fresh.&lt;/p&gt;


	&lt;p&gt;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.&lt;/p&gt;


	&lt;p&gt;From what I can tell, delayed delta indexing makes everything Just Work&#8482;, and here&#8217;s how to use it…&lt;/p&gt;


	&lt;p&gt;After you&#8217;ve setup &lt;a&gt;ThinkingSphinx&lt;/a&gt;, set the &lt;code&gt;:delayed&lt;/code&gt; property to &lt;code&gt;:delta&lt;/code&gt; in your index:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class Listing &amp;lt; ActiveRecord::Base
  define_index do
    indexes title
    indexes description
    indexes user.login, :as =&amp;gt; :user

    set_property :delta =&amp;gt; :delayed
  end
end

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:

&amp;lt;filter:jscode lang=&amp;quot;ruby&amp;quot;&amp;gt;
create_table :delayed_jobs, :force =&amp;gt; true do |table| 
   table.integer  :priority, :default =&amp;gt; 0 
   table.integer  :attempts, :default =&amp;gt; 0 
   table.text     :handler 
   table.string   :last_error 
   table.datetime :run_at 
   table.datetime :locked_at 
   table.datetime :failed_at 
   table.string   :locked_by 
   table.timestamps 
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And lastly, all you need to do is fire up the worker process:&lt;/p&gt;


&lt;pre&gt;
$ rake thinking_sphinx:delayed_delta
&lt;/pre&gt;

	&lt;p&gt;Now whenever changes are made to your models, the index will be updated moments later.&lt;/p&gt;</content>  </entry>
</feed>
