Browse Source

feat: locator of page content search (backend)

Junyi Du 3 years ago
parent
commit
5838d93927
2 changed files with 12 additions and 6 deletions
  1. 8 2
      src/electron/electron/search.cljs
  2. 4 4
      src/main/frontend/search.cljs

+ 8 - 2
src/electron/electron/search.cljs

@@ -106,7 +106,7 @@
 
 (defn create-pages-fts-table!
   [db]
-  (let [stmt (prepare db "CREATE VIRTUAL TABLE IF NOT EXISTS pages_fts USING fts5(uuid, content, page)")]
+  (let [stmt (prepare db "CREATE VIRTUAL TABLE IF NOT EXISTS pages_fts USING fts5(uuid, content)")]
     (.run ^object stmt)))
 
 (defn get-search-dir
@@ -276,7 +276,13 @@
                           (str "\"" match-input "\""))
             non-match-input (str "%" (string/replace q #"\s+" "%") "%")
             limit  (or limit 20)
-            select "select rowid, uuid, content from pages_fts where "
+            ;; 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
+            highlight-aux "highlight(pages_fts, 1, '$pfts_2lqh>$', '$<pfts_2lqh$')"
+            snippet-aux "snippet(pages_fts, 1, '$pfts_2lqh>$', '$<pfts_2lqh$', '...', 32)"
+            select (str "select rowid, uuid, " highlight-aux ", " snippet-aux " from pages_fts where ")
             match-sql (str select
                            " content match ? order by rank limit ?")
             non-match-sql (str select

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

@@ -10,7 +10,6 @@
             [frontend.search.agency :as search-agency]
             [frontend.search.db :as search-db :refer [indices]]
             [frontend.search.protocol :as protocol]
-            [frontend.modules.datascript-report.core :as ds-report]
             [frontend.state :as state]
             [frontend.util :as util]
             [frontend.util.property :as property]
@@ -262,12 +261,13 @@
                (get-pages-from-datoms-impl pages))))))
 
 (defn- get-indirect-pages
-  "Return the set of pages that will have content updated" 
+  "Return the set of pages that will have content updated"
   [tx-report]
   (let [data   (:tx-data tx-report)
         datoms (filter
                 (fn [datom]
-                  (contains? #{:file/content} (:a datom)))
+                  (and (:added datom)
+                       (contains? #{:file/content} (:a datom))))
                 data)]
     (when (seq datoms)
       (->> datoms
@@ -318,7 +318,7 @@
       (let [indice-pages   (map search-db/page->index updated-pages)
             invalid-set    (->> (map (fn [updated indiced] ;; get id of pages without valid page index
                                        (if indiced nil (:db/id updated)))
-                                     pages-to-add indice-pages)
+                                     updated-pages indice-pages)
                                 (remove nil?)
                                 set)
             pages-to-add   (->> indice-pages