From 6263ba06d9ab3cff525fab28c0bdf72dd663c890 Mon Sep 17 00:00:00 2001 From: megayu Date: Wed, 4 Mar 2026 21:31:56 +0800 Subject: [PATCH] Fix unnecessary highlighted snippets in [[]] block search results (#12421) * fix [[]] block search render unnecessary highlight snippt * match the name of the test with the corresponding fact --- src/main/frontend/components/cmdk/state.cljs | 4 +- src/main/frontend/worker/search.cljs | 47 ++++++------------- .../frontend/components/cmdk/state_test.cljs | 20 ++++---- src/test/frontend/worker/search_test.cljs | 6 +-- 4 files changed, 30 insertions(+), 47 deletions(-) diff --git a/src/main/frontend/components/cmdk/state.cljs b/src/main/frontend/components/cmdk/state.cljs index 45e89c5597..79e9bdb527 100644 --- a/src/main/frontend/components/cmdk/state.cljs +++ b/src/main/frontend/components/cmdk/state.cljs @@ -74,7 +74,7 @@ (let [nodes-base {:limit 100 :dev? dev? :built-in? true - :enable-snippet? false}] + :enable-snippet? true}] (case filter-group :code (assoc nodes-base @@ -84,7 +84,7 @@ :current-page (cond-> {:limit 100 - :enable-snippet? false} + :enable-snippet? true} page-uuid (assoc :page (str page-uuid))) diff --git a/src/main/frontend/worker/search.cljs b/src/main/frontend/worker/search.cljs index 0aa9b0fdfb..201faf56bd 100644 --- a/src/main/frontend/worker/search.cljs +++ b/src/main/frontend/worker/search.cljs @@ -145,18 +145,6 @@ DROP TRIGGER IF EXISTS blocks_au; (def ^:private query-boolean-operators #{"and" "or" "not" "|" "&"}) (def ^:private query-break-chars #{\, \. \; \! \? \uFF0C \u3002 \uFF1B \uFF01 \uFF1F \u3001}) ;; , . ; ! ? , 。 ; ! ? 、 -(defn- snippet-by - [content length] - (str (subs content 0 length) (when (> (count content) max-snippet-length) "..."))) - -(defn- get-snippet-result - [snippet] - (let [;; Cut snippet to limited size chars for non-matched results - snippet (if (string/includes? snippet snippet-highlight-start) - snippet - (snippet-by snippet max-snippet-length))] - snippet)) - (defn- query->terms [q] (->> (string/split (string/trim q) #"\s+") @@ -378,9 +366,9 @@ DROP TRIGGER IF EXISTS blocks_au; [input limit]))) (defn- search-blocks-aux - ([db sql q input page limit enable-snippet?] - (search-blocks-aux db sql q input page limit enable-snippet? false)) - ([db sql q input page limit enable-snippet? use-namespace-last-part?] + ([db sql q input page limit] + (search-blocks-aux db sql q input page limit false)) + ([db sql q input page limit use-namespace-last-part?] (try (let [bind (build-search-bind q input page limit use-namespace-last-part?) result (.exec db (bean/->js @@ -389,9 +377,7 @@ DROP TRIGGER IF EXISTS blocks_au; :rowMode "array"})) blocks (bean/->clj result)] (keep (fn [block] - (let [[id page title _rank snippet] (if enable-snippet? - (update block 4 get-snippet-result) - block)] + (let [[id page title _rank snippet] block] (when title {:id id :keyword-score (fuzzy/score q title) @@ -580,10 +566,12 @@ DROP TRIGGER IF EXISTS blocks_au; (let [block-id (uuid id)] (when-let [block (d/entity @conn [:block/uuid block-id])] (when (include-search-block? conn block code-class option) - (let [display-title (or (block-search-title block) title)] + (let [display-title (if (:enable-snippet? option) + (ensure-highlighted-snippet snippet (or (block-search-title block) title) q) + (or snippet title))] {:db/id (:db/id block) :block/uuid (:block/uuid block) - :block/title (ensure-highlighted-snippet snippet display-title q) + :block/title display-title :block.temp/original-title (:block/title block) :block/page (or (:block/uuid (:block/page block)) @@ -601,7 +589,7 @@ DROP TRIGGER IF EXISTS blocks_au; * :page - the page to specifically search on * :limit - Number of result to limit search results. Defaults to 100 * :search-limit - Number of result to limit sqlite search results. Defaults to nil - * :enable-snippet? - Whether to ask sqlite to generate snippet values. Defaults to true + * :enable-snippet? - Whether to replace title with snippet. Defaults to true * :dev? - Allow all nodes to be seen for development. Defaults to false * :code-only? - Whether to return only code blocks. Defaults to false * :built-in? - Whether to return public built-in nodes for db graphs. Defaults to false" @@ -610,30 +598,25 @@ DROP TRIGGER IF EXISTS blocks_au; :or {enable-snippet? true}}] (m/sp (when-not (string/blank? q) - (let [match-input (get-match-input q) + (let [option (assoc option :enable-snippet? enable-snippet?) + match-input (get-match-input q) page-count (count (d/datoms @conn :avet :block/name)) large-graph? (> page-count 2500) non-match-input (when (<= (count q) 2) (str "%" (string/replace q #"\s+" "%") "%")) limit (or limit 100) limit-p (or search-limit limit) - ;; https://www.sqlite.org/fts5.html#the_highlight_function - ;; the 2nd column in blocks_fts (content) - ;; pfts_2lqh is a key for retrieval - ;; highlight and snippet only works for some matching with high rank - snippet-aux "snippet(blocks_fts, 1, '$pfts_2lqh>$', '$> (search-blocks-aux search-db non-match-sql q non-match-input page limit-p enable-snippet?) + (->> (search-blocks-aux search-db non-match-sql q non-match-input page limit-p) (map (fn [result] (assoc result :keyword-score (fuzzy/score q (:title result))))))) ;; fuzzy is too slow for large graphs diff --git a/src/test/frontend/components/cmdk/state_test.cljs b/src/test/frontend/components/cmdk/state_test.cljs index 805761e6d0..6375840387 100644 --- a/src/test/frontend/components/cmdk/state_test.cljs +++ b/src/test/frontend/components/cmdk/state_test.cljs @@ -86,10 +86,10 @@ (is (= 0 @set-count*)))))) (deftest cmdk-block-search-options-default-and-nodes - (testing "default and nodes options keep snippet disabled and include expected base params" - (is (= {:limit 100 :dev? false :built-in? true :enable-snippet? false} + (testing "default and nodes options keep snippet enabled and include expected base params" + (is (= {:limit 100 :dev? false :built-in? true :enable-snippet? true} (state/cmdk-block-search-options {:dev? false}))) - (is (= {:limit 100 :dev? true :built-in? true :enable-snippet? false} + (is (= {:limit 100 :dev? true :built-in? true :enable-snippet? true} (state/cmdk-block-search-options {:filter-group :nodes :dev? true}))))) (deftest cmdk-block-search-options-code @@ -98,23 +98,23 @@ :search-limit 300 :dev? true :built-in? true - :enable-snippet? false + :enable-snippet? true :code-only? true} (state/cmdk-block-search-options {:filter-group :code :dev? true}))))) (deftest cmdk-block-search-options-current-page-and-move-blocks (testing "current-page adds page and move-blocks adds page-only flag" (is (= {:limit 100 - :enable-snippet? false + :enable-snippet? true :page "00000000-0000-0000-0000-000000000111"} (state/cmdk-block-search-options {:filter-group :current-page - :dev? false - :page-uuid #uuid "00000000-0000-0000-0000-000000000111"}))) + :dev? false + :page-uuid #uuid "00000000-0000-0000-0000-000000000111"}))) (is (= {:limit 100 :dev? true :built-in? true - :enable-snippet? false + :enable-snippet? true :page-only? true} (state/cmdk-block-search-options {:filter-group :nodes - :dev? true - :action :move-blocks}))))) + :dev? true + :action :move-blocks}))))) diff --git a/src/test/frontend/worker/search_test.cljs b/src/test/frontend/worker/search_test.cljs index cacab6c0c3..50fa52d22f 100644 --- a/src/test/frontend/worker/search_test.cljs +++ b/src/test/frontend/worker/search_test.cljs @@ -185,18 +185,18 @@ (deftest search-blocks-aux-bind-count (testing "namespace match SQL keeps bind count aligned" (let [sql "select id, page, title, rank from blocks_fts where title match ? or title match ? order by rank limit ?" - result (#'search/search-blocks-aux (checking-db) sql "a/b" "a/b" nil 10 false true)] + result (#'search/search-blocks-aux (checking-db) sql "a/b" "a/b" nil 10 true)] (is (some? result)) (is (empty? result)))) (testing "namespace non-match SQL without page keeps bind count aligned" (let [sql "select id, page, title, rank from blocks_fts where title like ? limit ?" - result (#'search/search-blocks-aux (checking-db) sql "a/" "%a/%" nil 10 false)] + result (#'search/search-blocks-aux (checking-db) sql "a/" "%a/%" nil 10)] (is (some? result)) (is (empty? result)))) (testing "namespace non-match SQL with page keeps bind count aligned" (let [sql "select id, page, title, rank from blocks_fts where page = ? and title like ? limit ?" - result (#'search/search-blocks-aux (checking-db) sql "a/" "%a/%" "page-1" 10 false)] + result (#'search/search-blocks-aux (checking-db) sql "a/" "%a/%" "page-1" 10)] (is (some? result)) (is (empty? result)))))