/ Published in: Ruby
This is an example of how to search within a Ferret index (based upon http://kasparov.skife.org/blog/src/ruby/ferret.html).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
require 'rubygems' require 'ferret' require 'find' wot = ARGV[0] if wot.nil? puts "use: search.rb <query>" exit end index = Ferret::Index::Index.new(:default_field => 'content', :path => '/tmp/index_folder') ini = Time.now puts "Searching.." docs=0 index.search_each(wot, options={:limit=>:all}) do |doc, score| res= <<STRING_END ------------------------------------------------------- #{File.basename(index[doc]['file'])} : #{index.highlight(wot, doc,:field => :content,:pre_tag => "->>",:post_tag => "<<-")} STRING_END puts res docs+=1 end elapsed = Time.now - ini puts "Elapsed time: #{elapsed} secs\n" puts "Documents found: #{docs}"