Following my scrap and crawling experiences, I was looking for a good indexer. Initially I was setup to use Lucene, as I got pretty good recomendations about. Lucene really shines, but I was decided about using Ruby or any other scripting language to avoid bloated code.

Browsing around I found about Ferret, which is a text indexing library for Ruby. The benchmarks and references were good, and so I setup to work on some testing to get used to it. Fortunately, the results were good, and the API is a breeze. Also pagination is built-in. How cool is that ?

For an initial test, I setup to index the Linux Kernel source code. By looking at Brian McCallister example, I wrote two small scripts: indexer.rb and search.rb. I ran indexer over the source tree, and came up with some very interesting results. The words I searched for were ‘net’, ‘skb’, ‘x86’ and finally ‘linux’.

Source :
System path: /usr/src/linux-

$ du -hs linux-
296M linux-

filetypes: mostly text, .c, .h

$ ruby indexer.rb
Files: 23867
Elapsed time: 307.072062 secs

Index size on disk:
$ du -hs /tmp/ferret-test/
469M /tmp/ferret-test/

Three time runs, limited on 10 first relevant results (:offset=0)

$ ruby search.rb net
/usr/src/linux- score: 1.0
/usr/src/linux- score: 0.837733394053738
/usr/src/linux- score: 0.837733394053738
/usr/src/linux- score: 0.728849451255887
/usr/src/linux- score: 0.722764197161946
/usr/src/linux- score: 0.698111124955755
/usr/src/linux- score: 0.676843858509696
/usr/src/linux- score: 0.653023192474492
/usr/src/linux- score: 0.634811095955514
/usr/src/linux- score: 0.634811095955514

(1)Elapsed time: 0.010868 secs
(2)Elapsed time: 0.010751 secs
(3)Elapsed time: 0.011172 secs

$ ruby search.rb skb
/usr/src/linux- score: 1.0
/usr/src/linux- score: 0.859210759788879
/usr/src/linux- score: 0.842525497336466
/usr/src/linux- score: 0.828721377551808
/usr/src/linux- score: 0.825503025430993
/usr/src/linux- score: 0.822541791389715
/usr/src/linux- score: 0.809219293235739
/usr/src/linux- score: 0.799289861992655
/usr/src/linux- score: 0.79427853649446
/usr/src/linux- score: 0.793999290076671

(1)Elapsed time: 0.008609 secs
(2)Elapsed time: 0.008855 secs
(3)Elapsed time: 0.010647 secs

$ ruby search.rb x86
/usr/src/linux- score: 1.0
/usr/src/linux- score: 1.0
/usr/src/linux- score: 0.625
/usr/src/linux- score: 0.559017013920514
/usr/src/linux- score: 0.530330073130523
/usr/src/linux- score: 0.5182226426261
/usr/src/linux- score: 0.5
/usr/src/linux- score: 0.5
/usr/src/linux- score: 0.5
/usr/src/linux- score: 0.5

(1)Elapsed time: 0.011245 secs
(2)Elapsed time: 0.011024 secs
(3)Elapsed time: 0.011324 secs

Full run with all results for:

Elapsed time: 0.094946 secs
Documents found: 488

Elapsed time: 0.254102 secs
Documents found: 1248

Elapsed time: 0.061239 secs
Documents found: 332

Elapsed time: 0.986499 secs
Documents found: 4030

I made 3 runs of each 10-result search. So far the times were consistent. My machine was under some load, as I use it as desktop and didn’t closed any application prior to testing:

$ free
total used free shared buffers cached
Mem: 2074644 1532372 542272 0 25572 1182912
-/+ buffers/cache: 323888 1750756
Swap: 4096552 665880 3430672

Processor is a Intel(R) Pentium(R) D CPU 2.80GHz (according to /proc/cpuinfo).

Ferret is pretty tight and seems that the author is commited to keeping good performance through smart code optimization (from the 0.9 version the core of the library is written in C). Setup is easy as gem install ferret could be and the learning curve is smooth. The hardest part until now is the FQL, Ferret Query Language, which may be used to fine tune queries and results.

———- Code for indexer.rb ———–

#!/usr/bin/env ruby
require 'rubygems'

require 'ferret'

require 'find'

include Ferret

index = => 'content', :path => '/tmp/ferret-test')

ini =


Find.find("/usr/src/linux-") do |path|

 puts "Indexing: #{path}"


 if FileTest.file? path do |file|

 		index.add_document(:file => path, :content => file.readlines)




elapsed = - ini

puts "Files: #{numFiles}"

puts "Elapsed time: #{elapsed} secs\n"

———- Code for indexer.rb ———–

———- Code for search.rb ———–

#!/usr/bin/env ruby
require 'rubygems'

require 'ferret'

require 'find'

wot = ARGV[0]

if wot.nil?

 puts "use: search.rb <query>"



index = => 'content', :path => '/tmp/ferret-test')

ini =

puts "Searching"


# uncomment line below for 10 first results, and comment the subsequent line.

# index.search_each(wot) do |doc, score| 

index.search_each(wot, options={:limit=>:all}) do |doc, score|

 puts index[doc]['file'] + " score: "+score.to_s



elapsed = - ini

puts "Elapsed time: #{elapsed} secs\n"

puts "Documents found: #{docs}"

———- Code for search.rb ———–

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: