<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Slash Dot Dash: Rails searching with Sphinx</title>
    <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Rolling on Rails</description>
    <item>
      <title>Rails searching with Sphinx</title>
      <description>&lt;p&gt;Over the weekend I was implementing search for &lt;a href="http://www.trawlr.com"&gt;trawlr.com&lt;/a&gt; using &lt;a href="http://www.sphinxsearch.com/"&gt;Sphinx&lt;/a&gt;, the nginx of the search world (fast and Russian) according to &lt;a href="http://blog.evanweaver.com/articles/2007/07/09/ultrasphinx-searching-the-world-in-231-seconds"&gt;Evan Weaver&lt;/a&gt;. Previously I was using &lt;a href="http://www.slashdotdash.net/articles/2007/01/09/quick-ferret-primer-with-examples"&gt;Ferret&lt;/a&gt;, but I had to remove the search feature almost immediately due to the ferret indexes constantly corrupting and causing me a major headache. I decided to drop ferret in favour of Sphinx which I&amp;#8217;ve lots of &lt;a href="http://www.mysqlperformanceblog.com/2006/10/30/using-sphinx-as-mysql-data-retrieval-accelerator/"&gt;good things&lt;/a&gt; about recently.&lt;/p&gt;


	&lt;p&gt;Installation on my MacBook Pro required a slight adjustment of the mysql directories with mysql5 from MacPorts.&lt;/p&gt;


&lt;pre&gt;
$ wget http://www.sphinxsearch.com/downloads/sphinx-0.9.7.tar.gz
$ tar xvzf sphinx-0.9.7.tar.gz
$ cd sphinx-0.9.7
$ ./configure --with-mysql-includes=/opt/local/include/mysql5/mysql/ --with-mysql-libs=/opt/local/lib/mysql5/mysql/
$ make
$ sudo make install
&lt;/pre&gt;

	&lt;p&gt;Initally, I chose to use Evan&amp;#8217;s &lt;a href="http://blog.evanweaver.com/files/doc/fauna/ultrasphinx/files/README.html"&gt;UltraSphinx&lt;/a&gt; plugin and it was very helpful to start with by auto-generating the sphinx.conf. After indexing the entire content of trawlr.com &amp;#8211; almost 1.5 million blog posts &amp;#8211; in just a few minutes I was suitably impressed. The search speed was also lightening fast. Unfortunately I had problems with my Rails app with the UltraSphinx plugin installed &amp;#8211; very strange errors started occurring.&lt;/p&gt;


	&lt;p&gt;Having already looked at the alternative Sphinx plugins I decided to try &lt;a href="http://www.datanoise.com/articles/2007/3/23/acts_as_sphinx-plugin"&gt;acts_as_sphinx&lt;/a&gt;. After some small tweaks to the sphinx.conf file (and a re-index) the search was working and more importantly so was my Rails app. An alternative option is &lt;a href="http://seattlerb.rubyforge.org/Sphincter/"&gt;Sphincter&lt;/a&gt; which I did experiment with but struggled with the limited documentation &amp;#8211; mostly concerning the configuration file required but &lt;span class="caps"&gt;YMMV&lt;/span&gt;.&lt;/p&gt;


&lt;pre&gt;
$ rake sphinx:index
$ rake sphinx:start
&lt;/pre&gt;

	&lt;p&gt;Indexing on my MacBook Pro&amp;#8230;&lt;/p&gt;


&lt;pre&gt;
$ time rake sphinx:index

using config file 'sphinx.conf'...
indexing index 'items'...
collected 1455733 docs, 1255.2 MB
sorted 182.4 Mhits, 100.0% done
total 1455733 docs, 1255246639 bytes
total 438.695 sec, 2861316.50 bytes/sec, 3318.32 docs/sec

real    7m25.307s
user    4m28.963s
sys     0m17.578s
&lt;/pre&gt;

	&lt;p&gt;Searching with &lt;code&gt;acts_as_sphinx&lt;/code&gt; via the console (&lt;code&gt;ruby script/console&lt;/code&gt;) for the term &amp;#8216;Google&amp;#8217;, sorted by published date.&lt;/p&gt;


