Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 10
- Log:
Checking in HEAD from RForum's SVN of 22-Jul-2006, 8pm (revision 906).
- Author:
- adh
- Date:
- Sat Jul 22 20:02:44 +0100 2006
- Size:
- 3043 Bytes
1 | module FerretMixin |
2 | module Acts #:nodoc: |
3 | module ARFerret #:nodoc: |
4 | # not threadsafe |
5 | class MultiIndex |
6 | |
7 | attr_reader :reader |
8 | |
9 | # todo: check for necessary index rebuilds in this place, too |
10 | # idea - each class gets a create_reader method that does this |
11 | def initialize(model_classes, options = {}) |
12 | @model_classes = model_classes |
13 | @options = { |
14 | :default_search_field => '*', |
15 | :analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new |
16 | }.update(options) |
17 | end |
18 | |
19 | def search(query, options={}) |
20 | #puts "querystring: #{query.to_s}" |
21 | query = process_query(query) |
22 | #puts "parsed query: #{query.to_s}" |
23 | searcher.search(query, options) |
24 | end |
25 | |
26 | # checks if all our sub-searchers still are up to date |
27 | def latest? |
28 | return false unless @searcher |
29 | @sub_searchers.each do |s| |
30 | return false unless s.reader.latest? |
31 | end |
32 | true |
33 | end |
34 | |
35 | def ensure_searcher |
36 | unless latest? |
37 | field_names = Set.new |
38 | @sub_searchers = @model_classes.map { |clazz| |
39 | begin |
40 | searcher = Ferret::Search::IndexSearcher.new(clazz.class_index_dir) |
41 | rescue Exception |
42 | puts "error opening #{clazz.class_index_dir}: #{$!}" |
43 | end |
44 | if searcher.reader.respond_to?(:get_field_names) |
45 | field_names << searcher.reader.send(:get_field_names).to_set |
46 | elsif clazz.fields_for_ferret |
47 | field_names << clazz.fields_for_ferret.to_set |
48 | else |
49 | puts <<-END |
50 | unable to retrieve field names for class #{clazz.name}, please |
51 | consider naming all indexed fields in your call to acts_as_ferret! |
52 | END |
53 | clazz.content_columns.each { |col| field_names << col.name } |
54 | end |
55 | searcher |
56 | } |
57 | @searcher = Ferret::Search::MultiSearcher.new(@sub_searchers) |
58 | @field_names = field_names.flatten.to_a |
59 | @query_parser = nil # trigger re-creation from new field_name array |
60 | end |
61 | end |
62 | |
63 | def searcher |
64 | ensure_searcher |
65 | @searcher |
66 | end |
67 | |
68 | def doc(i) |
69 | searcher.doc(i) |
70 | end |
71 | |
72 | def query_parser |
73 | unless @query_parser |
74 | ensure_searcher # we dont need the searcher, but the @field_names array is built by this function, too |
75 | @query_parser ||= Ferret::QueryParser.new( |
76 | @options[:default_search_field], |
77 | { :fields => @field_names }.merge(@options) |
78 | ) |
79 | end |
80 | @query_parser |
81 | end |
82 | |
83 | def process_query(query) |
84 | query = query_parser.parse(query) if query.is_a?(String) |
85 | return query |
86 | end |
87 | |
88 | end # of class MultiIndex |
89 | |
90 | end |
91 | end |
92 | end |