read

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.

Plugin installation

ruby script/plugin install git://github.com/freelancing-god/thinking-sphinx.git

OR

## Gem installation

To install gem run the following command.

<div class='code-highlight'><pre class='code-highlight-pre'><div data-line='1' class='code-highlight-row numbered'><div class='code-highlight-line'>gem install thinking-sphinx –source http://rubygems.org/ </div></div><div data-line='2' class='code-highlight-row numbered'><div class='code-highlight-line'> </div></div><div data-line='3' class='code-highlight-row numbered'><div class='code-highlight-line'>If you’re upgrading, you should read this: </div></div><div data-line='4' class='code-highlight-row numbered'><div class='code-highlight-line'>http://freelancing-god.github.com/ts/en/upgrading.html </div></div><div data-line='5' class='code-highlight-row numbered'><div class='code-highlight-line'> </div></div><div data-line='6' class='code-highlight-row numbered'><div class='code-highlight-line'>Successfully installed thinking-sphinx-1.3.16 </div></div><div data-line='7' class='code-highlight-row numbered'><div class='code-highlight-line'>1 gem installed</div></div></pre></div>

Edit environment file and add following lines to it inside initializer block.

<div class='code-highlight'><pre class='code-highlight-pre'><div data-line='1' class='code-highlight-row numbered'><div class='code-highlight-line'>config.gem( </div></div><div data-line='2' class='code-highlight-row numbered'><div class='code-highlight-line'> ‘thinking-sphinx’, </div></div><div data-line='3' class='code-highlight-row numbered'><div class='code-highlight-line'> :lib => ‘thinking_sphinx’, </div></div><div data-line='4' class='code-highlight-row numbered'><div class='code-highlight-line'> :version => ‘1.3.16’ </div></div><div data-line='5' class='code-highlight-row numbered'><div class='code-highlight-line'> )</div></div></pre></div>

Edit Rakefile and add following lines to it.

require 'thinking_sphinx/tasks'

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_index do
# following fields are database fields
# we can not add model methods in sphinx index
# sphinx fields allows ONLY model based associations
# fields
indexes customer_name
indexes phone
indexes mobile
indexes other_phone
indexes car_make
indexes car_model
indexes registration_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.

Search using sphinx

ruby script console
>>Call.search 'sandip'
=> [#<call 00:00:00"="" 09:22:35",="" 1018,="" 18:10:00",="" 2009-02-10="" 2009-03-27="" 2009-04-07="" 2009-11-18="" 20:29:06",="" 6,="" created_at:="" id:="" insurance_expiry_date:="" is="" is_existing_customer:="" nil,="" note:="" phone="" ringing",="" scheduled_at:="" state_id:="" station_shift_customer_car_insurance_product_id:="" updated_at:="">, #<call 00:00:00"="" 11:13:33",="" 18:30:00",="" 2009-02-10="" 2009-02-16="" 2009-04-03="" 2009-11-18="" 20:34:52",="" 6,="" 6024,="" call="" created",="" created_at:="" customer="" id:="" insurance_expiry_date:="" is_existing_customer:="" new="" nil,="" note:="" scheduled_at:="" state_id:="" station_shift_customer_car_insurance_product_id:="" updated_at:="">] </call></call>

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_index do </div></div><div data-line='2' class='code-highlight-row numbered'><div class='code-highlight-line'> indexes content </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'> indexes comments.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'> has author_id, created_at </div></div><div data-line='8' class='code-highlight-row numbered'><div class='code-highlight-line'>end</div></div></pre></div>

Blog Logo

Sandip Ransing


Published

Image

Fun On Rails

Journal of a Web Developer #ruby #rails #JS

Back to Overview