diff --git a/src/metabase/api/search.clj b/src/metabase/api/search.clj index e18e35640ef88b30fba999b65e7d9b42644c58ff..4c368d7fcb0ddac6372418ec404345e1eb4ae5bc 100644 --- a/src/metabase/api/search.clj +++ b/src/metabase/api/search.clj @@ -7,7 +7,7 @@ [honeysql.helpers :as h] [metabase.api.common :as api] [metabase.db :as mdb] - [metabase.models.bookmark :refer [CardBookmark DashboardBookmark]] + [metabase.models.bookmark :refer [CardBookmark CollectionBookmark DashboardBookmark]] [metabase.models.collection :as coll :refer [Collection]] [metabase.models.interface :as mi] [metabase.models.metric :refer [Metric]] @@ -272,6 +272,10 @@ (s/defmethod search-query-for-model "collection" [model search-ctx :- SearchContext] (-> (base-query-for-model model search-ctx) + (h/left-join [CollectionBookmark :bookmark] + [:and + [:= :bookmark.collection_id :collection.id] + [:= :bookmark.user_id api/*current-user-id*]]) (add-collection-join-and-where-clauses :collection.id search-ctx))) (s/defmethod search-query-for-model "database" @@ -283,7 +287,7 @@ (-> (base-query-for-model model search-ctx) (h/left-join [DashboardBookmark :bookmark] [:and - [:= :dashboard.id :bookmark.dashboard_id] + [:= :bookmark.dashboard_id :dashboard.id ] [:= :bookmark.user_id api/*current-user-id*]]) (add-collection-join-and-where-clauses :dashboard.collection_id search-ctx))) diff --git a/src/metabase/search/config.clj b/src/metabase/search/config.clj index 257ec2a1e16754beae930f16a56d4cb6aa1c90dd..6be7a6af216c7ccb8e6a73189ac570279a25836a 100644 --- a/src/metabase/search/config.clj +++ b/src/metabase/search/config.clj @@ -96,7 +96,7 @@ [:id :name :description :archived :updated_at]) (def ^:private bookmark-col - "Case statement to return boolean values of `:bookmark` for Card and Dashboard." + "Case statement to return boolean values of `:bookmark` for Card, Collection and Dashboard." [(hsql/call :case [:not= :bookmark.id nil] true :else false) :bookmark]) (def ^:private dashboardcard-count-col @@ -153,8 +153,9 @@ (defmethod columns-for-model "collection" [_] - (conj (remove #{:updated_at} default-columns) [:id :collection_id] [:name :collection_name] - [:authority_level :collection_authority_level])) + (conj (remove #{:updated_at} default-columns) [:collection.id :collection_id] [:name :collection_name] + [:authority_level :collection_authority_level] + bookmark-col)) (defmethod columns-for-model "segment" [_] diff --git a/src/metabase/search/scoring.clj b/src/metabase/search/scoring.clj index 85d7cddde0f2b8551c5e9c6c151a6d3eb1d21e89..3bfe2637394f11ffc683d134f726e8953ebdcd75 100644 --- a/src/metabase/search/scoring.clj +++ b/src/metabase/search/scoring.clj @@ -183,6 +183,13 @@ 1 0))) +(defn- bookmarked-score + [{:keys [model bookmark]}] + (when (#{"card" "collection" "dashboard"} model) + (if bookmark + 1 + 0))) + (defn- dashboard-count-score [{:keys [model dashboardcard_count]}] (when (= model "card") @@ -237,6 +244,9 @@ [{:weight 2 :score (pinned-score result) :name "pinned"} + {:weight 2 + :score (bookmarked-score result) + :name "bookmarked"} {:weight 3/2 :score (recency-score result) :name "recency"} diff --git a/test/metabase/api/search_test.clj b/test/metabase/api/search_test.clj index 69060e1e1193cee1bea4a3544bf0c986ec8c3a8b..c25a98e88641cc69efbdca929e982ff2f864c79a 100644 --- a/test/metabase/api/search_test.clj +++ b/test/metabase/api/search_test.clj @@ -59,6 +59,7 @@ (apply array-map kvs))) (def ^:private test-collection (make-result "collection test collection" + :bookmark false :model "collection" :collection {:id true, :name true :authority_level nil} :updated_at false)) diff --git a/test/metabase/search/scoring_test.clj b/test/metabase/search/scoring_test.clj index 7d715d2d7286e3787b95dec80fc6c2d33664be31..e231a0047cac4783e99b3efa95701c5927cab4d0 100644 --- a/test/metabase/search/scoring_test.clj +++ b/test/metabase/search/scoring_test.clj @@ -265,6 +265,21 @@ (map :result) (map :name)))))) +(deftest bookmarked-test + (let [search-string "my card" + labeled-results {:a {:name "my card a" :model "dashboard"} + :b {:name "my card b" :model "dashboard" :bookmark true :collection_position 1} + :c {:name "my card c" :model "dashboard" :bookmark true}} + {:keys [a b c]} labeled-results] + (is (= (map :name [b c a]) + (->> labeled-results + vals + (map (partial search/score-and-result search-string)) + (sort-by :score) + reverse + (map :result) + (map :name)))))) + (deftest score-and-result-test (testing "If all scores are 0, does not divide by zero" (let [scorer (reify search/ResultScore