Tutorial :Search in Ruby On Rails


I have a small site with about 500 photos and 150 visitors per day which is hosted on Dreamhost. I would like to add a simple search engine that does not need to run long time processes which are not allowed on shared host.

The search engine should process different fields belonging to various models: Photo, Photo.author.name, Photo.comments.content and many others

Is there any plugin that can help?


The full-text search feature of MySQL, which is generally available on any shared hosting environment, is a great way to add this kind of functionality. The only downside is that it works only on MyISAM tables which have generally been deprecated in favor of InnoDB.

The approach that I have seen work, where a great example is the Wikipedia database architecture, is to create derivative copies of model records specifically for searching purposes. These need to be kept in sync with the main record, but that's easily done with an after_save handler or a simple SQL update statement.

One note is that ActiveRecord is not capable of understanding full-text indexes. A rather ugly extension is required to make it cooperate, though I do have an example bundled up in a collection of MySQL hacks:



There are at least 3 full-text search engines for Rails:

Which one is the best choice? It depends. Here you can read a comparison between sphinx and ferret, including a comment from Engine Yard.


I think that the acts_as_indexed plugin is the best bet for your needs. It doesn't require a separate server; it creates its index automatically in the filesystem. On a site similar to yours, it was plenty fast and easy to configure.


acts_as_ferret will most likely serve you well.

But I don't know if and how Ferret will perform on a setup like yours.


Ferret or sphinx WILL need dedicated background process. Your only option seems like mysql full text search with proper indexes.


You might want to look into Sphinx/Thinking Sphinx for fast full-text search.

Thinking Sphinx

I don't know if installing Unix packages and that whole process is overkill for what you're looking for on Dreamhost, but it's a very fast and robust solution that will serve your needs well in the future.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »