feat(search): improve alias handling for snippet display in search results

This commit is contained in:
Mega Yu
2026-05-15 23:04:01 +08:00
parent ffdbe79974
commit ccd6bf2d5f
2 changed files with 59 additions and 3 deletions

View File

@@ -668,9 +668,11 @@ DROP TRIGGER IF EXISTS blocks_au;
(when (include-search-block? conn block code-class option)
(let [alias-source (some-> (first (:block/_alias block))
(select-keys [:block/uuid :block/title]))
alias-match (matched-alias q block)
display-title (if (:enable-snippet? option)
(ensure-highlighted-snippet snippet title q)
alias-match (matched-alias q block)
display-title (if (:enable-snippet? option)
(if (page-or-object? block)
(ensure-highlighted-snippet snippet (:block/title block) q)
(ensure-highlighted-snippet snippet title q))
(if (page-or-object? block)
(:block/title block)
(or snippet title)))

View File

@@ -617,6 +617,60 @@
(is (= "Artificial Intelligence" (:block/title result)))
(is (not (contains? result :alias))))))))
(deftest search-result-snippet-uses-canonical-page-title
(testing "page snippets do not display alias titles from the search index"
(let [page-id #uuid "00000000-0000-0000-0000-000000000242"
page {:db/id 1
:block/uuid page-id
:block/title "Artificial Intelligence"
:block/alias [{:db/id 2
:block/uuid #uuid "00000000-0000-0000-0000-000000000243"
:block/title "ai"}]}]
(with-redefs [d/entity (fn [_db [_attr id]]
(when (= id page-id)
page))
ldb/page? (fn [entity] (= (:db/id entity) (:db/id page)))
ldb/built-in? (constantly false)
ldb/hidden? (constantly false)]
(let [result (#'search/search-result->block-result
(atom :db)
"Artificial"
nil
{:enable-snippet? true}
{:id (str page-id)
:page (str page-id)
:title "Artificial Intelligence ai"
:snippet "$pfts_2lqh>$Artificial$<pfts_2lqh$ Intelligence ai"})]
(is (= "$pfts_2lqh>$Artificial$<pfts_2lqh$ Intelligence" (:block/title result)))
(is (not (contains? result :alias))))))))
(deftest search-result-keeps-valid-page-snippet
(testing "page snippets are preserved when they only contain canonical title text"
(let [page-id #uuid "00000000-0000-0000-0000-000000000244"
page {:db/id 1
:block/uuid page-id
:block/title "Artificial Intelligence"
:block/alias [{:db/id 2
:block/uuid #uuid "00000000-0000-0000-0000-000000000245"
:block/title "ai"}]}]
(with-redefs [d/entity (fn [_db [_attr id]]
(when (= id page-id)
page))
ldb/page? (fn [entity] (= (:db/id entity) (:db/id page)))
ldb/built-in? (constantly false)
ldb/hidden? (constantly false)]
(let [result (#'search/search-result->block-result
(atom :db)
"Artificial"
nil
{:enable-snippet? true}
{:id (str page-id)
:page (str page-id)
:title "Artificial Intelligence ai"
:snippet "$pfts_2lqh>$Artificial$<pfts_2lqh$ Intelligence"})]
(is (= "$pfts_2lqh>$Artificial$<pfts_2lqh$ Intelligence" (:block/title result)))
(is (not (contains? result :alias))))))))
(deftest upsert-blocks-batches-rows-into-single-sql-statement
(let [calls (atom [])
tx #js {:exec (fn [opts]