Return to Snippet

Revision: 6091
at April 27, 2008 07:22 by narkisr


Updated Code
require 'rubygems'
require 'ferret'
require 'find'
include Ferret

index = Index::Index.new(:default_field => 'content', :path => '/tmp/index_folder')
ini = Time.now
numFiles=0
IndexedExts=['.java','.properties']
Find.find('/code/to/index') do |path|

 if(IndexedExts.find {|ext| path.include?(ext)}==nil)
   next  
 end 
 puts "Indexing: #{path}"
 numFiles=numFiles+1

 if FileTest.file? path
 	File.open(path) do |file|
 		index.add_document(:file => path, :content => file.readlines)
 	end
 end

end

elapsed = Time.now - ini
puts "Files: #{numFiles}"
puts "Elapsed time: #{elapsed} secs\n"

Revision: 6090
at April 27, 2008 07:20 by narkisr


Updated Code
# This is the search.rb
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 => 'C:\\ferret\\index')
ini = Time.now
puts "Searching.."
docs=0

index.search_each(wot, options={:limit=>:all}) do |doc, score|
 
 res= <<STRING_END
 -------------------------------------------------------
 #{File.new(index[doc]['file']).base_name} :
 #{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}"

Revision: 6089
at April 27, 2008 07:19 by narkisr


Initial Code
# This is the search.rb
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 => 'C:\\ferret\\index')
ini = Time.now
puts "Searching.."
docs=0

index.search_each(wot, options={:limit=>:all}) do |doc, score|
 
 res= <<STRING_END
 -------------------------------------------------------
 #{File.new(index[doc]['file']).base_name} :
 #{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}"

# This is the index.rb

require 'rubygems'
require 'ferret'
require 'find'
include Ferret

index = Index::Index.new(:default_field => 'content', :path => 'C:\\ferret\\index')
ini = Time.now
numFiles=0
IndexedExts=['.as','.properties']
Find.find('D:\\dev\\ccmTrunk\\Starteam\\ws\\assets\\client\\') do |path|

 if(IndexedExts.find {|ext| path.include?(ext)}==nil)
   next  
 end 
 puts "Indexing: #{path}"
 numFiles=numFiles+1

 if FileTest.file? path
 	File.open(path) do |file|
 		index.add_document(:file => path, :content => file.readlines)
 	end
 end

end

elapsed = Time.now - ini
puts "Files: #{numFiles}"
puts "Elapsed time: #{elapsed} secs\n"

Initial URL

                                

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

Initial Title
Ruby Ferret simple indexing and search example part 1

Initial Tags
search, ruby

Initial Language
Ruby