开发者

Implementing search in a Ruby on Rails 3 application?

I am writing my first Ruby on Rails application and need to implement a "search" feature. It will need to search the database (1 column per table in 3 different tables), and return the most relevant results in each of the 3 categories. Kind of like how you can do a search on Amazon.com that will return results from all the different开发者_C百科 departments.

Is there a gem/library/common technique in the Ruby on Rails world that I should know about (that works with Rails 3)? Otherwise, what should I do in order to implement a search feature in my application?


I think it depends on how "serious" you're about your search.

If you just want to search in some simple VARCHAR fields, which you'd manually do by generating some "LIKE '%xyz%'" statements, than all you need is a plugin that does that for you. My favourite here is searchlogic. It enables quite a few handy dynamic scopes in your models, which you can chain together (like other scopes). For example you can write something like this:

User.last_name_like("Doe").age_gt(30).age_lt(40)

Here's a great screencast on how to use its other features.

But SQL LIKE statement isn't really a good fit for searching in large chunks of text. So if that's what you need, you'd be better off using a plugin that actually indexes your data.

In that case thinking sphinx (mentioned in other answers) is a great solution. Just keep in mind that it requires some platform specific installation steps and only works with PostreSQL and MySQL (it doesn't work with Rails' default SQLite). Using it is also not that straightforward - you need to define what to index on every model you want to search, build the index, start Sphinx, etc.


Probably you would need to use some search engine. Take a look at thinking sphinx plugin. I also used acts_as_ferret but it can cause some problems.

I don't know if there is a plugin that do all what you want for you. I would do it this way:

  1. Implement searching (with sql or with some search engine like Sphinx etc.)
  2. Then add some ajax stuff for autocomplete.

Google is your friend: take a look here and here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