views:

241

answers:

3

Hi, I'm trying to add search capability to one of my rails applications. I just need it to search through the mysql columns of quite a few models well. I'm wondering what the best way to do this is. I figure I should use a plugin but I don't see anyone really recommending one over another. Is there one you would recommend over all others? How is acts_as_ferret? Thanks.

+1  A: 

I've never used acts_as_ferret. Here's something you could do. Create a table like so:

create_table(:indexed_models) do |t|
  t.column :name, :string
  t.column :model_id, :id
  t.column :index, :string
end

For each model you want to search on, concatenate the values from the searchable columns to create an "index". Insert the results in to the indexed_models table. Where the name is the model's name, model_id is it's primary key, and index is the searchable content. Use observers to create and update the indexed_models table when the models you've designated as "searchable" change.

Do a text search of the index field of indexed_models, then generate a container of models based on the return model name & primary key.

I've never used this approach either, but I've been meaning too.

Terry Lorber
Just a thought: If you're going to go down that route, you could use a polymorphic relationship between the indexed model and its original model - ActiveRecord would then give you a method of getting from the indexed model to the original without you needing to roll your own.
NeilS
@NeilS Good point, use STI (Single Table Inheritance) for all these searchable models. That way, the "name" column of create_table is not necessary. If you've already got models, or can't make STI work for these models, then it's a bit more work.
Terry Lorber
+1  A: 

Searchlogic might be worth checking out. Mr ryanb just did an awesome railscast on it. If you're looking for a fulltext solution, he also has a screencast on Thinking Sphinx.

Andy Gaskell
I use Sphinx for this. It's pretty clean to implement - I've got 20 lines of Ruby code that uses the Riddle client (from the thinking_sphinx guy). There is one catch - Sphinx needs a unique document ID for each thing that you index. I use sql_query_pre, sql_query_post and a @variable to make sure that each document gets a unique ID (that I ignore completely).
casey
Searchlogic is great, but I don't believe is offers any cross-model search capabilities. (Correct me if I'm wrong!)Sphinx and ThinkingSphinx certainly do, though, and very effectively in my opinion.
NeilS
+1  A: 

I highly recommend Sphinx as a search daemon and indexer. It's really good, customizable and scalable.

As a client and front-end library use UltraSphinx or ThinkingSphinx . The second has a better support recently.

Stanislav