1 module ActsAsFerret #:nodoc:
3 # this class is not threadsafe
6 def initialize(model_classes, options = {})
7 @model_classes = model_classes
8 # ensure all models indexes exist
9 @model_classes.each { |m| m.aaf_index.ensure_index_exists }
10 default_fields = @model_classes.inject([]) do |fields, c|
11 fields + c.aaf_configuration[:ferret][:default_field]
14 :default_field => default_fields
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?
51 @query_parser ||= Ferret::QueryParser.new(@options)
54 def process_query(query)
55 query = query_parser.parse(query) if query.is_a?(String)
60 @searcher.close if @searcher
61 @reader.close if @reader
68 @sub_readers = @model_classes.map { |clazz|
70 reader = Ferret::Index::IndexReader.new(clazz.aaf_configuration[:index_dir])
72 puts "error opening #{clazz.aaf_configuration[:index_dir]}: #{$!}"
77 @reader = Ferret::Index::IndexReader.new(@sub_readers)
78 @searcher = Ferret::Search::Searcher.new(@reader)
82 end # of class MultiIndex