Browse Source

Remove page content search

Tienson Qin 2 years ago
parent
commit
678d5c8ee6

+ 2 - 17
src/electron/electron/handler.cljs

@@ -339,15 +339,10 @@
 (defmethod handle :search-blocks [_window [_ repo q opts]]
   (search/search-blocks repo q opts))
 
-(defmethod handle :search-pages [_window [_ repo q opts]]
-  (search/search-pages repo q opts))
-
-(defmethod handle :rebuild-indice [_window [_ repo block-data page-data]]
+(defmethod handle :rebuild-indice [_window [_ repo block-data]]
   (search/truncate-blocks-table! repo)
   ;; unneeded serialization
   (search/upsert-blocks! repo (bean/->js block-data))
-  (search/truncate-pages-table! repo)
-  (search/upsert-pages! repo (bean/->js page-data))
   [])
 
 (defmethod handle :transact-blocks [_window [_ repo data]]
@@ -359,18 +354,8 @@
       ;; unneeded serialization
       (search/upsert-blocks! repo (bean/->js blocks-to-add)))))
 
-(defmethod handle :transact-pages [_window [_ repo data]]
-  (let [{:keys [pages-to-remove-set pages-to-add]} data]
-    ;; Order matters! Same id will delete then upsert sometimes.
-    (when (seq pages-to-remove-set)
-      (search/delete-pages! repo pages-to-remove-set))
-    (when (seq pages-to-add)
-      ;; unneeded serialization
-      (search/upsert-pages! repo (bean/->js pages-to-add)))))
-
 (defmethod handle :truncate-indice [_window [_ repo]]
-  (search/truncate-blocks-table! repo)
-  (search/truncate-pages-table! repo))
+  (search/truncate-blocks-table! repo))
 
 (defmethod handle :remove-db [_window [_ repo]]
   (search/delete-db! repo))

+ 1 - 135
src/electron/electron/search.cljs

@@ -1,5 +1,5 @@
 (ns electron.search
-  "Provides both page level and block level index"
+  "Provides block level index"
   (:require ["path" :as node-path]
             ["fs-extra" :as fs]
             ["better-sqlite3" :as sqlite3]
@@ -68,31 +68,6 @@
       (let [stmt (prepare db trigger db-name)]
         (.run ^object stmt)))))
 
