Really simple extension to add full text search functionality to ActiveModel-style classes
Currently supports ActiveRecord and Mongoid.
Load the library (add the gem to your Gemfile) and add one line to the classes you would like to search like this:
class Person < ActiveRecord::Base acts_as_full_text_searchable end class Page include Mongoid::Document acts_as_full_text_searchable end
You can then query the classes like you would do with where():
Person.full_text_search("Pete") # equals Person.where(:first_name => "Pete") OR Person.where(:last_name => "Pete") OR Person.where(:pet_name => "Pete")
AND is also supported:
Person.full_text_search("Pete", :method => :and)
You can use anything that goes normally goes into where() conditions, for example:
# on Mongoid: Person.full_text_search(/any regex/)
Finally, there is also a global search, that operates on all classes that act as full_text_searchable:
FullTextSearch.global_search("Pete")
-
full_text_search only operates on the attributes defined on the class itself, not on any relations.
-
global search only works on classes that have been loaded. If unsure, add them manually to FullTextSearch.classes
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-
Send me a pull request. Bonus points for topic branches.
Copyright © 2010 Michel Jansen. See LICENSE for details.