I am using acts_as_ferret and ferret server as of now with my rails application and it just works fine for me. The ONLY problem is performance as it takes a lot time to build index and to rebuild index when it gets screwed up and that’s where sphinx rocks!
To get started with thinking sphinx you need to install sphinx server first. for installation help click here.
To use sphinx search in rails we need to use either thinking sphinx gem or plugin that can be easily find on github.
List rake tasks that should show up sphinx related tasks. <div class='code-highlight'><pre class='code-highlight-pre'><div data-line='1' class='code-highlight-row numbered'><div class='code-highlight-line'>rake -T ts
</div></div><div data-line='2' class='code-highlight-row numbered'><div class='code-highlight-line'>(in /home/sandip/v1)
</div></div><div data-line='3' class='code-highlight-row numbered'><div class='code-highlight-line'>rake doc:plugins:acts_as_audited # Generate documentation for the acts_as…
</div></div><div data-line='4' class='code-highlight-row numbered'><div class='code-highlight-line'>rake doc:plugins:acts_as_ferret # Generate documentation for the acts_as…
</div></div><div data-line='5' class='code-highlight-row numbered'><div class='code-highlight-line'>rake rails:update:javascripts # Update your javascripts from your curre…
</div></div><div data-line='6' class='code-highlight-row numbered'><div class='code-highlight-line'>rake rails:update:scripts # Add new scripts to the application scri…
</div></div><div data-line='7' class='code-highlight-row numbered'><div class='code-highlight-line'>rake stats # Report code statistics (KLOCs, etc) fro…
</div></div><div data-line='8' class='code-highlight-row numbered'><div class='code-highlight-line'>rake test:units # Run tests for unitsdb:test:prepare / Ru…
</div></div><div data-line='9' class='code-highlight-row numbered'><div class='code-highlight-line'>rake tmp:sockets:clear # Clears all files in tmp/sockets
</div></div><div data-line='10' class='code-highlight-row numbered'><div class='code-highlight-line'>rake ts:conf # Generate the Sphinx configuration file …
</div></div><div data-line='11' class='code-highlight-row numbered'><div class='code-highlight-line'>rake ts:config # Generate the Sphinx configuration file …
</div></div><div data-line='12' class='code-highlight-row numbered'><div class='code-highlight-line'>rake ts:in # Index data for Sphinx using Thinking Sp…
</div></div><div data-line='13' class='code-highlight-row numbered'><div class='code-highlight-line'>rake ts:rebuild # Stop Sphinx (if it’s running), rebuild …
</div></div><div data-line='14' class='code-highlight-row numbered'><div class='code-highlight-line'>rake ts:reindex # Reindex Sphinx without regenerating the…
</div></div><div data-line='15' class='code-highlight-row numbered'><div class='code-highlight-line'>rake ts:restart # Restart Sphinx / Restart Sphinx
</div></div><div data-line='16' class='code-highlight-row numbered'><div class='code-highlight-line'>rake ts:run # Stop if running, then start a Sphinx se…
</div></div><div data-line='17' class='code-highlight-row numbered'><div class='code-highlight-line'>rake ts:start # Start a Sphinx searchd daemon using Thi…
</div></div><div data-line='18' class='code-highlight-row numbered'><div class='code-highlight-line'>rake ts:stop # Stop Sphinx using Thinking Sphinx’s set…
</div></div><div data-line='19' class='code-highlight-row numbered'><div class='code-highlight-line'>rake ts:version # Output the current Thinking Sphinx vers…</div></div></pre></div>
Starting Sphinx
Start sphinx server this should give up an error. <div class='code-highlight'><pre class='code-highlight-pre'><div data-line='1' class='code-highlight-row numbered'><div class='code-highlight-line'>rake ts:start
</div></div><div data-line='2' class='code-highlight-row numbered'><div class='code-highlight-line'>(in /home/sandip/v1)
</div></div><div data-line='3' class='code-highlight-row numbered'><div class='code-highlight-line'>Failed to start searchd daemon. Check /home/sandip/v1/log/searchd.log.</div></div></pre></div>
Adding indexes in models
define_indexdo
# following fields are database fields
# we can not add model methods in sphinx index
# sphinx fields allows ONLY model based associations
# fields
indexescustomer_name
indexesphone
indexesmobile
indexesother_phone
indexescar_make
indexescar_model
indexesregistration_no
end
Configure sphinx
rake ts:config
Generating Configuration to /home/sandip/v1/config/development.sphinx.conf
Index sphinx
rake ts:in
(in /home/sandip/v1)
Generating Configuration to /home/sandip/v1/config/development.sphinx.conf
Sphinx 0.9.8-rc2 (r1234)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file '/home/sandip/v1/config/development.sphinx.conf'...
indexing index 'call_core'...
collected 113521 docs, 2.1 MB
collected 0 attr values
sorted 0.1 Mvalues, 100.0% done
sorted 0.3 Mhits, 100.0% done
total 113521 docs, 2114790 bytes
total 6.102 sec, 346589.68 bytes/sec, 18604.78 docs/sec
distributed index 'call' can not be directly indexed; skipping.
Generating Configuration to /home/sandip/v1/config/development.sphinx.conf
Sphinx 0.9.8-rc2 (r1234)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file '/home/sandip/v1/config/development.sphinx.conf'...
indexing index 'call_core'...
collected 113521 docs, 2.1 MB
collected 0 attr values
sorted 0.1 Mvalues, 100.0% done
sorted 0.3 Mhits, 100.0% done
total 113521 docs, 2114790 bytes
total 3.628 sec, 582909.70 bytes/sec, 31290.34 docs/sec
distributed index 'call' can not be directly indexed; skipping.
It returns array of records found. conditions are allowed by sphinx. If you need to add conditions on integer attributes then index block in model needs to have has method like author_id in following. <div class='code-highlight'><pre class='code-highlight-pre'><div data-line='1' class='code-highlight-row numbered'><div class='code-highlight-line'>define_indexdo
</div></div><div data-line='2' class='code-highlight-row numbered'><div class='code-highlight-line'> indexescontent
</div></div><div data-line='3' class='code-highlight-row numbered'><div class='code-highlight-line'> indexes:name,:sortable=>true
</div></div><div data-line='4' class='code-highlight-row numbered'><div class='code-highlight-line'> indexescomments.content,:as=>:comment_content
</div></div><div data-line='5' class='code-highlight-row numbered'><div class='code-highlight-line'> indexes[author.first_name,author.last_name],:as=>:author_name
</div></div><div data-line='6' class='code-highlight-row numbered'><div class='code-highlight-line'> </div></div><div data-line='7' class='code-highlight-row numbered'><div class='code-highlight-line'> hasauthor_id,created_at
</div></div><div data-line='8' class='code-highlight-row numbered'><div class='code-highlight-line'>end</div></div></pre></div>