Class: Search

Inherits:
Object
  • Object
show all
Defined in:
lib/shoes/search.rb

Defined Under Namespace

Classes: Document

Instance Method Summary collapse

Constructor Details

#initializeSearch

Returns a new instance of Search


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/shoes/search.rb', line 11

def initialize
   @documents = []
   @index = Picky::Index.new :terms do
      indexing removes_characters: %r{[^a-z0-9\s\/\-\_\:\"\&\.]}i,
         splits_text_on:     %r{[\s/\-\_\:\"\&/\.]}
      category :uri, :from => lambda { |doc| doc.uri.dup }
      category :body, :from => lambda { |doc| doc.body.dup }
   end
   @search = Picky::Search.new @index do 
      searching removes_characters: %r{[^a-z0-9\s\/\-\_\:\"\&\.]}i,
         splits_text_on:     %r{[\s/\-\_\:\"\&/\.]}
   end
   
   @update = true
   if File.directory?(File.join(Picky.root, "index"))
      file = Dir[File.join(Picky.root, "index", "development", "terms", "*")].first
      if (File.mtime("#{DIR}/static/manual-en.txt") < File.mtime(file))
         @index.load
         @update = false
      end
   end
end

Instance Method Details

#add_document(terms = {}) ⇒ Object


34
35
36
37
# File 'lib/shoes/search.rb', line 34

def add_document(terms = {})
   @documents << Document.new(@documents.size + 1, terms[:uri], terms[:body])
   @index.add @documents[-1] if @update
end

#find_all(terms) ⇒ Object


39
40
41
42
43
44
45
46
47
48
# File 'lib/shoes/search.rb', line 39

def find_all(terms)
   retval = []
   results = @search.search(terms)
   results.sort_by { |id| @documents.detect { |n| n.id == id }.uri =~ /#{terms}/ ? 0 : id }
   results.ids.each do |id|
      document = @documents.detect { |n| n.id == id }
      retval << [document.uri] unless document.nil?
   end
   retval
end

#finish!Object


50
51
52
# File 'lib/shoes/search.rb', line 50

def finish!
   @index.dump if @update
end