-
Notifications
You must be signed in to change notification settings - Fork 3
/
full-text-index.lisp
116 lines (105 loc) · 4.11 KB
/
full-text-index.lisp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
(in-package #:vivace-graph)
(defun tokenize-string (seq)
(remove-if #'(lambda (token)
(< (length token) 3))
(tokenize seq
:escapes #\\
:multi-escapes "\"|"
:delimiters (format nil "_,:~A" #\Tab)
:terminators ""
:punctuation "[]()!.?~`;'<>/+=-*&^%$#@"
:whitespace :whitespace
:defaults (let ((i 0))
(lambda () (incf i))))))
(defmethod index-text ((triple triple))
(with-transaction ((full-text-idx *graph*))
(when (stringp (triple-subject triple))
(dolist (token (remove-duplicates (tokenize-string (triple-subject triple)) :test 'equalp))
(set-btree (full-text-idx *graph*)
(make-slot-key "s" (string-downcase token))
(triple-uuid triple) :mode :concat)))
(when (stringp (triple-object triple))
(dolist (token (remove-duplicates (tokenize-string (triple-object triple)) :test 'equalp))
(set-btree (full-text-idx *graph*)
(make-slot-key "o" (string-downcase token))
(triple-uuid triple) :mode :concat)))))
(defmethod deindex-text ((triple triple))
(with-transaction ((full-text-idx *graph*))
(when (stringp (triple-subject triple))
(dolist (token (remove-duplicates (tokenize-string (triple-subject triple)) :test 'equalp))
(rem-btree (full-text-idx *graph*)
(make-slot-key "s" (string-downcase token))
:value (triple-uuid triple))))
(when (stringp (triple-object triple))
(dolist (token (remove-duplicates (tokenize-string (triple-object triple)) :test 'equalp))
(rem-btree (full-text-idx *graph*)
(make-slot-key "o" (string-downcase token))
:value (triple-uuid triple))))))
(defun full-text-search (string &key subject? object?)
(let ((result nil))
(dolist (token (remove-duplicates (tokenize-string string) :test 'equalp))
(when subject?
(let ((klist (get-btree (full-text-idx *graph*)
(make-slot-key "s" (string-downcase token)) :mode :klist)))
(when (klist? klist)
(unwind-protect
(map-klist #'(lambda (id)
(format t "GOT ~A~%" (lookup-triple-by-id id))
(pushnew (lookup-triple-by-id id) result)) klist)
(klist-free klist)))))
(when object?
(let ((klist (get-btree (full-text-idx *graph*)
(make-slot-key "o" (string-downcase token)) :mode :klist)))
(when (klist? klist)
(unwind-protect
(map-klist #'(lambda (id)
(format t "GOT ~A~%" (lookup-triple-by-id id))
(pushnew (lookup-triple-by-id id) result)) klist)
(klist-free klist))))))
result))
#|
(defmethod index-text ((triple triple))
(when (or (stringp (triple-subject triple))
(stringp (triple-object triple)))
(let ((doc (make-instance 'montezuma:document)))
(montezuma:add-field
doc (montezuma:make-field "uuid"
(format nil "~A" (triple-uuid triple))
:stored t :index :untokenized))
(when (stringp (triple-subject triple))
(montezuma:add-field
doc (montezuma:make-field "subject"
(triple-subject triple)
:stored t :index :tokenized)))
(when (stringp (triple-object triple))
(montezuma:add-field
doc (montezuma:make-field "object"
(triple-object triple)
:stored t :index :tokenized)))
(with-recursive-lock-held ((full-text-lock *graph*))
(montezuma:add-document-to-index (full-text-idx *graph*) doc)))))
(defmethod deindex-text ((triple triple))
(let ((docs nil))
(with-recursive-lock-held ((full-text-lock *graph*))
(montezuma:search-each
(full-text-idx *graph*)
(uuid:print-bytes nil (triple-uuid triple))
#'(lambda (doc score) (declare (ignore score)) (push doc docs)))
(dolist (doc docs)
(montezuma:delete-document (full-text-idx *graph*) doc)))))
(defun map-text-search (string fn &key collect)
(let ((result nil))
(with-recursive-lock-held ((full-text-lock *graph*))
(montezuma:search-each
(full-text-idx *graph*)
string
#'(lambda (doc score)
(declare (ignore score))
(let ((r
(funcall fn
(lookup-triple-by-id
(montezuma:document-value
(montezuma:get-document (full-text-idx *graph*) doc) "uuid")))))
(if collect (push r result)))))
(nreverse result))))
|#