-(defn add-pages-fts-triggers!
-  "Table bindings of pages tables and the pages FTS virtual tables"
-  [db db-name]
-  (let [triggers [;; add
-                  "CREATE TRIGGER IF NOT EXISTS pages_ad AFTER DELETE ON pages
-                  BEGIN
-                      DELETE from pages_fts where rowid = old.id;
-                  END;"
-                  ;; insert
-                  "CREATE TRIGGER IF NOT EXISTS pages_ai AFTER INSERT ON pages
-                  BEGIN
-                      INSERT INTO pages_fts (rowid, uuid, content)
-                      VALUES (new.id, new.uuid, new.content);
-                  END;"
-                  ;; update
-                  "CREATE TRIGGER IF NOT EXISTS pages_au AFTER UPDATE ON pages
-                  BEGIN
-                      DELETE from pages_fts where rowid = old.id;
-                      INSERT INTO pages_fts (rowid, uuid, content)
-                      VALUES (new.id, new.uuid, new.content);
-                  END;"]]
-    (doseq [trigger triggers]
-      (let [stmt (prepare db trigger db-name)]
-        (.run ^object stmt)))))
-
 (defn create-blocks-table!
   [db db-name]
   (let [stmt (prepare db "CREATE TABLE IF NOT EXISTS blocks (
@@ -108,20 +83,6 @@
   (let [stmt (prepare db "CREATE VIRTUAL TABLE IF NOT EXISTS blocks_fts USING fts5(uuid, content, page)" db-name)]
     (.run ^object stmt)))
 
-(defn create-pages-table!
-  [db db-name]
-  (let [stmt (prepare db "CREATE TABLE IF NOT EXISTS pages (
-                        id INTEGER PRIMARY KEY,
-                        uuid TEXT NOT NULL,
-                        content TEXT NOT NULL)"
-                      db-name)]
-    (.run ^object stmt)))
-
-(defn create-pages-fts-table!
-  [db db-name]
-  (let [stmt (prepare db "CREATE VIRTUAL TABLE IF NOT EXISTS pages_fts USING fts5(uuid, content)" db-name)]
-    (.run ^object stmt)))
-
 (defn get-search-dir
   []
   (let [path (.getPath ^object app "userData")]
@@ -151,10 +112,7 @@
     (try (let [db (sqlite3 db-full-path nil)]
            (create-blocks-table! db db-name)
            (create-blocks-fts-table! db db-name)
-           (create-pages-table! db db-name)
-           (create-pages-fts-table! db db-name)
            (add-blocks-fts-triggers! db db-name)
-           (add-pages-fts-triggers! db db-name)
            (swap! databases assoc db-sanitized-name db))
          (catch :default e
            (logger/error (str e ": " db-name))
@@ -190,25 +148,6 @@
         (open-db! repo)
         (get-db repo))))
 
-(defn upsert-pages!
-  [repo pages]
-  (when-let [db (get-or-open-db repo)]
-    ;; TODO: what if a CONFLICT on uuid
-    ;; Should update all values on id conflict
-    (let [insert (prepare db "INSERT INTO pages (id, uuid, content) VALUES (@id, @uuid, @content) ON CONFLICT (id) DO UPDATE SET (uuid, content) = (@uuid, @content)" repo)
-          insert-many (.transaction ^object db
-                                    (fn [pages]
-                                      (doseq [page pages]
-                                        (.run ^object insert page))))]
-      (insert-many pages))))
-
-(defn delete-pages!
-  [repo ids]
-  (when-let [db (get-db repo)]
-    (let [sql (str "DELETE from pages WHERE id IN " (clj-list->sql ids))
-          stmt (prepare db sql repo)]
-      (.run ^object stmt))))
-
 (defn upsert-blocks!
   [repo blocks]
   (when-let [db (get-or-open-db repo)]
@@ -286,71 +225,6 @@
          (take limit)
          (vec))))))
 
-(defn- snippet-by
-  [content length]
-  (str (subs content 0 length) (when (> (count content) 250) "...")))
-
-(defn- search-pages-res-unpack
-  [arr]
-  (let [[rowid uuid content snippet] arr]
-    {:id      rowid
-     :uuid    uuid
-     :content content
-     ;; post processing
-     :snippet (let [;; Remove title from snippet
-                    flag-title " $<pfts_f6ld$ "
-                    flag-title-pos (string/index-of snippet flag-title)
-                    snippet (if flag-title-pos
-                              (subs snippet (+ flag-title-pos (count flag-title)))
-                              snippet)
-                    ;; Cut snippet to 250 chars for non-matched results
-                    flag-highlight "$pfts_2lqh>$ "
-                    snippet (if (string/includes? snippet flag-highlight)
-                              snippet
-                              (snippet-by snippet 250))]
-                snippet)}))
-
-(defn- search-pages-aux
-  [repo database sql input limit]
-  (let [stmt (prepare database sql repo)]
-    (try
-      (doall
-       (map search-pages-res-unpack (-> (.raw ^object stmt)
-                                        (.all input limit)
-                                        (js->clj))))
-      (catch :default e
-        (logger/error "Search page failed: " (str e))))))
-
-(defn search-pages
-  [repo q {:keys [limit]}]
-  (when-let [database (get-db repo)]
-    (when-not (string/blank? q)
-      (let [match-inputs (get-match-inputs q)
-            non-match-input (str "%" (string/replace q #"\s+" "%") "%")
-            limit  (or limit 20)
-            ;; https://www.sqlite.org/fts5.html#the_highlight_function
-            ;; the 2nd column in pages_fts (content)
-            ;; pfts_2lqh is a key for retrieval
-            ;; highlight and snippet only works for some matching with high rank
-            snippet-aux "snippet(pages_fts, 1, ' $pfts_2lqh>$ ', ' $<pfts_2lqh$ ', '...', 32)"
-            select (str "select rowid, uuid, content, " snippet-aux " from pages_fts where ")
-            match-sql (str select
-                           " content match ? order by rank limit ?")
-            non-match-sql (str select
-                               " content like ? limit ?")
-            matched-result (->>
-                            (map
-                             (fn [match-input]
-                               (search-pages-aux repo database match-sql match-input limit))
-                             match-inputs)
-                            (apply concat))]
-        (->>
-         (concat matched-result
-                 (search-pages-aux repo database non-match-sql non-match-input limit))
-         (distinct-by :id)
-         (take limit)
-         (vec))))))
-
 (defn truncate-blocks-table!
   [repo]
   (when-let [database (get-db repo)]
@@ -359,14 +233,6 @@
           stmt (prepare database "delete from blocks_fts;" repo)]
       (.run ^object stmt))))
 
-(defn truncate-pages-table!
-  [repo]
-  (when-let [database (get-db repo)]
-    (let [stmt (prepare database "delete from pages;" repo)
-          _ (.run ^object stmt)
-          stmt (prepare database "delete from pages_fts;" repo)]
-      (.run ^object stmt))))
-
 (defn query
   [repo sql]
   (when-let [database (get-db repo)]

+ 2 - 4
src/main/frontend/handler/search.cljs

@@ -32,14 +32,12 @@
                         (:db/id (db/entity repo [:block/name (util/page-name-sanity-lc page-db-id)]))
                         page-db-id)
            opts (if page-db-id (assoc opts :page (str page-db-id)) opts)]
-       (p/let [blocks (search/block-search repo q opts)
-               pages-content (search/page-content-search repo q opts)]
+       (p/let [blocks (search/block-search repo q opts)]
          (let [result (merge
                        {:blocks blocks
                         :has-more? (= limit (count blocks))}
                        (when-not page-db-id
-                         {:pages-content pages-content
-                          :pages (search/page-search q)
+                         {:pages (search/page-search q)
                           :files (search/file-search q)}))
                search-key (if more? :search/more-result :search/result)]
            (swap! state/state assoc search-key result)

+ 0 - 8
src/main/frontend/search.cljs

@@ -98,14 +98,6 @@
       (when-not (string/blank? q)
         (protocol/query engine q option)))))
 
-(defn page-content-search
-  [repo q option]
-  (when-let [engine (get-engine repo)]
-    (let [q (util/search-normalize q (state/enable-search-remove-accents?))
-          q (if (util/electron?) q (escape-str q))]
-      (when-not (string/blank? q)
-        (protocol/query-page engine q option)))))
-
 (defn- transact-blocks!
   [repo data]
   (when-let [engine (get-engine repo)]

+ 0 - 11
src/main/frontend/search/agency.cljs

@@ -31,13 +31,6 @@
         (protocol/query e q opts))
       (protocol/query e1 q opts)))
 
-  (query-page [_this q opts]
-    (println "D:Search > Query-page contents:" repo q opts)
-    (let [[e1 e2] (get-registered-engines repo)]
-      (doseq [e e2]
-        (protocol/query-page e q opts))
-      (protocol/query-page e1 q opts)))
-
   (rebuild-blocks-indice! [_this]
     (println "D:Search > Initial blocks indice!:" repo)
     (let [[e1 e2] (get-registered-engines repo)]
@@ -49,10 +42,6 @@
     (doseq [e (get-flatten-registered-engines repo)]
       (protocol/transact-blocks! e data)))
 
-  (transact-pages! [_this data]
-    (doseq [e (get-flatten-registered-engines repo)]
-      (protocol/transact-pages! e data)))
-
   (truncate-blocks! [_this]
     (println "D:Search > Truncate blocks!" repo)
     (doseq [e (get-flatten-registered-engines repo)]

+ 0 - 2
src/main/frontend/search/browser.cljs

@@ -35,7 +35,6 @@
   protocol/Engine
   (query [_this q option]
     (p/promise (search-blocks repo q option)))
-  (query-page [_this _q _opt] nil) ;; Page index is not available with fuse.js until sufficient performance benchmarking
   (rebuild-blocks-indice! [_this]
     (let [indice (search-db/make-blocks-indice! repo)]
       (p/promise indice)))
@@ -52,7 +51,6 @@
                  (doseq [block blocks-to-add]
                    (.add indice (bean/->js block)))))
              indice)))
