First things first: let’s get Ferret installed. Thanks to RubyGems, this is pretty easy. If you haven’t used
RubyGems before, there is a great introduction at the RubyGems web site
(http://docs.rubygems.org/). If you
are on Windows and you used the Ruby One-Click Installer to install
Ruby, you’ll have everything you need. Other systems, such as Linux or Mac, need to have make
and a C compiler such as gcc
to build the
extension. Other than that, Ferret comes dependency-free. You simply need to run the
gem install
script:
dave$ sudo gem install ferret
Once this process successfully completes, you will have Ferret
installed on your system. The easiest way to check that everything is
working correctly is to open an irb
session, as shown
in Example 1-1.
Example 1-1. irb session
dave
$
irb
-
rubygems
>>
require
'
ferret
'
#=> true
>>
index
=
Ferret
::
I
.
new
#=> #<Ferret::Index:...>
>>
index
<<
"
Time heals all wounds
"
#=> #<Ferret::Index:...>
>>
index
<<
"
A rolling stone gathers no moss
"
#=> #<Ferret::Index:...>
>>
index
<<
"
A stitch in time saves nine
"
#=> #<Ferret::Index:...>
>>
index
<<
"
Look before you leap
"
#=> #<Ferret::Index:...>
>>
index
<<
"
Time and tide wait for no man
"
#=> #<Ferret::Index:...>
>>
index
<<
"
Time wounds all heels
"
#=> #<Ferret::Index:...>
>>
puts
index
.
search
("
time
")
All we’ve done here is load RubyGems and Ferret, create a new
in-memory index, add a few strings to it, and run a search, printing out the results. If everything is working
correctly, you will see the results of your search printed out in order of
relevance. It doesn’t get much simpler than that. irb
is a great way to play around with Ferret
and try out new things. Next, we’ll show you how to index all the text
files under a particular directory.
Get Ferret now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.