Created
February 22, 2009 18:53
-
-
Save jraines/68567 to your computer and use it in GitHub Desktop.
Basic ferret example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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