&lt;pre&gt;
&amp;gt;&amp;gt; search = Item.find_with_sphinx 'Google', :sphinx =&amp;gt; {:sort_mode =&amp;gt; [:attr_desc, 'pub_date'], :page =&amp;gt; 1}, :order =&amp;gt; 'items.pub_date DESC'; 0
=&amp;gt; 0
&amp;gt;&amp;gt; search.total
=&amp;gt; 1000
&amp;gt;&amp;gt; search.total_found
=&amp;gt; 73717
&amp;gt;&amp;gt; search.time       
=&amp;gt; "0.000" 
&lt;/pre&gt;

	&lt;p&gt;That&amp;#8217;s with an index of 943Mb and almost 1.5 million items. Note that the search results are limited to 1,000 items due to the settings in my &lt;code&gt;spinx.conf&lt;/code&gt; file.&lt;/p&gt;


	&lt;p&gt;Within the Rails controller, search is done via:&lt;/p&gt;


&lt;pre&gt;
@items = Item.find_with_sphinx(params[:query], 
      :sphinx =&amp;gt; {:sort_mode =&amp;gt; [:attr_desc, 'pub_date'], :limit =&amp;gt; 50, :page =&amp;gt; (params[:page] || 1)}, 
      :order =&amp;gt; 'items.pub_date DESC')
&lt;/pre&gt;

	&lt;h2&gt;Updating the Sphinx index&lt;/h2&gt;


	&lt;p&gt;There&amp;#8217;s another rake task for updating the Sphinx index which can be called via a cron job, rather than &amp;#8216;live&amp;#8217; updates. The rotate command allows the index to be rebuilt whilst the Sphinx daemon is running, forcing a restart once completed.&lt;/p&gt;


