# Ferret killer - John Leach - http://johnleach.co.uk # Crashes Ferret 0.10.14, 0.11.2 (most likely all 0.10.* versions) # run with first argument 'index' to run an indexer process # run with first argument 'search' to run a searcher process # run one indexer and one searcher - Ferret will crash out with various errors # class FerretKiller require 'rubygems' require 'ferret' include Ferret @@ferret_index = nil # Return the Ferret index object for this class. Initialise if necessary def self.ferret_index(options = {}) ferret_init_index(options) if @@ferret_index.nil? or options[:create] == true @@ferret_index end # Initialise ferret index for this class def self.ferret_init_index(options = {}) @@ferret_index.close unless @@ferret_index.nil? field_infos = Index::FieldInfos.new(:term_vector => :no, :store => :no) field_infos.add_field(:id, :index => :untokenized, :store => :yes) field_infos.add_field(:text) @@ferret_index = Index::Index.new(:path => "ferret_killer_index/", :field_infos => field_infos, :id_field => :id, :key => :id, :default_input_field => :text, :create => true) end # Execute a search on the ferret index and return matching collection objects def self.ferret_search(search_string, options = {}, activerecord_options = {}) versions = {} options = { :limit => 10 }.merge(options) options[:page] = 1 unless options[:page] @@query_parser ||= QueryParser.new(:default_slop => 2, :all_fields => [:text, :title, :url], :or_default => false) query = @@query_parser.parse(search_string) hits = self.ferret_index.search( query, :limit => options[:limit], :sort => options[:sort], :offset => (options[:page].to_i-1) * options[:limit] ) return hits end end if ARGV.first == "index" puts "Indexing..." while true do id = rand(2000) FerretKiller.ferret_index << { :id => id, :text => "no real text here" } puts "Added id #{id} to index" end end if ARGV.first == "search" puts "Searching..." while true do puts FerretKiller.ferret_search("here") end end