Class: Stellr::Collections::SearchableCollection
- Defined in:
- lib/stellr/collections/searchable_collection.rb
Overview
Base class for searchable collection implementations
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#close ⇒ Object
close this collection.
- #highlight(doc_id, query, options = {}) ⇒ Object
-
#initialize(name, options) ⇒ SearchableCollection
constructor
A new instance of SearchableCollection.
- #on_shutdown(mode) ⇒ Object
-
#search(query, options = {}) ⇒ Object
Search this collection.
- #size ⇒ Object
Methods inherited from Base
Methods included from Utils::Observable
#add_listener, #listeners, #notify_listeners
Methods included from Utils::Shutdown
Constructor Details
#initialize(name, options) ⇒ SearchableCollection
Returns a new instance of SearchableCollection.
7 8 9 10 11 12 |
# File 'lib/stellr/collections/searchable_collection.rb', line 7 def initialize( name, ) super name, @reader_monitor = Monitor.new @query_parser_monitor = Monitor.new @reader = @searcher = @query_parser = nil end |
Instance Method Details
#close ⇒ Object
close this collection
68 69 70 |
# File 'lib/stellr/collections/searchable_collection.rb', line 68 def close close_reader end |
#highlight(doc_id, query, options = {}) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/stellr/collections/searchable_collection.rb', line 52 def highlight( doc_id, query, = {}) return searcher.highlight(process_query(query, ), doc_id, [:field], ) rescue @logger.error "error in highlight: #{$!}. Document #{doc_id}, Query: #{query}, options: #{.inspect}" '' end |
#on_shutdown(mode) ⇒ Object
63 64 65 |
# File 'lib/stellr/collections/searchable_collection.rb', line 63 def on_shutdown( mode ) close end |
#search(query, options = {}) ⇒ Object
Search this collection. Options is a hash taking the usual Ferret::Search::Searcher options, plus:
page
-
Page of results to show, starting with 1
per_page
-
Number of records per page, default 10.
fields
-
Array of fields to search in
get_fields
-
Array of fields to retrieve in addition to the :id field
The page and per_page options take precedence over any given limit and offset values.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/stellr/collections/searchable_collection.rb', line 26 def search(query, = {}) results = Stellr::Search::SearchResults.new if [:page] results.current_page = .delete(:page).to_i [:limit] = results.per_page = (.delete(:per_page).to_i rescue nil) || 10 [:offset] = (p = results.current_page - 1) <= 0 ? 0 : p * results.per_page end get_fields = .delete :get_fields # TODO replace synchronization with some kind of shared read/exclusive # write locking mechanism allowing parallel searches but guarding # against the reader instance being shut down while we're inside # retrieve_field_data @reader_monitor.synchronize do q = process_query query, @logger.debug "options: #{.inspect}" results.total_hits = searcher.search_each q, do |id, score| field_data = retrieve_field_data(id, get_fields) results << Stellr::Search::SearchResult.new( id, score, field_data ) end @logger.info "query #{query} : #{results.total_hits} results" end return results end |
#size ⇒ Object
59 60 61 |
# File 'lib/stellr/collections/searchable_collection.rb', line 59 def size reader.num_docs end |