Skip to content

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

semantic search: fallback to backup engine when search-string is nil (#62947)

appleby41551f5654cc
diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj
index d0d0873db7e..8f48f913b73 100644
--- a/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj
+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj
@@ -1,6 +1,7 @@
(ns metabase-enterprise.semantic-search.core
"Enterprise implementations of semantic search core functions using defenterprise."
(:require
+ [clojure.string :as str]
[medley.core :as m]
[metabase-enterprise.semantic-search.db.datasource :as semantic.db.datasource]
[metabase-enterprise.semantic-search.env :as semantic.env]
@@ -43,7 +44,10 @@
final-count (count results)
threshold (semantic.settings/semantic-search-min-results-threshold)]
(if (or (>= final-count threshold)
- (zero? raw-count))
+ (and (zero? raw-count)
+ ;; :search-string is nil when using search to populate the list of tables for a given database in
+ ;; the native query editor. Semantic search doesn't support this, so fallback in this case.
+ (not (str/blank? (:search-string search-ctx)))))
results
;; Fallback: semantic search found results but some were filtered out (e.g. due to permission checks), so try to
;; supplement with appdb search.
diff --git a/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.clj b/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.clj
index 1b80213441e..bfe69a4127b 100644
--- a/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.clj
@@ -65,17 +65,19 @@
(is (= "search.engine/appdb" (:engine response))))))))))))
(def ^:private search-context
- {:query "test" :search-engine :search.engine/semantic})
+ {:search-string "test" :search-engine :search.engine/semantic})
(defn- with-search-engine-mocks!
"Sets up search engine mocks for the semantic & appdb backends for testing fallback behavior.
appdb-fn can be a collection of results or a function that takes a context."
[semantic-results appdb-results thunk]
(with-redefs [semantic.pgvector-api/query (fn [_pgvector _index-metadata _search-ctx]
- {:results semantic-results
- ;; Set raw-count to a non-zero value to ensure fallback logic is
- ;; triggered
- :raw-count 1})
+ (if (map? semantic-results)
+ semantic-results
+ {:results semantic-results
+ ;; Set raw-count to a non-zero value to ensure fallback logic is
+ ;; triggered
+ :raw-count 1}))
search.engine/results (fn [ctx]
(case (:search-engine ctx)
:search.engine/appdb (if (fn? appdb-results)
@@ -156,3 +158,18 @@
(let [remaining (rest results)]
(is (= 4 (count remaining)))
(is (every? #(contains? (set appdb-results) %) remaining))))))))))))
+
+(deftest test-semantic-search-fallback-empty-or-nil-search-string
+ (testing "fallback to backup search engine if :search-string is empty or nil"
+ (mt/with-premium-features #{:semantic-search}
+ (let [semantic-results {:results [] :raw-count 0}
+ appdb-results [(make-card-result 1 "appdb-card-1")]
+ search-ctx search-context]
+ (with-search-engine-mocks! semantic-results appdb-results
+ (fn []
+ (testing ":search-string is nil"
+ (let [results (semantic.core/results (assoc search-ctx :search-string nil))]
+ (is (= appdb-results results))))
+ (testing ":search-string is empty"
+ (let [results (semantic.core/results (assoc search-ctx :search-string ""))]
+ (is (= appdb-results results))))))))))