3 module ARFerret #:nodoc:
7 # todo: check for necessary index rebuilds in this place, too
8 # idea - each class gets a create_reader method that does this
9 def initialize(model_classes, options = {})
10 @model_classes = model_classes
12 :default_field => '*',
13 #:analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new
14 :analyzer => Ferret::Analysis::StandardAnalyzer.new
18 def search(query, options={})
19 #puts "querystring: #{query.to_s}"
20 query = process_query(query)
21 #puts "parsed query: #{query.to_s}"
22 searcher.search(query, options)
25 def search_each(query, options = {}, &block)
26 query = process_query(query)
27 searcher.search_each(query, options, &block)
30 # checks if all our sub-searchers still are up to date
32 return false unless @reader
33 # segfaults with 0.10.4 --> TODO report as bug @reader.latest?
34 @sub_readers.each do |r|
35 return false unless r.latest?
53 @query_parser ||= Ferret::QueryParser.new(@options)
55 @query_parser.fields = @reader.field_names
59 def process_query(query)
60 query = query_parser.parse(query) if query.is_a?(String)
65 @searcher.close if @searcher
66 @reader.close if @reader
73 @sub_readers = @model_classes.map { |clazz|
75 reader = Ferret::Index::IndexReader.new(clazz.class_index_dir)
77 puts "error opening #{clazz.class_index_dir}: #{$!}"
82 @reader = Ferret::Index::IndexReader.new(@sub_readers)
83 @searcher = Ferret::Search::Searcher.new(@reader)
87 end # of class MultiIndex