Kevin Marsh

Dead Simple Full-text Searching with Rails

Using MySQL’s built-in FULLTEXT searching capabilities, you can add full-text searching to your Rails app’s pretty easily. It’s not quite as sexy as acts_as_searchable but implementation is pretty trivial and no dependencies are required. One caveat: your tables need to be MyISAM.

First, make sure you add a FULLTEXT index to your MySQL table like so:

ALTER TABLE articles ADD FULLTEXT (title, content);

Then, just use ActiveRecord’s find() method, but pass in a :conditions parameter:

@articles = Article.find(:all, :conditions => ['MATCH(title, content) AGAINST (?)', params[:query]])

Others' Comments

  1. since1968 said on July 20, 2006 @ 07:32 AM:
    Kevin, thanks for posting this. I assume you're generating/updating your tables via DDL and not with migrations? Do you know of any way to support FULLTEXT within a migration?

Your Comments

Your name:

Your email:

Your blog:

Your comment: