Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Tolerate legacy search weight overrides (#75614)

Chris Truter75ea90c02946
diff --git a/src/metabase/search/config.clj b/src/metabase/search/config.clj
index 8d84a86611e..bd2eadb2882 100644
--- a/src/metabase/search/config.clj
+++ b/src/metabase/search/config.clj
@@ -231,21 +231,27 @@
(defn- normalize-override-keys
"Re-key persisted weight overrides ([[search.settings/experimental-search-weight-overrides]]) by
- [[normalized-context]], so an override saved under a context that has since been collapsed (e.g.
- `:command-palette` -> `:global`) is still applied.
- When a raw alias and its normalized context both carry overrides, the normalized one wins -- it's
- what the weights API writes today."
+ [[normalized-context]], merging overrides whose alias has since collapsed onto the surviving context.
+ Legacy flat `{scorer weight}` overrides fold into the `:default` base, losing to an explicit `:default`."
[overrides]
- (reduce-kv (fn [acc context weight-overrides]
- (let [normalized (normalized-context context)]
- (update acc normalized
- (if (= context normalized)
- #(merge % weight-overrides)
- #(merge weight-overrides %)))))
- {}
- ;; sorted so the winner is deterministic when several aliases collapse to one normalized
- ;; context (the normalized key still wins; among aliases the lowest-sorted one does)
- (into (sorted-map) overrides)))
+ (let [;; legacy overrides (pre-#50338) were a flat {scorer weight} map with no context layer, so they
+ ;; applied to every context; these are the entries whose value is a bare weight, not a per-context map
+ legacy (into {} (remove (comp map? val)) overrides)
+ nested (reduce-kv (fn [acc context weight-overrides]
+ (let [normalized (normalized-context context)]
+ (update acc normalized
+ ;; when an alias and its normalized context both carry overrides the
+ ;; normalized one wins -- it's what the weights API writes today
+ (if (= context normalized)
+ #(merge % weight-overrides)
+ #(merge weight-overrides %)))))
+ {}
+ ;; sorted so the winner is deterministic when several aliases collapse to one
+ ;; normalized context (the normalized key still wins; among aliases the lowest-sorted)
+ (into (sorted-map) (filter (comp map? val)) overrides))]
+ (cond-> nested
+ ;; :default feeds every context, so folding the flat weights there preserves their global reach
+ (seq legacy) (update :default #(merge legacy %)))))
;; This gets called *a lot* during a search request, so we'll almost certainly need to optimize it. Maybe just TTL.
(defn weights
diff --git a/test/metabase/search/config_test.clj b/test/metabase/search/config_test.clj
index 1a825d4d3f1..de90f065e79 100644
--- a/test/metabase/search/config_test.clj
+++ b/test/metabase/search/config_test.clj
@@ -51,7 +51,12 @@
(is (= {:global {:text 5 :exact 9}} (normalize {:type-filter {:text 5} :global {:exact 9}}))))
(testing "the :default base and un-remapped contexts pass through untouched"
(is (= {:default {:text 7} :entity-picker {:exact 3}}
- (normalize {:default {:text 7} :entity-picker {:exact 3}})))))
+ (normalize {:default {:text 7} :entity-picker {:exact 3}}))))
+ (testing "legacy flat overrides (bare weights, not per-context maps) fold into the :default base"
+ (is (= {:default {:text 7.0 :exact 3.0}} (normalize {:text 7.0 :exact 3.0})))
+ (is (= {:global {:exact 1} :default {:text 7.0}} (normalize {:command-palette {:exact 1} :text 7.0}))))
+ (testing "an explicit :default context override wins over a legacy flat weight for the same scorer"
+ (is (= {:default {:text 5.0 :exact 9}} (normalize {:text 5.0 :exact 1.0 :default {:exact 9}})))))
(testing "several aliases collapsing to one normalized context resolve deterministically, regardless of
input order (the lowest-sorted alias wins among aliases)"
(let [normalize #'search.config/normalize-override-keys]