Skip to content

Commit

Permalink
support for limit_multi_searches in multi-search (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen authored Dec 22, 2021
1 parent 306be79 commit d0d1ada
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/typesense/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@

(defn- multi-search-uri
"Returns the full multi-search uri resource path."
[uri common-search-parameters]
(str uri "/multi_search" (util/build-query common-search-parameters)))
[uri common-search-params opt-query-params]
(let [query-parameter-map (merge common-search-params opt-query-params)]
(str uri "/multi_search" (util/build-query query-parameter-map))))

(defn create-collection-req
[{:keys [uri key]} schema]
Expand Down Expand Up @@ -136,8 +137,8 @@
:req {:headers {api-key-header-name key}}})

(defn multi-search-req
[{:keys [uri key]} search-reqs common-search-params]
{:uri (multi-search-uri uri common-search-params)
[{:keys [uri key]} search-reqs common-search-params opt-query-params]
{:uri (multi-search-uri uri common-search-params opt-query-params)
:req {:headers {api-key-header-name key
"Content-Type" "application/json"}
:body (json/write-str search-reqs)}})
Expand Down
4 changes: 2 additions & 2 deletions src/typesense/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@

(defn multi-search
"Search for documents in multiple collections."
[settings search-reqs common-search-params]
[settings search-reqs common-search-params & opt-query-params]
(try+
(let [{:keys [uri req]} (api/multi-search-req settings search-reqs common-search-params)]
(let [{:keys [uri req]} (api/multi-search-req settings search-reqs common-search-params opt-query-params)]
(util/http-response-json->map (http/post uri req)))
(catch [:type :clj-http.client/unexceptional-status] e
(throw (http-ex-data->typesense-ex-info e)))))
Expand Down
18 changes: 17 additions & 1 deletion test/typesense/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,29 @@
:filter_by "price:=[50..120]"}
{:collection "brands"
:q "Nike"}]}
{:query_by "name"})
{:query_by "name"}
nil)
exp {:uri "http://localhost:8108/multi_search?query_by=name"
:req {:headers {"X-TYPESENSE-API-KEY" "key"
"Content-Type" "application/json"}
:body "{\"searches\":[{\"collection\":\"products\",\"q\":\"shoe\",\"filter_by\":\"price:=[50..120]\"},{\"collection\":\"brands\",\"q\":\"Nike\"}]}"}}]
(is (= exp req))))

(deftest multi-search-req-test-optional-parameter
(let [req (sut/multi-search-req settings
{:searches [{:collection "products"
:q "shoe"
:filter_by "price:=[50..120]"}
{:collection "brands"
:q "Nike"}]}
{:query_by "name"}
{:limit_multi_searches 100})
exp {:uri "http://localhost:8108/multi_search?query_by=name&limit_multi_searches=100"
:req {:headers {"X-TYPESENSE-API-KEY" "key"
"Content-Type" "application/json"}
:body "{\"searches\":[{\"collection\":\"products\",\"q\":\"shoe\",\"filter_by\":\"price:=[50..120]\"},{\"collection\":\"brands\",\"q\":\"Nike\"}]}"}}]
(is (= exp req))))

(deftest create-api-key-req-test
(let [req (sut/create-api-key-req settings
{:description "Search only companies key."
Expand Down

0 comments on commit d0d1ada

Please sign in to comment.