Skip to content

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

Enable searching for models through agent api (#76076)

lbrdnk07853910a8ed
diff --git a/src/metabase/agent_api/api.clj b/src/metabase/agent_api/api.clj
index 57d56a8e07b..a895522eea0 100644
--- a/src/metabase/agent_api/api.clj
+++ b/src/metabase/agent_api/api.clj
@@ -103,10 +103,10 @@
;; - Convert keyword enum values (like :table, :metric) to strings for JSON
(mr/def ::search-result-item
- "A table or metric returned from search."
+ "A table, metric, or model returned from search."
[:map {:encode/api #(update-keys % metabot.u/safe->snake_case_en)}
[:id :int]
- [:type [:enum "table" "metric"]]
+ [:type [:enum "table" "metric" "model"]]
[:name :string]
[:display_name {:optional true} [:maybe :string]]
[:description {:optional true} [:maybe :string]]
@@ -117,7 +117,7 @@
[:created_at {:optional true} [:maybe :any]]])
(mr/def ::search-response
- "Search results containing tables and metrics matching the query."
+ "Search results containing tables, metrics, and models matching the query."
[:map {:encode/api #(update-keys % metabot.u/safe->snake_case_en)}
[:data [:sequential ::search-result-item]]
[:total_count :int]])
@@ -150,14 +150,14 @@
:else v))
(api.macros/defendpoint :post "/v1/search" :- ::search-response
- "Search for tables and metrics.
+ "Search for tables, metrics, and models.
Supports both term-based and semantic search queries. Results are ranked using
Reciprocal Rank Fusion when both query types are provided."
{:scope metabot/agent-search
:tool {:name "search"
- :title "Search Tables and Metrics"
- :description (str "Search for tables and metrics in Metabase. "
+ :title "Search Tables, Metrics, and Models"
+ :description (str "Search for tables, metrics, and models in Metabase. "
"Use term_queries for keyword search or semantic_queries for natural language search. "
"Both arguments are arrays of strings, for example term_queries: [\"orders\", \"revenue\"].")
:annotations {:read-only? true}}}
@@ -175,7 +175,7 @@
(let [results (metabot-search/search
{:term-queries (or (coerce-query-list term-queries) [])
:semantic-queries (or (coerce-query-list semantic-queries) [])
- :entity-types ["table" "metric"]
+ :entity-types ["table" "metric" "model"]
:limit (or (request/limit) 50)})]
{:data results
:total_count (count results)}))
diff --git a/test/metabase/agent_api/api_test.clj b/test/metabase/agent_api/api_test.clj
index 4227e841dd6..3fb5f8ca59b 100644
--- a/test/metabase/agent_api/api_test.clj
+++ b/test/metabase/agent_api/api_test.clj
@@ -528,6 +528,19 @@
(mt/user-http-request :rasta :post 200 "agent/v1/search"
{:term_queries ["AgentSearchTestMetric"]}))))))))
+(deftest search-finds-models-test
+ (binding [search.ingestion/*force-sync* true]
+ (search.tu/with-new-search-if-available-otherwise-legacy
+ (mt/with-temp [:model/Card _model {:name "AgentSearchTestModel"
+ :type :model
+ :database_id (mt/id)
+ :dataset_query (orders-count-query)}]
+ (testing "Returns models in search results"
+ (is (=? {:data [{:type "model" :name "AgentSearchTestModel"}]
+ :total_count 1}
+ (mt/user-http-request :rasta :post 200 "agent/v1/search"
+ {:term_queries ["AgentSearchTestModel"]}))))))))
+
;;; ------------------------------------------------ Create Question Tests -------------------------------------------
(deftest create-question-test