Class: Stellr::Collections::SearchableCollection

Inherits:
Base
  • Object
show all
Defined in:
lib/stellr/collections/searchable_collection.rb

Overview

Base class for searchable collection implementations

Direct Known Subclasses

MultiCollection, WriteableCollection

Instance Attribute Summary

Attributes inherited from Base

#logger, #name

Instance Method Summary collapse

Methods inherited from Base

#batch_finished, create

Methods included from Utils::Observable

#add_listener, #listeners, #notify_listeners

Methods included from Utils::Shutdown

#shutdown, #shutting_down?

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, options )
  super name, options
  @reader_monitor = Monitor.new
  @query_parser_monitor = Monitor.new
  @reader = @searcher = @query_parser = nil
end

Instance Method Details

#closeObject

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, options = {})
  return searcher.highlight(process_query(query, options), doc_id, options[:field], options)
rescue
  @logger.error "error in highlight: #{$!}. Document #{doc_id}, Query: #{query}, options: #{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, options = {})
  results = Stellr::Search::SearchResults.new

  if options[:page]
    results.current_page = options.delete(:page).to_i
    options[:limit] = results.per_page = (options.delete(:per_page).to_i rescue nil) || 10
    options[:offset] = (p = results.current_page - 1) <= 0 ? 0 : p * results.per_page
  end

  get_fields = options.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, options
    @logger.debug "options: #{options.inspect}"
    results.total_hits = searcher.search_each q, options 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

#sizeObject



59
60
61
# File 'lib/stellr/collections/searchable_collection.rb', line 59

def size
  reader.num_docs
end