Solr indexer code cleanup

This commit is contained in:
Cadence Ember 2023-12-12 10:35:49 +13:00
parent aea627b27f
commit 27c9680f5b

View file

@ -1,10 +1,13 @@
#lang racket #lang cli
(require racket/function (require (for-syntax racket/base))
(require racket/format
racket/function
racket/future racket/future
racket/match racket/match
racket/path racket/path
racket/promise racket/promise
racket/port racket/port
racket/runtime-path
racket/string racket/string
file/gunzip file/gunzip
db db
@ -16,27 +19,76 @@
"../lib/xexpr-utils.rkt" "../lib/xexpr-utils.rkt"
"../lib/tree-updater.rkt") "../lib/tree-updater.rkt")
(define-syntax (seq stx) (flag (read-from-cache?)
(syntax-case stx () ("-c" "--read-from-cache" "read from last run cache instead of rebuilding documents")
[(_ body ...) (read-from-cache? #t))
#`(for ([op (list (lambda () body) ...)]
[i (in-naturals)]) (define-runtime-path storage-path "../storage/archive")
(define res (op))
(when (>= (response-status-code res) 400) ;; ***************************************************************************************************
(error 'seq "op #~a: status code was ~a: ~v" i (response-status-code res) (response-json res))) ;; Progress bar display
(define taskuid (json-pointer-value "/taskUid" (response-json res))) ;; ***************************************************************************************************
(for/or ([ticks (in-naturals)]
[res2 (in-producer (lambda () (get (format "http://localhost:7700/tasks/~a" taskuid))))]) (struct progress^ (n max title) #:transparent)
(define status (json-pointer-value "/status" (response-json res2)))
(case status (define (make-m-s seconds)
[("enqueued" "processing") (define-values (eta-m eta-s) (quotient/remainder seconds 60))
(sleep 1) (format "~a:~a" eta-m (~a eta-s #:width 2 #:align 'right #:pad-string "0")))
#f]
[("succeeded") (define (make-progress get-p [history-size 20])
(printf "op #~a: ~a (~a ticks)~n" i status ticks) (define update-sleep 1)
#t] (define name-width 30)
[else (define max-width 105)
(error 'seq "op #~a: task status was ~a: ~v" i status res2)])))])) (define history (make-vector history-size 0))
(define history-pointer 0)
(define elapsed 0)
(define (report-progress)
(define p (get-p))
(define history-cycle (vector-ref history history-pointer))
(vector-set! history history-pointer (progress^-n p))
(set! history-pointer (modulo (add1 history-pointer) history-size))
(set! elapsed (add1 elapsed))
(define-values (eta-display diff-per-second)
(cond
[((progress^-n p) . >= . (progress^-max p)) (values (format "~a **" (make-m-s elapsed)) (format "** ~a" (quotient (progress^-max p) (max elapsed 1))))]
[(= history-cycle 0) (values "-:--" "--")]
[else (define diff-per-second (/ (- (progress^-n p) history-cycle) (* history-size update-sleep)))
(define eta-total
(if (diff-per-second . > . 0)
(floor (round (/ (- (progress^-max p) (progress^-n p)) diff-per-second)))
0))
(values (make-m-s eta-total)
(round diff-per-second))]))
(define left (format "~a/~a ~a/s ~a ~a%"
(~a (progress^-n p) #:width (string-length (~a (progress^-max p))) #:align 'right #:pad-string " ")
(progress^-max p)
diff-per-second
eta-display
(floor (* 100 (/ (progress^-n p) (progress^-max p))))))
(define name-display (~a (progress^-title p) #:max-width name-width #:limit-marker "..."))
(define remaining-space (- max-width name-width (string-length left) 2))
(define bar-width
(floor (* (sub1 remaining-space)
(/ (progress^-n p) (progress^-max p)))))
(define bar (string-append (make-string bar-width #\=)
">"
(make-string (- remaining-space bar-width) #\ )))
(printf "\e[2K\r~a~a~a" left bar name-display)
(flush-output))
(define (report-progress-loop)
(sleep update-sleep)
(report-progress)
(report-progress-loop))
(define t (thread report-progress-loop))
(define (quit)
(kill-thread t)
(report-progress)
(displayln ""))
quit)
;; ***************************************************************************************************
;; Page text extractor
;; ***************************************************************************************************
(define (class-has? attributes substrs) (define (class-has? attributes substrs)
(define cl (or (get-attribute 'class attributes) "")) (define cl (or (get-attribute 'class attributes) ""))
@ -49,9 +101,6 @@
[#t [#t
(list element-type attributes children)])) (list element-type attributes children)]))
(define slc (sqlite3-connect #:database "../storage/fts-separate.db"))
(sqlite3-load-extension slc "fts5")
(define (writer tables-mode? page) (define (writer tables-mode? page)
(define (writer-inner page) (define (writer-inner page)
(for ([bit page]) (for ([bit page])
@ -72,12 +121,9 @@
(define shrink-text (regexp-replace* #px"([ \t]*\r?\n+)+" text "\n")) (define shrink-text (regexp-replace* #px"([ \t]*\r?\n+)+" text "\n"))
shrink-text) shrink-text)
(define wikiname "bloons")
(define tablename (format "page_~a" wikiname))
(define ((extract f)) ; f - filename (define ((extract f)) ; f - filename
(with-handlers (with-handlers
([exn:fail? (λ (err) (println f) (raise err))]) ([exn:fail? (λ (err) (printf "extract: ~a: ~v~n" f err))])
(define j (define j
(case (path-get-extension f) (case (path-get-extension f)
[(#".json") [(#".json")
@ -93,110 +139,62 @@
(define page (update-tree updater (html->xexp page-html))) (define page (update-tree updater (html->xexp page-html)))
(define body (write-and-post-process #f page)) (define body (write-and-post-process #f page))
(define table (write-and-post-process #t page)) (define table (write-and-post-process #t page))
(values title body table pageid))) (list title body table pageid)))
;; ***************************************************************************************************
;; Program, loop, Solr APIs
;; ***************************************************************************************************
(program
(start [wikiname "wikiname to download"])
(define results (define results
(for/list ([f (directory-list (format "../storage/archive/~a" wikiname) #:build? #t)] (for/list ([f (directory-list (build-path storage-path wikiname) #:build? #t)]
#:when (member (path-get-extension f) '(#".json" #".gz"))) #:when (member (path-get-extension f) '(#".gz")))
(extract f))) (extract f)))
;; ***************************************************************************************************
;; TESTING WRITER
;; ***************************************************************************************************
#;(for/first ([fut results]
[i (in-naturals 1)]
#:when (i . >= . 4859))
(define-values (title body table pageid) (fut))
(println title)
(println body)
(println table))
(println "inserting...")
;; ***************************************************************************************************
;; SQLite FTS5
;; ***************************************************************************************************
#;(begin
(query-exec slc "begin transaction")
#;(query-exec slc (format "create virtual table \"~a\" using fts5 (title, body, tokenize='porter unicode61')" wikiname))
(time
(for ([fut results]
[i (in-naturals 1)])
(display "-")
(when (and (> i 0) (= (modulo i 100) 0))
(println i))
(define-values (title shrink-text) (fut))
(query-exec slc (format "insert into \"~a\" (title, body) values (?, ?)" tablename) title shrink-text)))
(println "running optimize...")
(query-exec slc (format "insert into \"~a\" (\"~a\") values ('optimize')" tablename tablename))
(println "committing...")
(query-exec slc "commit"))
;; ***************************************************************************************************
;; Solr
;; ***************************************************************************************************
(begin
(define data (define data
(cond (cond
#;[(file-exists? "cache.rkt") [(and (read-from-cache?) (file-exists? "cache.rkt"))
(println "reading in...") (displayln "Reading in...")
(with-input-from-file "cache.rkt" (λ () (read)))] (with-input-from-file "cache.rkt" (λ () (read)))]
[else [else
(define x (box (progress^ 0 1 "...")))
(define quit (make-progress (λ () (unbox x))))
(define data (define data
(for/list ([fut results] (for/list ([fut results]
[i (in-naturals 1)]) [i (in-naturals 1)]
(display "-") #:do [(define page (fut))]
(when (and (> i 0) (= (modulo i 100) 0)) #:when (not (void? page)))
(println i)) (match-define (list title body table pageid) page)
(define-values (title body table pageid) (fut))
(define len (string-length body)) (define len (string-length body))
(set-box! x (progress^ i (length results) title))
`#hasheq((id . ,(number->string pageid)) `#hasheq((id . ,(number->string pageid))
(title . ,title) (title . ,title)
(body . ,body) (body . ,body)
(table . ,table) (table . ,table)
(len . ,len)))) (len . ,len))))
(quit)
(println "writing out...") (display "Writing out... ")
(flush-output)
(with-output-to-file "cache.rkt" (λ () (write data)) #:exists 'truncate/replace) (with-output-to-file "cache.rkt" (λ () (write data)) #:exists 'truncate/replace)
data])) data]))
(println "posting...") (display "Converting... ")
(flush-output)
(define ser (jsexpr->bytes data))
(define ser-port (open-input-bytes ser))
(define quit (make-progress (λ () (progress^ (ceiling (/ (file-position ser-port) 64 1024))
(ceiling (/ (bytes-length ser) 64 1024))
"Posting..."))
2))
(define res (define res
(post (format "http://localhost:8983/solr/~a/update?commit=true" wikiname) (post (format "http://localhost:8983/solr/~a/update?commit=true" wikiname)
#:json data))) #:data ser-port
;; ***************************************************************************************************
;; Meilisearch
;; ***************************************************************************************************
#;(begin
(seq
(put (format "http://localhost:7700/indexes/~a/settings/searchable-attributes" wikiname)
#:json '("title" "body"))
(put (format "http://localhost:7700/indexes/~a/settings/ranking-rules" wikiname)
#:json '("words" "typo" #;"proximity" "attribute" "sort" "exactness" #;"len:desc"))
(call-with-input-file "stop-words.json"
(λ (in)
(put (format "http://localhost:7700/indexes/~a/settings/stop-words" wikiname)
#:headers '#hasheq((Content-Type . "application/json")) #:headers '#hasheq((Content-Type . "application/json"))
#:data in)))) #:timeouts (make-timeout-config #:lease 5 #:connect 5 #:request 300)))
(define data (quit)
(for/list ([fut results] (displayln (response-status-line res)))
[i (in-naturals 1)])
(display "-")
(when (and (> i 0) (= (modulo i 100) 0))
(println i))
(define-values (title body pageid) (fut))
(define len (string-length body))
`#hasheq((id . ,pageid)
(title . ,title)
(body . ,body)
(len . ,len))))
(define res
(post (format "http://localhost:7700/indexes/~a/documents" wikiname)
#:json data))
(seq res)
(println (response-json res)))
(disconnect slc) (run start)