Class: Polecat::IndexSearcher
- Inherits:
-
Object
- Object
- Polecat::IndexSearcher
- Defined in:
- lib/polecat/index_searcher.rb
Overview
interface for searching an index
Build on top of an Polecat::IndexReader, this class let’s you search through all documents stored in an index.
Instance Attribute Summary collapse
-
#default_field ⇒ Object
readonly
Returns the value of attribute default_field.
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
Instance Method Summary collapse
-
#initialize(options) ⇒ IndexSearcher
constructor
creates a new Polecat::IndexSearcher.
-
#path ⇒ String
returns the path of the index directory.
-
#search(query) ⇒ Array
searches through all documents.
Constructor Details
#initialize(options) ⇒ IndexSearcher
creates a new Polecat::IndexSearcher
Create a new Polecat::IndexSearcher to search documents. Either a path to a directory or a Polecat::IndexReader has to be given, to make this searcher work.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/polecat/index_searcher.rb', line 19 def initialize if .has_key? :path @reader = Polecat::IndexReader.new([:path]) elsif .has_key? :reader @reader = [:reader] raise ArgumentError, 'no reader' unless @reader.kind_of?(Polecat::IndexReader) end if .has_key? :default_field @default_field = [:default_field] end end |
Instance Attribute Details
#default_field ⇒ Object (readonly)
Returns the value of attribute default_field.
8 9 10 |
# File 'lib/polecat/index_searcher.rb', line 8 def default_field @default_field end |
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
7 8 9 |
# File 'lib/polecat/index_searcher.rb', line 7 def reader @reader end |
Instance Method Details
#path ⇒ String
returns the path of the index directory
34 35 36 |
# File 'lib/polecat/index_searcher.rb', line 34 def path @reader.path end |
#search(query) ⇒ Array
searches through all documents
Run the query against the @default_field@ of every stored document to get a list of all matching documents.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/polecat/index_searcher.rb', line 44 def search query @reader.read.select do |doc| #doc.attributes.fetch(@default_field).fetch(:value) == query rs = [] query.terms.each do |term| val = doc.send(term.field.to_sym) if compare val, term.operator, term.value rs << true end end if query.relation == :and rs.count == query.terms.count else !rs.empty? end end end |