Skip to content

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

Ensure results are realized when falling back to in-place results in semantic search (#63254)

applebye4f11aba6621
diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj
index 790ecc8c0ff..0fe75e04aa2 100644
--- a/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj
+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj
@@ -12,7 +12,8 @@
[metabase.analytics.core :as analytics]
[metabase.premium-features.core :refer [defenterprise]]
[metabase.search.engine :as search.engine]
- [metabase.util.log :as log]))
+ [metabase.util.log :as log]
+ [toucan2.realize :as t2.realize]))
(defn- fallback-engine
"Find the highest priority search engine available for fallback."
@@ -58,7 +59,12 @@
(analytics/inc! :metabase-search/semantic-fallback-triggered {:fallback-engine fallback})
(analytics/observe! :metabase-search/semantic-results-before-fallback final-count)
(let [fallback-results (try
- (search.engine/results (assoc search-ctx :search-engine fallback))
+ (cond->> (search.engine/results (assoc search-ctx :search-engine fallback))
+ ;; The in-place engine returns a reducible (but not seqable) result that needs to
+ ;; be realized before we concat and dedup with the semantic engine results.
+ (= :search.engine/in-place fallback)
+ (into [] (comp (map t2.realize/realize)
+ (take (- (semantic.settings/semantic-search-results-limit) final-count)))))
(catch Throwable t
(log/warn t "Semantic search fallback errored, ignoring")
[]))
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 eeab9afcd6a..2d0a270335f 100644
--- a/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.clj
@@ -56,7 +56,9 @@
in-place.legacy/results
(fn [_]
(reset! legacy-called? true)
- [{:id 2 :name "legacy-result" :model "card" :collection_id 1 :score 0.7}])]
+ (reify clojure.lang.IReduceInit
+ (reduce [_this rf init]
+ (reduce rf init [{:id 2 :name "legacy-result" :model "card" :collection_id 1 :score 0.7}]))))]
(testing "API defaults to semantic engine when configured"
(let [response (mt/user-http-request :crowberto :get 200 "search" :q "test")]
(is @semantic-called? "Semantic search should be called by default")
@@ -97,11 +99,15 @@
{:result (dissoc result :score)
:score (:score result)})
search.engine/results (fn [ctx]
- (case (:search-engine ctx)
- :search.engine/semantic (semantic.core/results ctx)
- (if (fn? fallback-results)
- (fallback-results ctx)
- fallback-results)))]
+ (let [fallback-results* #(if (fn? fallback-results)
+ (fallback-results ctx)
+ fallback-results)]
+ (case (:search-engine ctx)
+ :search.engine/semantic (semantic.core/results ctx)
+ :search.engine/in-place (reify clojure.lang.IReduceInit
+ (reduce [_this rf init]
+ (reduce rf init (fallback-results*))))
+ (fallback-results*))))]
(thunk)))
(defn- make-card-result