English 中文(简体)
Adding Search to Ruby on Rails - Easy Question
原标题:

I am trying to figure out how to add search to my rails application. I am brand new so go slow. I have created a blog and done quite a bit of customizing including adding some AJAX, pretty proud of myself so far. I am having trouble finding any good tutorials about how to add this functionality. Basically I just want to enable a full search to search my posts table. What is the easiest way to do this?

最佳回答

There s a great Railscast on Thinking Sphinx, which is my favorite above the other mentioned options. It s fast, simple, and is continually being developed.

There s also SearchLogic which is great if you don t really need full text indexing (you probably don t for a blog). And a Railscast to go along with that as well.

问题回答

Depending on the RDBMS you are using there might be a built-in solution for fulltext search.

Otherwise you might check out Sunspot (and the Rails plugin) which uses Apache Solr for fulltext search and is easy to use. Especially writing queries/searches is much more fun than with the standard acts_as_solr plugin.

Edit Oh, and here is a screencast on Sunspot for the visual people.

I d suggest using the acts_as_solr plugin. I m just starting with Rails too and that s the search indexing plugin recommended by my professor. It includes the SOLR search engine in the plugin. The site includes installation and usage instructions.

Basically, as the usage shows, you would just include the acts_as_solr tag in whichever models you want to be searchable, and then specify which attributes in your model you want to be indexed for searching on... so you d do something like:

class Post < ActiveRecord::Base
    acts_as_solr :fields => [:post, :comments, :whatever]

And for searching you d do something like...

    Post.find_by_solr(query_string)

The easiest way to do this would be to do something like this:

results = Post.find(:all, :conditions => "post_body LIKE %#{search_string}% " )

However, this is pretty limited in that it will only search for the exact word or phrase you are looking for (not to mention a vulnerability to SQL injection). As I mentioned, this is the "easiest" way to do a search, but definitely not the best. I would look into using acts_as_solr if you want to do this seriously.





相关问题
Acronyms with Sphinx search engine

how can i index acronyms like m.i.a. ? when i search for mia , i get results for mia and not m.i.a. . when i search for m.i.a. , i get nothing at all. edit: solution looks roughly like: ...

Querying multiple index in django-sphinx

The django-sphinx documentation shows that django-sphinx layer also supports some basic querying over multiple indexes. http://github.com/dcramer/django-sphinx/blob/master/README.rst from ...

Adding Search to Ruby on Rails - Easy Question

I am trying to figure out how to add search to my rails application. I am brand new so go slow. I have created a blog and done quite a bit of customizing including adding some AJAX, pretty proud of ...

Searching and ranking short phrases (e.g. movie titles)

I m trying to improve our search capabilities for short phrases (in our case movie titles) and am currently looking at SQL Server 2008 Full Text Search, which provides some of the functionality we ...

Will Full text search consider indexes?

Ok I have a full text search index created on my JobsToDo table, but what I m concerned about is if this is rendering my other indexes on the table useless. I have a normal nonclustered index on the ...

Lucene.NET on shared hosting

I m trying to get Lucene.NET to work on a shared hosting environment. Mascix over on codeproject outlines here how he got this to work on godaddy. I m attempting this on isqsolutions. Both ...

Hibernate Search or Compass

I can t seem to find any recent talk on the choice. Back in 06 there was criticism on Hibernate Search as being incomplete and not being ready to compete with Compass, is it now? Has anyone used both ...

热门标签