I’ve whipped up this script to demonstrate what I’m trying [and failing]
to achieve. The idea is that thread t1 adds docs to the index over time,
while threads t2 and t3 search the same index for the new docs.
Unfortunately the script doesn’t work, as t2 and t3 don’t find the docs
that t1 has added.
Can anyone point out where I am going wrong. Thanks so much.
Neville
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