-  (transact-pages! [_this _data] nil) ;; Page index is not available with fuse.js until sufficient performance benchmarking
   (truncate-blocks! [_this]
     (swap! indices assoc-in [repo :blocks] nil))
   (remove-db! [_this]

+ 2 - 12
src/main/frontend/search/node.cljs

@@ -17,22 +17,12 @@
                 {:block/uuid uuid
                  :block/content content
                  :block/page page})) result)))
-  (query-page [_this q opts]
-    (p/let [result (ipc/ipc "search-pages" repo q opts)
-            result (bean/->clj result)]
-      (keep (fn [{:keys [content snippet uuid]}]
-              (when-not (> (count content) (* 10 (state/block-content-max-length repo)))
-                {:block/uuid uuid
-                 :block/snippet snippet})) result)))
   (rebuild-blocks-indice! [_this]
-    (let [blocks-indice (search-db/build-blocks-indice repo)
-          pages-indice  (search-db/build-pages-indice repo)]
-      (ipc/ipc "rebuild-indice" repo blocks-indice pages-indice)))
+    (let [blocks-indice (search-db/build-blocks-indice repo)]
+      (ipc/ipc "rebuild-indice" repo blocks-indice)))
   (transact-blocks! [_this data]
     (ipc/ipc "transact-blocks" repo (bean/->js data)))
   (truncate-blocks! [_this]
     (ipc/ipc "truncate-indice" repo))
