Kaynağa Gözat

fix: block search in page filter

Weihua Lu 4 yıl önce
ebeveyn
işleme
8efa39d65e

+ 1 - 0
src/main/frontend/db/model.cljs

@@ -1165,6 +1165,7 @@
                 (let [e (db-utils/entity [:block/uuid id])]
                   {:db/id (:db/id e)
                    :block/uuid id
+                   :block/page (:db/id (:block/page e))
                    :block/content (:block/content e)
                    :block/format (:block/format e)}))))))
 

+ 3 - 5
src/main/frontend/handler/search.cljs

@@ -18,12 +18,10 @@
             :as opts}]
    (let [page-db-id (if (string? page-db-id)
                       (:db/id (db/entity repo [:block/name (string/lower-case page-db-id)]))
-                      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)]
-      (let [blocks (if page-db-id
-                     (filter (fn [block] (= (get-in block [:block/page :db/id]) page-db-id)) blocks)
-                     blocks)
-            result (merge
+      (let [result (merge
                     {:blocks blocks
                      :has-more? (= limit (count blocks))}
                     (when-not page-db-id

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

@@ -12,20 +12,25 @@
 ;; fuse.js
 
 (defn search-blocks
-  [repo q {:keys [limit]
+  [repo q {:keys [limit page]
             :or {limit 20}
             :as option}]
   (let [indice (or (get-in @indices [repo :blocks])
                    (search-db/make-blocks-indice! repo))
-        result (.search indice q (clj->js {:limit limit}))
+        result
+        (if page
+          (.search indice
+                   (clj->js {:$and [{"page" page} {"content" q}]})
+                   (clj->js {:limit limit}))
+          (.search indice q (clj->js {:limit limit})))
         result (bean/->clj result)]
     (->>
      (map
        (fn [{:keys [item matches] :as block}]
-         (let [{:keys [content uuid]} item]
+         (let [{:keys [content uuid page]} item]
            {:block/uuid uuid
             :block/content content
-            :block/page (:block/page (db/entity [:block/uuid (medley/uuid (str uuid))]))
+            :block/page page
             :search/matches matches}))
        result)
      (remove nil?))))

+ 3 - 2
src/main/frontend/search/db.cljs

@@ -14,11 +14,12 @@
   (nil? (get @indices repo)))
 
 (defn block->index
-  [{:block/keys [uuid content format] :as block}]
+  [{:block/keys [uuid content format page] :as block}]
   (when-let [result (->> (text/remove-level-spaces content format)
                          (text/remove-id-property! format))]
     {:id (:db/id block)
      :uuid (str uuid)
+     :page page
      :content result}))
 
 (defn build-blocks-indice
@@ -32,7 +33,7 @@
   [repo]
   (let [blocks (build-blocks-indice repo)
         indice (fuse. blocks
-                      (clj->js {:keys ["uuid" "content"]
+                      (clj->js {:keys ["uuid" "content" "page"]
                                 :shouldSort true
                                 :tokenize true
                                 :minMatchCharLength 1