-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_picky.rb
51 lines (44 loc) · 1.48 KB
/
search_picky.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
51
require("yajl")
require("picky")
Picky.logger = Picky::Loggers::Silent.new
class Search
Document = Struct.new :id, :uri, :body
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?("index")
file = Dir[File.join("index", "development", "terms", "*")].first
if (File.mtime("#{DIR}/static/manual-en.txt") < File.mtime(file))
@index.load
@update = false
end
end
end
def add_document(terms = {})
@documents << Document.new(@documents.size + 1, terms[:uri], terms[:body])
@index.add @documents[-1] if @update
end
def find_all(terms)
retval = []
results = @search.search(terms)
results.sort_by { |id| id }
results.ids.each do |id|
document = @documents.detect { |n| n.id == id }
retval << [document.uri] unless document.nil?
end
retval
end
def finish!
@index.dump if @update
end
end