-  (transact-pages! [_this data]
-    (ipc/ipc "transact-pages" repo (bean/->js data)))
   (remove-db! [_this]
     (ipc/ipc "remove-db" repo)))

+ 1 - 9
src/main/frontend/search/plugin.cljs

@@ -23,8 +23,6 @@
   (query [_this q opts]
     (call-service! service "search:query" (merge {:q q} opts) true))
 
-  (query-page [_this q opts]
-    (call-service! service "search:queryPage" (merge {:q q} opts) true))
 
   (rebuild-blocks-indice! [_this]
    ;; Not pushing all data for performance temporarily
@@ -37,14 +35,8 @@
                      {:data {:added   blocks-to-add
                              :removed blocks-to-remove-set}})))
 
-  (transact-pages! [_this data]
-    (let [{:keys [pages-to-remove-set pages-to-add]} data]
-      (call-service! service "search:transactpages"
-                     {:data {:added   pages-to-add
-                             :removed pages-to-remove-set}})))
-
   (truncate-blocks! [_this]
     (call-service! service "search:truncateBlocks" {}))
 
   (remove-db! [_this]
-    (call-service! service "search:removeDb" {})))
+    (call-service! service "search:removeDb" {})))

+ 0 - 2
src/main/frontend/search/protocol.cljs

@@ -2,9 +2,7 @@
 
 (defprotocol Engine
   (query [this q option])
-  (query-page [this q option])
   (rebuild-blocks-indice! [this]) ;; TODO: rename to rebuild-indice!
   (transact-blocks! [this data])
   (truncate-blocks! [this]) ;; TODO: rename to truncate-indice!
-  (transact-pages! [this data])
   (remove-db! [this]))