Thanks Dave, I think I understand now …
FWIW, the following script works now I have read your responses. I’ve
posted it here for others to read.
==================
require ‘rubygems’
require ‘ferret’
p Ferret::VERSION
@dir = Ferret::Store::RAMDirectory.new
@writer = Ferret::Index::IndexWriter.new(:dir => @dir)
@searcher = Ferret::Search::Searcher.new(@dir)
@parser = Ferret::QueryParser.new
@docs = []
@docs << {:id => 1, :name => ‘Fred’, :occupation => ‘Toon’}
@docs << {:id => 2, :name => ‘Barney’, :occupation => ‘Toon’}
@docs << {:id => 3, :name => ‘Wilma’, :occupation => ‘Toon’}
@docs << {:id => 4, :name => ‘Betty’, :occupation => ‘Toon’}
@docs << {:id => 5, :name => ‘Pebbles’, :occupation => ‘Toon’}
@docs << {:id => 6, :name => ‘Superman’, :occupation => ‘Hero’}
@docs << {:id => 7, :name => ‘Batman’, :occupation => ‘Hero’}
@docs << {:id => 8, :name => ‘Spiderman’, :occupation => ‘Hero’}
@docs << {:id => 9, :name => ‘Green Lantern’, :occupation => ‘Hero’}
@docs << {:id => 10, :name => ‘Dr Strange’, :occupation => ‘Hero’}
@docs << {:id => 11, :name => ‘Phantom’, :occupation => ‘Hero’}
#populate index over time
t1 = Thread.new do
@docs.each do |doc|
p “t1: adding #{doc[:id]} to index”
@writer << doc
sleep(10)
end
end
#search for heroes over time
t2 = Thread.new do
query_txt = ‘occupation:hero’
query = @parser.parse(query_txt)
while true do
hits = @searcher.search(query)
p “t2: searching for #{query_txt} found #{hits.total_hits}”
return if hits.total_hits == 6
sleep(5)
end
end
#search for toons over time
t3 = Thread.new do
query_txt = ‘occupation:toon’
query = @parser.parse(query_txt)
while true do
hits = @searcher.search(query)
p “t3: searching for #{query_txt} found #{hits.total_hits}”
return if hits.total_hits == 5
sleep(5)
end
end
t1.join; t2.join; t3.join