| Class | RDig::Search::Searcher |
| In: |
lib/rdig/search.rb
|
| Parent: | Object |
This class is used to search the index. Call RDig::searcher to retrieve an instance ready for use.
| query_parser | [R] | the query parser used to parse query strings |
takes the ferret section of the rdig configuration as a parameter.
# File lib/rdig/search.rb, line 13
13: def initialize(settings)
14: @ferret_config = settings
15: @query_parser = Ferret::QueryParser.new(settings.marshal_dump)
16: ferret_searcher
17: end
# File lib/rdig/search.rb, line 60
60: def build_extract(data)
61: (data && data.length > 200) ? data[0..200] : data
62: end
returns the Ferret::Search::IndexSearcher instance used internally.
# File lib/rdig/search.rb, line 20
20: def ferret_searcher
21: if @ferret_searcher and !@ferret_searcher.reader.latest?
22: # reopen searcher
23: @ferret_searcher.close
24: @ferret_searcher = nil
25: end
26: unless @ferret_searcher
27: @ferret_searcher = Ferret::Search::Searcher.new(@ferret_config.path)
28: @query_parser.fields = @ferret_searcher.reader.field_names.to_a
29: end
30: @ferret_searcher
31: end
run a search. query usually will be a user-entered string. See the Ferret query language for more information on queries. A Ferret::Search::Query instance may be given, too.
Some of the more often used otions are:
| offset: | first document in result list to retrieve (0-based). The default is 0. |
| limit: | number of documents to retrieve. The default is 10. |
Please see the Ferret::Search::Searcher API for more options.
# File lib/rdig/search.rb, line 43
43: def search(query, options={})
44: result = {}
45: query = query_parser.parse(query) if query.is_a?(String)
46: puts "Query: #{query}"
47: results = []
48: searcher = ferret_searcher
49: result[:hitcount] = searcher.search_each(query, options) do |doc_id, score|
50: doc = searcher[doc_id]
51: results << { :score => score,
52: :title => doc[:title],
53: :url => doc[:url],
54: :extract => build_extract(doc[:data]) }
55: end
56: result[:list] = results
57: result
58: end