&lt;pre&gt;
$ rake sphinx:rotate
&lt;/pre&gt;

	&lt;h2&gt;Update&lt;/h2&gt;


	&lt;ul&gt;
	&lt;li&gt;It looks like the UltraSphinx plugin requires edge Rails (thanks Evan)!&lt;/li&gt;
		&lt;li&gt;I&amp;#8217;ve deployed the search updates to the live &lt;a href="http://www.trawlr.com/reader/"&gt;trawlr.com&lt;/a&gt; site (currently search is only visible from the &lt;em&gt;reader&lt;/em&gt; view for logged in users).&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Live search is fantastically quick&lt;/strong&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://pastie.caboo.se/87943"&gt;Created some new rake tasks&lt;/a&gt; (to go in &lt;code&gt;lib/tasks/sphinx.rake&lt;/code&gt;) that allow you to have a sphinx.conf file per Rails environment (&lt;code&gt;config/sphinx.development.conf&lt;/code&gt; and &lt;code&gt;config/sphinx.production.conf&lt;/code&gt;). The available tasks are: &lt;code&gt;s:index&lt;/code&gt;, &lt;code&gt;s:rotate&lt;/code&gt;, &lt;code&gt;s:start&lt;/code&gt;, &lt;code&gt;s:stop&lt;/code&gt;, &lt;code&gt;s:status&lt;/code&gt; and &lt;code&gt;s:restart&lt;/code&gt;. The rake tasks assume that the Sphinx pid file is in the log directory (&lt;code&gt;pid_file = log/searchd.pid&lt;/code&gt;).&lt;/li&gt;
	&lt;/ul&gt;</description>
      <pubDate>Mon, 06 Aug 2007 20:30:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:fd2fdc8d-d688-477f-9108-34d35177a26a</guid>
      <author>ben@slashdotdash.net (Ben)</author>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx</link>
      <category>Ruby on Rails</category>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by Jan</title>
      <description>&lt;p&gt;Found it. The above error happened, because I set max_matches = 120. I found out, when I installed Dmytros client API and ran a query.&lt;/p&gt;


	&lt;p&gt;Sphinx::SphinxInternalError: searchd error: per-query max_matches=1000 out of bounds (per-server max_matches=120)
    from /Users/jan/Documents/test/Sphinx/vendor/plugins/sphinx-0.3.1/lib/client.rb:622:in `GetResponse&amp;#8217;
    from /Users/jan/Documents/test/Sphinx/vendor/plugins/sphinx-0.3.1/lib/client.rb:362:in `Query&amp;#8217;
    from (irb):2&lt;/p&gt;


	&lt;p&gt;Setting @maxmatches = 120 in acts_as_sphinx&amp;#8217; sphinx.rb fixed the problem.&lt;/p&gt;


	&lt;p&gt;Passing on this helpful error message instead of &amp;#8220;112&amp;#8221; would be great.&lt;/p&gt;</description>
      <pubDate>Fri, 28 Mar 2008 10:29:28 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:1c83cb0e-1f72-472e-acf8-4889b68fcf4a</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-413</link>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by Jan</title>
      <description>&lt;p&gt;Another question on the per-environment sphinx configs. How did you modify acts_as_sphinx to be using different servers for different environments?&lt;/p&gt;</description>
      <pubDate>Fri, 28 Mar 2008 10:10:07 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:7966048a-a5e9-4c4e-89b8-e21dbd26e4e2</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-412</link>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by Jan</title>
      <description>&lt;p&gt;I cannot get it running on rails 1.2.3 with sphinx 0.9.7. :-/&lt;/p&gt;


	&lt;p&gt;@articles = Article.find_with_sphinx(&amp;#8216;Jan&amp;#8217;, :sphinx =&amp;gt; {:page =&amp;gt; @page})&lt;/p&gt;


	&lt;p&gt;Sphinx::SphinxInternalError in ArticleController#fulltext_search
searchd error: 112&lt;/p&gt;


	&lt;p&gt;/Users/jan/Documents/Platinnetz/vendor/plugins/acts_as_sphinx/lib/sphinx.rb:437:in `get_response&amp;#8217;
/Users/jan/Documents/Platinnetz/vendor/plugins/acts_as_sphinx/lib/sphinx.rb:232:in `query&amp;#8217;
/Users/jan/Documents/Platinnetz/vendor/plugins/acts_as_sphinx/lib/acts_as_sphinx.rb:83:in `ask_sphinx&amp;#8217;
/Users/jan/Documents/Platinnetz/vendor/plugins/acts_as_sphinx/lib/acts_as_sphinx.rb:112:in `find_with_sphinx&amp;#8217;
/Users/jan/Documents/Platinnetz/app/controllers/article_controller.rb:353:in `fulltext_search&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel/rails.rb:76:in `process&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel/rails.rb:74:in `synchronize&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel/rails.rb:74:in `process&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel.rb:159:in `process_client&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel.rb:158:in `each&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel.rb:158:in `process_client&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel.rb:285:in `run&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel.rb:285:in `initialize&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel.rb:285:in `new&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel.rb:285:in `run&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel.rb:268:in `initialize&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel.rb:268:in `new&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel.rb:268:in `run&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel/configurator.rb:282:in `run&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel/configurator.rb:281:in `each&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel/configurator.rb:281:in `run&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/bin/mongrel_rails:128:in `run&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/lib/mongrel/command.rb:212:in `run&amp;#8217;
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.3/bin/mongrel_rails:281&lt;/p&gt;</description>
      <pubDate>Fri, 28 Mar 2008 10:09:07 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:267ab704-a777-4e11-b9e1-1285edc4e4de</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-411</link>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by seog</title>
      <description>&lt;p&gt;Thanks for the tutorial! I am looking for a good search engine for rails and it seems more people are mentioning Sphinx over Ferret due to stability issues in deployment.&lt;/p&gt;</description>
      <pubDate>Tue, 22 Jan 2008 18:05:28 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:b3a4d35c-6c4a-468a-9099-f47e1e03e68d</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-366</link>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by urlsbox</title>
      <description>&lt;p&gt;Very useful information. Thanks !&lt;/p&gt;</description>
      <pubDate>Mon, 03 Dec 2007 12:30:45 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:9ab0526f-ea7b-424a-8419-de3866d7af05</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-264</link>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by nguma</title>
      <description>&lt;p&gt;did you manage to use the filters? 
can&amp;#8217;t seem to find the correct syntax.&lt;/p&gt;


	&lt;p&gt;tried :sphinx =&amp;gt; {:filter =&amp;gt; [&amp;#8216;attribute_name&amp;#8217;,[1,2,3]]} without any success&lt;/p&gt;</description>
      <pubDate>Wed, 28 Nov 2007 19:36:56 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e93c5cc5-0158-4832-9643-647b631511e6</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-236</link>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by Eric Hodel</title>
      <description>&lt;p&gt;Sphincter runs on 1.2.3 and has per-environment sphinx.conf files built-in.&lt;/p&gt;</description>
      <pubDate>Fri, 31 Aug 2007 17:39:47 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:1e9ca003-5488-43f8-9f41-eaab158b2e2f</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-157</link>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by Ben</title>
      <description>&lt;p&gt;&lt;code&gt;acts_as_sphinx&lt;/code&gt; doesn&amp;#8217;t require Edge; I&amp;#8217;m using it with Rails 1.2.3.&lt;/p&gt;</description>
      <pubDate>Fri, 31 Aug 2007 14:57:34 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:ea43ba16-e85d-4bd7-9d2c-5753dd499138</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-155</link>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by Joey Marchy</title>
      <description>&lt;p&gt;Thanks for the Article Ben. I too am having issues with ferret and indexes so I am looking to implement Sphinx as my search also. I saw the UltraSphinx plugin requires running rails edge. Do you know if the acts_as_spinx will run without edge?&lt;/p&gt;


	&lt;p&gt;I am currently running rails 1.2.3&lt;/p&gt;</description>
      <pubDate>Fri, 31 Aug 2007 14:45:10 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:df3d62e7-047b-4471-82d3-da4008ca7bd6</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-154</link>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by Eric Hodel</title>
      <description>&lt;p&gt;For Sphincter there is no configuration file.  I built it that way on purpose.&lt;/p&gt;


	&lt;p&gt;If you need to tweak something you can, but most of the time you won&amp;#8217;t.&lt;/p&gt;</description>
      <pubDate>Tue, 21 Aug 2007 02:35:38 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:f529864b-e9b8-4501-a81c-b949e0c486aa</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-150</link>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by evan</title>
      <description>&lt;p&gt;Yeah, sorry about that. I added a note to the documentation.&lt;/p&gt;</description>
      <pubDate>Tue, 07 Aug 2007 00:00:07 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:050ecc24-2e6a-4d40-8a6c-698050ac3147</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-123</link>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by Ben</title>
      <description>&lt;p&gt;@evan &amp;#8211; Ah, that&amp;#8217;s probably why it all went &lt;em&gt;tits-up&lt;/em&gt; &amp;#8211; I&amp;#8217;m running Rails 1.2.3 rather than Edge.&lt;/p&gt;</description>
      <pubDate>Mon, 06 Aug 2007 21:02:53 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:2e8619e3-4c76-4421-a330-dec7f8e6239f</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-122</link>
    </item>
    <item>
      <title>"Rails searching with Sphinx" by evan</title>
      <description>&lt;p&gt;What errors did you get? It might not work well if you&amp;#8217;re not on edge Rails.&lt;/p&gt;</description>
      <pubDate>Mon, 06 Aug 2007 20:56:59 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:3f02dd37-9222-4f02-9556-1ed75d8a96bc</guid>
      <link>http://www.slashdotdash.net/articles/2007/08/06/rails-searching-with-sphinx#comment-121</link>
    </item>
  </channel>
</rss>
