We Recommend

Beginning Ruby: From Novice to Professional Beginning Ruby: From Novice to Professional
Beginning Ruby is a thoroughly contemporary guide for every type of reader wanting to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a genuine Ruby application.


Posted By

narkisr on 04/27/08


Tagged

search ruby ferret indexing


Versions (?)


Ruby Ferret Simple Indexing And Search Example Part 2


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).


  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 

You need to login to post a comment.