Class: RDig::Search::Searcher
- Inherits:
-
Object
- Object
- RDig::Search::Searcher
- Includes:
- Ferret::Search
- Defined in:
- lib/rdig/search.rb
Overview
This class is used to search the index. Call RDig::searcher to retrieve an instance ready for use.
Instance Attribute Summary collapse
-
#query_parser ⇒ Object
readonly
the query parser used to parse query strings.
Instance Method Summary collapse
- #build_extract(data) ⇒ Object
-
#ferret_searcher ⇒ Object
returns the Ferret::Search::IndexSearcher instance used internally.
- #get_maximum_score(query, options) ⇒ Object
-
#initialize(settings) ⇒ Searcher
constructor
takes the ferret section of the rdig configuration as a parameter.
-
#search(query, options = {}) ⇒ Object
run a search.
Constructor Details
#initialize(settings) ⇒ Searcher
takes the ferret section of the rdig configuration as a parameter.
13 14 15 16 17 |
# File 'lib/rdig/search.rb', line 13 def initialize(settings) @ferret_config = settings @query_parser = Ferret::QueryParser.new(settings.marshal_dump) ferret_searcher end |
Instance Attribute Details
#query_parser ⇒ Object (readonly)
the query parser used to parse query strings
10 11 12 |
# File 'lib/rdig/search.rb', line 10 def query_parser @query_parser end |
Instance Method Details
#build_extract(data) ⇒ Object
70 71 72 |
# File 'lib/rdig/search.rb', line 70 def build_extract(data) (data && data.length > 200) ? data[0..200] : data end |
#ferret_searcher ⇒ Object
returns the Ferret::Search::IndexSearcher instance used internally.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rdig/search.rb', line 20 def ferret_searcher if @ferret_searcher and !@ferret_searcher.reader.latest? # reopen searcher @ferret_searcher.close @ferret_searcher = nil end unless @ferret_searcher @ferret_searcher = Ferret::Search::Searcher.new(@ferret_config.path) @query_parser.fields = @ferret_searcher.reader.field_names.to_a end @ferret_searcher end |
#get_maximum_score(query, options) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/rdig/search.rb', line 33 def get_maximum_score(query, ) ferret_searcher.search_each(query, .merge(:limit => 1, :offset => 0)) do |doc_id, score| return score end 0 end |
#search(query, options = {}) ⇒ Object
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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rdig/search.rb', line 50 def search(query, ={}) result = {} query = query_parser.parse(query) if query.is_a?(String) RDig.logger.info "Query: #{query}" results = [] searcher = ferret_searcher maximum_score = get_maximum_score query, result[:hitcount] = searcher.search_each(query, ) do |doc_id, score| doc = searcher[doc_id] results << { :score => score, :title => doc[:title], :url => doc[:url], :extract => build_extract(doc[:data]), :relative_score => (score / maximum_score) } end result[:list] = results result end |