Ruby Ferret Simple Indexing And Search Example Part 2


/ Published in: Ruby
Save to your folder(s)

This is an example of how to search within a Ferret index (based upon http://kasparov.skife.org/blog/src/ruby/ferret.html).


Copy this code and paste it in your HTML
  1. require 'rubygems'
  2. require 'ferret'
  3. require 'find'
  4.  
  5. wot = ARGV[0]
  6. if wot.nil?
  7. puts "use: search.rb <query>"
  8. exit
  9. end
  10.  
  11. index = Ferret::Index::Index.new(:default_field => 'content', :path => '/tmp/index_folder')
  12. ini = Time.now
  13. puts "Searching.."
  14. docs=0
  15.  
  16. index.search_each(wot, options={:limit=>:all}) do |doc, score|
  17.  
  18. res= <<STRING_END
  19.  -------------------------------------------------------
  20.  #{File.basename(index[doc]['file'])} :
  21.  #{index.highlight(wot, doc,:field => :content,:pre_tag => "->>",:post_tag => "<<-")}
  22. STRING_END
  23. puts res
  24. docs+=1
  25. end
  26.  
  27. elapsed = Time.now - ini
  28. puts "Elapsed time: #{elapsed} secs\n"
  29. puts "Documents found: #{docs}"

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.