What's the best option for searching in Ruby on Rails?
Thinking Sphinx has more concise syntax to define which fields and which models are indexed.
Both UltraSphinx and Thinking Sphinx (recently) have ultra-cool feature which takes into account geographical proximity of objects.
UltraSphinx has annoying problems with how it loads models (it does not load entire Rails stack, so you could get strange and hard to diagnose errors, which are handled by adding explicit require
statements).
We use Thinking Sphinx on new projects, and UltraSphinx on projects which use geo content.
How do I do full-text searching in Ruby on Rails?
There are several options available and each have different strengths and weaknesses. If you would like to add full-text searching, it would be prudent to investigate each a little bit and try them out to see how well it works for you in your environment.
MySQL has built-in support for full-text searching. It has online support meaning that when new records are added to the database, they are automatically indexed and will be available in the search results. The documentation has more details.
acts_as_tsearch
offers a wrapper for similar built-in functionality for recent versions of PostgreSQL
For other databases you will have to use other software.
Lucene is a popular search provider written in Java. You can use Lucene through its search server Solr with Rails using acts_as_solr
.
If you don't want to use Java, there is a port of Lucene to Ruby called Ferret. Support for Rails is added using the acts_as_ferret
plugin.
Xapian is another good option and is supported in Rails using the acts_as_xapian
plugin.
Finally, my preferred choice is Sphinx using the Ultrasphinx plugin. It is extremely fast and has many options on how to index and search your databases, but is no longer being actively maintained.
Another plugin for Sphinx is Thinking Sphinx which has a lot of positive feedback. It is a little easier to get started using Thinking Sphinx than Ultrasphinx. I would suggest investigating both plugins to determine which fits better with your project.
Searching in Ruby on Rails - How do I search on each word entered and not the exact string?
If I read your problem correctly, you want to return a row if the tag names for the row matches one of the words passed in the query string.
You can rewrite your search
method as follows:
def self.search(search)
all :conditions => (search ? { :tag_name => search.split} : [])
end
If you need partial matching then do the following:
def self.search(str)
return [] if str.blank?
cond_text = str.split.map{|w| "tag_name LIKE ? "}.join(" OR ")
cond_values = str.split.map{|w| "%#{w}%"}
all(:conditions => (str ? [cond_text, *cond_values] : []))
end
Edit 1
If you want pass multiple search strings then:
def self.search(*args)
return [] if args.blank?
cond_text, cond_values = [], []
args.each do |str|
next if str.blank?
cond_text << "( %s )" % str.split.map{|w| "tag_name LIKE ? "}.join(" OR ")
cond_values.concat(str.split.map{|w| "%#{w}%"})
end
all :conditions => [cond_text.join(" AND "), *cond_values]
end
Now you can make calls such as:
Tag.search("Ruby On Rails")
Tag.search("Ruby On Rails", "Houston")
Tag.search("Ruby On Rails", "Houston", "TX")
Tag.search("Ruby On Rails", "Houston", "TX", "Blah")
Tag.search("Ruby On Rails", "Houston", "TX", "Blah", ....) # n parameters
Caveat:
The wild card LIKE
searches are not very efficient(as they don't use the index). You should consider using Sphinx (via ThinkingSphinx) OR Solr(via SunSpot) if you have lot of data.
Best way to add full web search to my site?
Depends what you mean by full web search really. If you want to search the whole web then the answers above wont help you much as they are really for indexing and searching the content of your site. I would suggest using the Google ajax search (just a 'powered by google' needed, no ads) or Boss from yahoo (might require ads not sure).
http://code.google.com/apis/ajaxsearch/
http://developer.yahoo.com/search/boss/
The best way to search according to distance in ruby on rails
have you checked rubygeocoder gem ,
read this about the distance queries
Following is a great screencast by ryan
Related Topics
Count Number of Days Between Two Dates
How to Get Rspec-2 to Give the Full Trace Associated with a Test Failure
Ruby - Replace the First Occurrence of a Substring with Another String
Ruby - Determine If a Number Is a Prime
How to Catch Errno::Econnreset Class in "Case When"
Getting Typed Results from Activerecord Raw SQL
Printing Qr Codes Through an Esc/Pos Thermal Printer
Could Not Find 'Cocoapods' (>= 0) Among N Total Gem(S) (Gem::Loaderror)
Rspec: How to Test If a Method Was Called
How to Modify an Array While I am Iterating Over It in Ruby
Rails 6.1, Ruby 3.0.0: Tests Error as They Cannot Load Rexml
Eager Loading: the Right Way to Do Things
Ruby/Sinatra - Serving Up CSS, JavaScript, or Image Files
Rails3 Devise Undefined Method 'Confirmation_Url'