开发者

Rails: Searching by Category without FullText

Lets say I do not want to get the VPS or Dedicated Server required to run constant indexing like with Thinking_Sphinx plugin or many of the other full text search plugins for ruby on rails.

I have a bunch of listings in the database with a Name field and category field (among other things).

It it possible search for a name in a certain category without having a fulltext se开发者_JS百科arch plugin that requires constant indexing? and how would I go about doing this?

Thank you. I hope I have been clear enough.


If you just want to search a specific column (or two), you could certainly take advantage of the LIKE SQL operator.

For example, using a named_scope in rails

class MyModel < ActiveRecord::Base
  named_scope :search, lambda { |query|
    { :conditions => ["category like ?", "%#{query}%"] }
  }
end

Then, in your controllers, you could do something like

@search_results = MyModel.search(params[:query])

There are a few drawbacks to this approach:

  1. You're only searching a single column. You can take advantage of the dynamic nature of Ruby to expand this to more columns, but you're definitely not getting the advantage of an index like you would with sphinx
  2. This will work fine for smallish sets of data. After you get a few thousand rows, you're going to start needing some decent indexing strategies to keep this fast, otherwise searching this way is going to slow down dramatically.

Hopefully that helps. If I misunderstood the question, let me know.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