-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathsearch.clj
599 lines (538 loc) · 21.7 KB
/
search.clj
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
(ns datalevin.search
"Full-text search engine"
(:require [datalevin.lmdb :as l]
[datalevin.util :as u]
[datalevin.sparselist :as sl]
[datalevin.constants :as c]
[datalevin.bits :as b])
(:import [datalevin.sm SymSpell SuggestItem]
[datalevin.utl PriorityQueue]
[datalevin.sparselist SparseIntArrayList]
[java.util HashMap ArrayList Map$Entry Arrays]
[java.util.concurrent.atomic AtomicInteger]
[java.io Writer]
[org.eclipse.collections.impl.map.mutable.primitive IntShortHashMap
IntDoubleHashMap]
[org.eclipse.collections.impl.list.mutable.primitive IntArrayList]
[org.eclipse.collections.impl.list.mutable FastList]
[org.eclipse.collections.impl.map.mutable UnifiedMap]
[org.roaringbitmap RoaringBitmap FastAggregation
FastRankRoaringBitmap PeekableIntIterator]))
(if (u/graal?)
(require 'datalevin.binding.graal)
(require 'datalevin.binding.java))
(defn- non-token-char?
[^Character c]
(or (Character/isWhitespace c) (c/en-punctuations? c)))
(defn en-analyzer
"English analyzer does the following:
- split on white space and punctuation, remove them
- lower-case all characters
- remove stop words
Return a list of [term, position, offset]"
[^String x]
(let [len (.length x)
len-1 (dec len)
res (FastList.)]
(loop [i 0
pos 0
in? false
start 0
sb (StringBuilder.)]
(if (< i len)
(let [c (.charAt x i)]
(if (non-token-char? c)
(if in?
(let [word (.toString sb)]
(when-not (c/en-stop-words? word)
(.add res [word pos start]))
(recur (inc i) (inc pos) false i (StringBuilder.)))
(recur (inc i) pos false i sb))
(recur (inc i) pos true (if in? start i)
(.append sb (Character/toLowerCase c)))))
(let [c (.charAt x len-1)]
(if (non-token-char? c)
res
(let [word (.toString sb)]
(when-not (c/en-stop-words? word)
(.add res [word pos start]))
res)))))))
(defn- collect-terms
[result]
(let [terms (HashMap.)]
(doseq [[term position offset] result]
(when (< (count term) c/+max-term-length+)
(.put terms term (if-let [^FastList lst (.get terms term)]
(do (.add lst [position offset]) lst)
(doto (FastList.) (.add [position offset]))))))
terms))
(defn- get-term-info
[lmdb term]
(l/get-value lmdb c/terms term :string :term-info))
(defn- idf
"inverse document frequency of a term"
[^long freq N]
(if (zero? freq) 0 (Math/log10 (/ ^long N freq))))
(defn- tf*
"log-weighted term frequency"
[freq]
(if (zero? ^short freq) 0 (+ (Math/log10 ^short freq) 1)))
(defn- add-max-weight
[mw tf norm]
(let [w (/ ^double (tf* tf) ^short norm)]
(if (< ^double mw w) w mw)))
(defn- del-max-weight
[^SparseIntArrayList sl doc-id mw tf norm]
(let [w (/ ^double (tf* tf) ^short norm)]
(if (= mw w)
(if (= (sl/size sl) 1)
0.0
(apply max (map #(/ (if (= doc-id %)
0.0
^double (tf* (sl/get sl %)))
^short norm)
(.-indices sl))))
mw)))
(defn- add-doc-txs
[lmdb doc-text ^AtomicInteger max-doc ^FastList txs doc-ref
^IntShortHashMap norms ^AtomicInteger max-term ^UnifiedMap hit-terms]
(let [result (en-analyzer doc-text)
new-terms ^HashMap (collect-terms result)
unique (.size new-terms)
doc-id (.incrementAndGet max-doc)]
(.add txs [:put c/docs doc-id [unique doc-ref] :int :doc-info])
(when norms (.put norms doc-id unique))
(doseq [^Map$Entry kv (.entrySet new-terms)]
(let [term (.getKey kv)
new-lst ^FastList (.getValue kv)
tf (.size new-lst)
[tid mw sl] (or (.get hit-terms term)
;; (when pre-terms (.get pre-terms term))
(get-term-info lmdb term)
[(.incrementAndGet max-term) 0.0
(sl/sparse-arraylist)])]
(.put hit-terms term [tid (add-max-weight mw tf unique)
(sl/set sl doc-id tf)])
(.add txs [:put-list c/positions [tid doc-id] new-lst
:int-int :int-int])))))
(defn- hydrate-query
[lmdb ^AtomicInteger max-doc ^SymSpell symspell tokens]
(let [sis (when symspell
(.getSuggestTerms symspell tokens c/dict-max-edit-distance false))
tms (if sis
(mapv #(.getSuggestion ^SuggestItem %) sis)
tokens)
eds (zipmap tms (if sis
(mapv #(.getEditDistance ^SuggestItem %) sis)
(repeat 0)))]
(into []
(comp
(map (fn [[term freq]]
(when-let [[id mw ^SparseIntArrayList sl]
(get-term-info lmdb term)]
(let [df (sl/size sl)
sl (sl/->SparseIntArrayList
(doto (FastRankRoaringBitmap.)
(.or ^RoaringBitmap (.-indices sl)))
(.-items sl))]
{:df df
:ed (eds term)
:id id
:mw mw
:sl sl
:tm term
:wq (* ^double (tf* freq)
^double (idf df (.get max-doc)))}))))
(filter map?))
(frequencies tms))))
(defn- priority-queue
[top]
(proxy [PriorityQueue] [top]
(lessThan [a b]
(< ^double (nth a 0) ^double (nth b 0)))))
(defn- pouring
[coll ^PriorityQueue pq ^RoaringBitmap result]
(let [lst (ArrayList.)]
(dotimes [_ (.size pq)]
(let [[_ did :as res] (.pop pq)]
(.add lst 0 res)
(.add result (int did))))
(reduce conj! coll lst)))
(defn- real-score
[tid did tf ^IntDoubleHashMap wqs ^IntShortHashMap norms]
(/ (* ^double (.get wqs tid) ^double (tf* tf))
(double (.get norms did))))
(defn- max-score
[^IntDoubleHashMap wqs ^IntDoubleHashMap mws tid]
(* ^double (.get wqs tid) ^double (.get mws tid)))
(defn- get-ws
[tids qterms k]
(let [m (IntDoubleHashMap.)]
(dorun (map (fn [tid qterm] (.put m tid (qterm k))) tids qterms))
m))
(defn- get-mxs [tids wqs mws]
(let [m (IntDoubleHashMap.)]
(doseq [tid tids] (.put m tid (max-score wqs mws tid)))
m))
(defprotocol ICandidate
(skip-before [this limit] "move the iterator to just before the limit")
(advance [this] "move the iterator to the next position")
(has-next? [this] "return true if there's next in iterator")
(get-did [this] "return the current did the iterator points to")
(get-tf [this] "return tf of the current did"))
(deftype Candidate [tid
^:volatile-mutable did
^SparseIntArrayList sl
^PeekableIntIterator iter]
ICandidate
(skip-before [this limit]
(.advanceIfNeeded iter limit)
this)
(has-next? [_]
(.hasNext iter))
(advance [this]
(set! did (.next iter))
this)
(get-did [_]
did)
(get-tf [_]
(.get ^IntArrayList (.-items sl)
(dec (.rank ^FastRankRoaringBitmap (.-indices sl) did)))))
(def candidate-comp
(comparator (fn [^Candidate a ^Candidate b]
(- ^int (get-did a) ^int (get-did b)))))
(defmethod print-method Candidate [^Candidate c, ^Writer w]
(.write w (pr-str {:tid (.-tid c)
:did (get-did c)
:tf (get-tf c)
:sl (.-sl c)
:has-next? (has-next? c)})))
(defn- find-pivot
[^IntDoubleHashMap mxs tao-1 minimal-score
^"[Ldatalevin.search.Candidate;" candidates]
(let [n (alength candidates)]
(loop [score 0.0 p 0]
(if (< p n)
(let [candidate ^Candidate (aget candidates p)
s (+ score ^double (.get mxs (.-tid candidate)))]
(if (and (<= ^long tao-1 p) (< ^double minimal-score s))
[s p (get-did candidate)]
(recur s (inc p))))
(let [n-1 (dec n)]
[score n-1 (get-did (aget candidates n-1))])))))
(defn- score-pivot
[wqs ^IntDoubleHashMap mxs norms pivot-did minimal-score mxscore tao n
^"[Ldatalevin.search.Candidate;" candidates]
(let [c (alength candidates)]
(loop [score mxscore hits 0 k 0]
(if (< k c)
(let [candidate ^Candidate (aget candidates k)
did (get-did candidate)]
(if (= ^int did ^int pivot-did)
(let [h (inc hits)]
(if (< ^long (+ h ^long (- ^long n k 1)) ^long tao)
:prune
(let [tid (.-tid candidate)
s (+ (- ^double score ^double (.get mxs tid))
^double (real-score tid did (get-tf candidate)
wqs norms))]
(if (< s ^double minimal-score)
:prune
(recur s h (inc k))))))
score))
score))))
(defn- first-candidates
[sls bms tids ^RoaringBitmap result tao n]
(let [z (inc (- ^long n ^long tao))
union-tids (set (take z tids))
union-bms (->> (select-keys bms union-tids)
vals
(into-array RoaringBitmap))
union-bm (FastAggregation/or
^"[Lorg.roaringbitmap.RoaringBitmap;" union-bms)
lst (ArrayList.)]
(doseq [tid tids]
(let [sl (sls tid)
bm ^RoaringBitmap (bms tid)
bm' (let [iter-bm (.clone bm)]
(if (or (union-tids tid) (= tao 1))
iter-bm
(doto ^RoaringBitmap iter-bm
(.and ^RoaringBitmap union-bm))))
bm' (doto ^RoaringBitmap bm' (.andNot result))
iter (.getIntIterator ^RoaringBitmap bm')]
(when (.hasNext ^PeekableIntIterator iter)
(let [did (.peekNext iter)
candidate (->Candidate tid did sl iter)]
(.add lst (advance candidate))))))
(.toArray lst ^"[Ldatalevin.search.Candidate;" (make-array Candidate 0))))
(defn- next-candidates
[did ^"[Ldatalevin.search.Candidate;" candidates]
(let [lst (FastList.)]
(dotimes [i (alength candidates)]
(let [candidate (aget candidates i)]
(skip-before candidate (inc ^int did))
(when (has-next? candidate) (.add lst (advance candidate)))))
(.toArray lst ^"[Ldatalevin.search.Candidate;" (make-array Candidate 0))))
(defn- skip-candidates
[pivot pivot-did ^"[Ldatalevin.search.Candidate;" candidates]
(let [lst (FastList.)]
(dotimes [i (alength candidates)]
(let [candidate (aget candidates i)]
(if (< i ^long pivot)
(do (skip-before candidate pivot-did)
(when (has-next? candidate) (.add lst (advance candidate))))
(.add lst candidate))))
(.toArray lst ^"[Ldatalevin.search.Candidate;" (make-array Candidate 0))))
(defn- current-threshold
[^PriorityQueue pq]
(if (< (.size pq) (.maxSize pq))
0.0
(nth (.top pq) 0)))
(defn- score-term
[^Candidate candidate ^IntDoubleHashMap mxs wqs norms minimal-score pq]
(let [tid (.-tid candidate)]
(when (< ^double minimal-score (.get mxs tid))
(loop [did (get-did candidate) minscore minimal-score]
(let [score (real-score tid did (get-tf candidate) wqs norms)]
(when (< ^double minscore ^double score)
(.insertWithOverflow ^PriorityQueue pq [score did])))
(when (has-next? candidate)
(recur (get-did (advance candidate)) (current-threshold pq)))))))
(defn- score-docs
[n tids sls bms mxs wqs ^IntShortHashMap norms ^RoaringBitmap result]
(fn [^PriorityQueue pq ^long tao] ; target # of overlaps between query and doc
(loop [^"[Ldatalevin.search.Candidate;" candidates
(first-candidates sls bms tids result tao n)]
(let [nc (alength candidates)
minimal-score ^double (current-threshold pq)]
(cond
(or (= nc 0) (< nc tao)) :finish
(= nc 1)
(score-term (aget candidates 0) mxs wqs norms minimal-score pq)
:else
(let [_ (Arrays/sort candidates candidate-comp)
[mxscore pivot did] (find-pivot mxs (dec tao) minimal-score
candidates)]
(if (= ^int did ^int (get-did (aget candidates 0)))
(let [score (score-pivot wqs mxs norms did minimal-score
mxscore tao n candidates)]
(when-not (= score :prune) (.insertWithOverflow pq [score did]))
(recur (next-candidates did candidates)))
(recur (skip-candidates pivot did candidates)))))))))
(defn- get-doc-ref
[lmdb [_ doc-id]]
(nth (l/get-value lmdb c/docs doc-id :int :doc-info) 1))
(defn- add-offsets
[lmdb terms [_ doc-id :as result]]
[(get-doc-ref lmdb result)
(sequence
(comp (map (fn [tid]
(let [lst (l/get-list lmdb c/positions [tid doc-id]
:int-int :int-int)]
(when (seq lst)
[(terms tid) (mapv #(nth % 1) lst)]))))
(remove nil? ))
(keys terms))])
(defn- display-xf
[display lmdb tms]
(case display
:offsets (map #(add-offsets lmdb tms %))
:refs (map #(get-doc-ref lmdb %))))
(defn- doc-ref->id
[lmdb doc-ref]
(let [is-ref? (fn [kv]
(= doc-ref (nth (b/read-buffer (l/v kv) :doc-info) 1)))]
(nth (l/get-some lmdb c/docs is-ref? [:all] :int :doc-info) 0)))
(defn- doc-id->term-ids
[lmdb doc-id]
(dedupe
(map ffirst
(l/range-filter lmdb c/positions
(fn [kv]
(let [[_ did] (b/read-buffer (l/k kv) :int-int)]
(= did doc-id)))
[:all] :int-int :ignore false))))
(defn- term-id->info
[lmdb term-id]
(let [is-id? (fn [kv] (= term-id (b/read-buffer (l/v kv) :int)))]
(l/get-some lmdb c/terms is-id? [:all] :string :term-info false)))
(defprotocol ISearchEngine
(add-doc [this doc-ref doc-text]
"Add a document to the search engine, `doc-ref` can be arbitrary Clojure data
that uniquely refers to the document in the system.
`doc-text` is the content of the document as a string. The search engine
does not store the original text, and assumes that caller can retrieve them
by `doc-ref`. This function is for online update of search engine index,
for index creation of bulk data, use `search-index-writer`.")
(remove-doc [this doc-ref]
"Remove a document referred to by `doc-ref`. A slow operation.")
(search [this query] [this query opts]
"Issue a `query` to the search engine. `query` is a string.
`opts` map may have these keys:
* `:display` can be one of `:refs` (default), `:offsets`.
* `:top` is an integer (default 10), the number of results desired.
Return a lazy sequence of
`[doc-ref [term1 [offset ...]] [term2 [...]] ...]`,
ordered by relevance to the query. `term` and `offset` can be used to
highlight the matched terms and their locations in the documents."))
(deftype SearchEngine [lmdb
^IntShortHashMap norms ; doc-id -> norm
^SymSpell symspell
^AtomicInteger max-doc
^AtomicInteger max-term]
ISearchEngine
(add-doc [this doc-ref doc-text]
(let [txs (FastList.)
hit-terms (UnifiedMap.)]
(add-doc-txs lmdb doc-text max-doc txs doc-ref norms max-term
hit-terms)
(doseq [^Map$Entry kv (.entrySet hit-terms)]
(let [term (.getKey kv)
info (.getValue kv)]
(.add txs [:put c/terms term info :string :term-info])))
(l/transact-kv lmdb txs)))
(remove-doc [this doc-ref]
(if-let [doc-id (doc-ref->id lmdb doc-ref)]
(let [txs (FastList.)
norm (.get norms doc-id)]
(.remove norms doc-id)
(.add txs [:del c/docs doc-id :int])
(doseq [term-id (doc-id->term-ids lmdb doc-id)]
(let [[term [_ mw sl]] (term-id->info lmdb term-id)
tf (sl/get sl doc-id)]
(.add txs [:put c/terms term
[term-id
(del-max-weight sl doc-id mw tf norm)
(sl/remove sl doc-id)]
:string :term-info]))
(.add txs [:del c/positions [term-id doc-id] :int-int]))
(l/transact-kv lmdb txs))
(u/raise "Document does not exist." {:doc-ref doc-ref})))
(search [this query]
(.search this query {:display :refs :top 10}))
(search [this query {:keys [display ^long top]
:or {display :refs top 10}}]
(let [tokens (->> (en-analyzer query)
(mapv first)
(into-array String))
qterms (->> (hydrate-query lmdb max-doc symspell tokens)
(sort-by :ed)
(sort-by :df)
vec)
n (count qterms)]
(when-not (zero? n)
(let [tids (mapv :id qterms)
sls (mapv :sl qterms)
bms (zipmap tids (mapv #(.-indices ^SparseIntArrayList %)
sls))
sls (zipmap tids sls)
tms (zipmap tids (mapv :tm qterms))
mws (get-ws tids qterms :mw)
wqs (get-ws tids qterms :wq)
mxs (get-mxs tids wqs mws)
result (RoaringBitmap.)
scoring (score-docs n tids sls bms mxs wqs norms result)]
(sequence
(display-xf display lmdb tms)
(persistent!
(reduce
(fn [coll tao]
(let [so-far (count coll)
to-get (- top so-far)]
(if (< 0 to-get)
(let [^PriorityQueue pq (priority-queue to-get)]
(scoring pq tao)
(pouring coll pq result))
(reduced coll))))
(transient [])
(range n 0 -1)))))))))
(defn- init-norms
[lmdb]
(let [norms (IntShortHashMap.)
load (fn [kv]
(let [doc-id (b/read-buffer (l/k kv) :int)
norm (b/read-buffer (l/v kv) :short)]
(.put norms doc-id norm)))]
(l/visit lmdb c/docs load [:all] :int)
norms))
(defn- init-max-doc
[lmdb]
(or (first (l/get-first lmdb c/docs [:all-back] :int :ignore)) 0))
(defn- init-max-term
[lmdb]
(or (first (l/get-first lmdb c/positions [:all-back] :int :ignore)) 0))
(defn- open-dbis
[lmdb]
(assert (not (l/closed-kv? lmdb)) "LMDB env is closed.")
(l/open-dbi lmdb c/terms c/+max-key-size+)
(l/open-dbi lmdb c/docs Integer/BYTES)
(l/open-inverted-list lmdb c/positions (* 2 Integer/BYTES) c/+max-key-size+))
(defn new-engine
([lmdb]
(new-engine lmdb {}))
([lmdb {:keys [fuzzy?]
:or {fuzzy? false}}]
(open-dbis lmdb)
(->SearchEngine lmdb
(init-norms lmdb)
(when fuzzy? (SymSpell. {} {} c/dict-max-edit-distance
c/dict-prefix-length))
(AtomicInteger. (init-max-doc lmdb))
(AtomicInteger. (init-max-term lmdb)))))
(defprotocol IIndexWriter
(write [this doc-ref doc-text] "Write a document")
(commit [this] "Commit writes"))
(deftype IndexWriter [lmdb
^AtomicInteger max-doc
^AtomicInteger max-term
^FastList txs
^UnifiedMap hit-terms]
IIndexWriter
(write [this doc-ref doc-text]
(add-doc-txs lmdb doc-text max-doc txs doc-ref nil max-term
hit-terms)
(when (< 10000000 (.size txs))
(.commit this)))
(commit [this]
(l/transact-kv lmdb txs)
(.clear txs)
(loop [iter (.iterator (.entrySet hit-terms))]
(when (.hasNext iter)
(let [^Map$Entry kv (.next iter)]
(.add txs [:put c/terms (.getKey kv) (.getValue kv)
:string :term-info])
(.remove iter)
(recur iter))))
(l/transact-kv lmdb txs)
(.clear txs)))
(defn search-index-writer
[lmdb]
(open-dbis lmdb)
(->IndexWriter lmdb
(AtomicInteger. (init-max-doc lmdb))
(AtomicInteger. (init-max-term lmdb))
(FastList.)
(UnifiedMap.)))
(comment
(def lmdb (l/open-kv "search-bench/data/wiki-datalevin-all"))
(def engine (time (new-engine lmdb)))
(.size (peek (l/get-value lmdb c/terms "s" :string :term-info))) ; over 3 mil.
(time (search engine "s"))
(time (search engine "french lick resort and casino"))
(time (search engine "rv solar panels"))
(time (search engine "f1"))
(time (search engine "solar system"))
(time (search engine "community service projects for children"))
(time (search engine "libraries michigan"))
(time (search engine "roadrunner email"))
(time (search engine "josephine baker"))
(time (search engine "tel"))
(time (search engine "novi expo center"))
(time (search engine "ocean logos"))
(time (search engine "can i deduct credit card interest on my taxes"))
(time (search engine "what california district am i in"))
(time (search engine "jokes about women turning 40"))
(time (search engine "free inmigration forms"))
(time (search engine "bartender license indiana"))
)