Skip to content

Instantly share code, notes, and snippets.

@jraines
Created February 22, 2009 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jraines/68567 to your computer and use it in GitHub Desktop.
Save jraines/68567 to your computer and use it in GitHub Desktop.
Basic ferret example
require 'rubygems'
require 'ferret'
# Create the full-text search index
jobs = Job.find(:all, :conditions => ["displayed = 'yes'"])
index = Index::Index.new(:path => 'search_index', :create => true)
jobs.each do |job|
index << {:id => job.id, :description => job.description}
end
##########################
# In the Sinatra app file
##########################
post '/search' do
query = params['query']
index = Index::Index.new(:path => 'search_index')
result_ids = []
index.search_each("description:'#{query}'") do |id, score|
result_ids << index[id][:id]
end
@jobs = Job.find(:all, :conditions => {:id => result_ids})
haml :index
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment