Ruby Ferret simple indexing and search example part 1


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

This is a simple example for building a search index with the Ferret library (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. include Ferret
  5.  
  6. index = Index::Index.new(:default_field => 'content', :path => '/tmp/index_folder')
  7. ini = Time.now
  8. numFiles=0
  9. IndexedExts=['.java','.properties']
  10. Find.find('/code/to/index') do |path|
  11.  
  12. if(IndexedExts.find {|ext| path.include?(ext)}==nil)
  13. next
  14. end
  15. puts "Indexing: #{path}"
  16. numFiles=numFiles+1
  17.  
  18. if FileTest.file? path
  19. File.open(path) do |file|
  20. index.add_document(:file => path, :content => file.readlines)
  21. end
  22. end
  23.  
  24. end
  25.  
  26. elapsed = Time.now - ini
  27. puts "Files: #{numFiles}"
  28. puts "Elapsed time: #{elapsed} secs\n"

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.