Making acts_as_ferret deal nicely with pluralisation

I recently discovered how to nicely deal with pluralisation in the Rails plugin ‘acts_as_ferret‘ (which we use in our site). When i search for ‘trumpet’, i want to find resources with ‘trumpets’, and vice versa. By default acts_as_ferret doesn’t do this, but it’s easy to add. I found the answer in this post on the acts_as_ferret mailing list/forum:

Anyway, here’s how i used it.  I’m going to assume that anyone reading this has already installed the ferret gem and acts_as_ferret plugin.

Make a new file in lib, called ‘acts_as_ferret_extensions.rb’


module Ferret::Analysis
  class StemmingAnalyzer
    def token_stream(field, text)

Now require this file in the place in your config where you require stuff.  In the default case this would be in config/environment.rb:

#in config/environment.rb
require 'acts_as_ferret_extensions'

Next, change your calls to acts_as_ferret to specify an analyzer: here’s one of mine as an example, from the ‘Asset’ class in this case.

  #in asset.rb (an example from my app)
  acts_as_ferret( :ferret => {:analyzer =>},
                  :fields => { :name => {},
                               :description => {}
                              } )

Now, delete the index and rebuild it – deleting it might not be necessary but i’ve found weird results rebuilding after changing the options to acts_as_ferret: deleting the index is definitely safer.

#at the command line
rm -r index/development/asset/*
#in the console

Now, when you do a search, specify this analyzer again:

#in the console
assets = Asset.find_with_ferret("trumpet", :page => 1,
                                           :per_page => 20,
                                           :analyzer =>

And that should be it!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s