December 14th, 2006
Saw this in a MacDonald’s at a local shopping mall:
If you can look past the fuzziness of the photo, it’s actually someone using a HP laptop with an Apple sticker. Now, if only I can get a HP sticker for my MacBook Pro…
December 4th, 2006
Yup, it’s that fast and simple, using Ferret, a Ruby port of Lucene (the de facto full text search engine written in Java) and the acts_as_ferret plugin, I added “Did you mean this?” functionality to a small Rails application (non-perma link) today rather quickly and painlessly.
Adding full text search funtionality to your Rails applications
Install Ferret via RubyGems.
sudo gem install ferret
Install the acts_as_ferret plugin for your Rails application.
ruby script/plugin install <ACTS_AS_FERRET_SVN_REPOSITORY>
Add the acts_as_ferret mixin method to your ActiveRecord model, specifying the fields you want indexed.
class Location < ActiveRecord::Base
belongs_to :country, :foreign_key => ‘country_code’
belongs_to :state
acts_as_ferret :fields => [:location_name, :country_name, :state_name]
def country_name
country.country_name
end
If you have any associations that you want searched as well, one way (there is more than one way to do this) would be like what I did above with the country_name attribute of Country. For a more detailed discussion on Ferret and ActiveRecord associations, I found this thread in the Ruby forum enlightening.
In your controller, use Model.find_by_contents() to perform the full text search with your query. If you need fuzzy matching, append a tilde (”~”) to the end of your query - if you’re here, you probably needed fuzzy matching anyway as you’re most likely creating “Did you mean this?”-type functionality.
# In the controller...
@locations = Location.find_by_contents("#{@search_request.location_entered}~") # The "~" is for fuzzy matching.
All that’s left is to flash the appropriate message in your view and display your model objects as you normally would.
Some gotchas:
- If you change the way your model is indexed, remember to clear out the existing one. acts_as_ferret stores the index by default in a ‘index/’ directory in the root of your Rails application. Stop your mongrel/Webrick process to remove the lock on the index files and delete the whole directory - Ferret will rebuild it the next time you perform a full text search.
- The first time you do a full text search, Ferret builds your index for you and this takes quite a while - don’t be alarmed if your Rails application appears for freeze or returns a 500 internal server error. You can observe the contents of the ‘index/’ directory in the root of your Rails application to tell when Ferret has finished indexing (files stop appearing and disappearing). I found it easier to run a ‘find_by_contents’ call on my models via ’script/console’ and waiting for some results to appear.
- Append a “~” to your query string for fuzzy matching! This allows you to retrieve results matching phonetically or are mispelled. There are more complex matching rules - check out the Lucene documentation.
How to setup your Trash/Sent/Drafts folders for IMAP on Mail.app. “Select mailbox, Mailbox -> Use this mailbox for…, Sent”. (OK I misspelled ‘trash’ in the post title - see trackback below.) (1)
December 4th, 2006
All I got for my birthday was this t-shirt:
In case the image doesn’t load at some point or is too small, here’s what it says:
Two hours of broadband to ftp the Linux Package, 15 cents.
CDs to burn the files, $1.00.
The knowledge that nothing on your computer is from Microsoft, PRICELESS.
There are some operating systems that you don’t need money to buy.
For everyone else, there’s Windows.
Ironically, I’m now a Mac OS X snob, but the t-shirt still kicks arse. And no, I’m not one of those Microsoft haters without a cause - it’s just a t-shirt.
(Actually I got more than the t-shirt, but I wanted to start the entry with “All I got was this t-shirt”.)