public
Description: share my private rails plugins: fuzzy-search, datanmapper-session-store, idle-session-timeouts, etc
Homepage: <a href="http://code.google.com/p/kristians-rails-plugins">code.google.com/p/kristians-rails-plugins</a>
Clone URL: git://github.com/mkristian/kristians_rails_plugins.git
kristians_rails_plugins / act_as_fuzzy_search
name age message
..
file MIT-LICENSE Loading commit data...
file MIT-LICENSE~
file README
file README~
file Rakefile
file init.rb Mon Dec 15 03:33:15 -0800 2008 first commit [mkristian]
file init.rb~
file install.rb
directory lib/ Mon Dec 15 03:40:33 -0800 2008 cleaned up rubbish [mkristian]
directory tasks/ Mon Dec 15 03:40:33 -0800 2008 cleaned up rubbish [mkristian]
directory test/ Mon Dec 15 03:40:33 -0800 2008 cleaned up rubbish [mkristian]
file uninstall.rb
act_as_fuzzy_search/README
ActAsFuzzySearch
================

this plugin is inspired by the following blog entry
http://unirec.blogspot.com/2007/12/live-fuzzy-search-using-n-grams-in.html
it can add a fuzzy search to model. this search needs another table where all the
trigrams are stored but that table is hidden otherwise.

the whole plugins does not work with ActiveRecords, but for Datamapper !! to get it to work
for ActiveRecord only a few code places need to be changed, I marked the in the code #DM.

configuration
=============

add the following in the model class

  include FuzzySearch

  act_as_fuzzy_search :property1, :property1
  
where :property1, :property2 are the properties which are index for the fuzzy search. each property gets split into 
words (separator is the whitespaces)

if you define a methods returning a string

  def self.normalize(word)

before include the FuzzySearch than this method gets used to normlize any word. default is to downcast 
the string (which does only work properly within ascii)

trigrams table
==============

the naming follows the convention

class <model_classname>Trigram

  property :id, Integer, :serial => true

  property :<model_lowercase>_id, Integer, :index => true

  property :token, String, :nullable => false, :length => 3, :index => true

end

example
=======

class UserTrigram

  include DataMapper::Resource

  property :id, Integer, :serial => true

  property :user_id, Integer, :index => true

  property :token, String, :nullable => false, :length => 3, :index => true

end

the model class looks like this

class User

  include DataMapper::Resource

  def self.normalize(word)
     #TODO something
     word.downcase
  end

  include FuzzySearch

  act_as_fuzzy_search :firstname, :surname
  
  property :id, Integer, :serial => true

  property :surname, String, :nullable => false , :format => /^[^<'&">]*$/, :length => 32
  property :firstname, String, :nullable => false , :format => /^[^<'&">]*$/, :length => 32

end

now you can create users and search them in a fuzzy manner like

  User.fuzzy_find("Heywy")

enjoy !!

Copyright (c) 2008 kristian, released under the MIT license