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 1


Published in: Ruby 


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


  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 

You need to login to post a comment.