From 2bdb84f35e2b340baee342b8414e592d2f312e1a Mon Sep 17 00:00:00 2001 From: Rune Nielsen Date: Fri, 18 Mar 2022 21:10:14 +0100 Subject: [PATCH] fixes issue with search returning a vector instead of map (#12) --- README.md | 48 +++++++++++++++++- src/typesense/client.clj | 2 +- test/typesense/client_test.clj | 90 +++++++++++++++++++++++++++++++--- 3 files changed, 131 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d1b9c77..2391cd3 100644 --- a/README.md +++ b/README.md @@ -323,7 +323,20 @@ Search for documents in a collection. You can find all the query arguments [here ```clojure (search settings "companies" {:q "Stark" - :query_by "test_name"}) + :query_by "company_name"}) + +;; Example success response => +{:facet_counts [] + :found 0 + :hits [] + :out_of 1 + :page 1 + :request_params + {:collection_name "companies_documents_test" + :per_page 10 + :q "Stark"} + :search_cutoff false + :search_time_ms 0} ``` ## Multi search @@ -338,6 +351,39 @@ You can send multiple search requests in a single HTTP request, using the Multi- {:collection "brands" :q "Nike"}]} {:query_by "name"}) + +;; Example success response => +{:results + [{:facet_counts [] + :found 1 + :hits + [{:document {:id "1" :name "shoe" :price 75} + :highlights + [{:field "name" + :matched_tokens ["shoe"] + :snippet "shoe"}] + :text_match 33514497}] + :out_of 1 + :page 1 + :request_params + {:collection_name "products" :per_page 10 :q "shoe"} + :search_cutoff false + :search_time_ms 0} + {:facet_counts [] + :found 1 + :hits + [{:document {:id "1" :name "Nike"} + :highlights + [{:field "name" + :matched_tokens ["Nike"] + :snippet "Nike"}] + :text_match 33514497}] + :out_of 1 + :page 1 + :request_params + {:collection_name "brands" :per_page 10 :q "Nike"} + :search_cutoff false + :search_time_ms 0}]} ``` ## Api keys diff --git a/src/typesense/client.clj b/src/typesense/client.clj index 801be4e..dd65dd3 100644 --- a/src/typesense/client.clj +++ b/src/typesense/client.clj @@ -155,7 +155,7 @@ [settings collection-name options] (try+ (let [{:keys [uri req]} (api/search-req settings collection-name options)] - (util/http-response-jsonline->maps (http/get uri req))) + (util/http-response-json->map (http/get uri req))) (catch [:type :clj-http.client/unexceptional-status] e (throw (http-ex-data->typesense-ex-info e))))) diff --git a/test/typesense/client_test.clj b/test/typesense/client_test.clj index 2075c01..8f5c712 100644 --- a/test/typesense/client_test.clj +++ b/test/typesense/client_test.clj @@ -287,6 +287,82 @@ {:filter_by "num_employees:<=100"})] (is (= exp res)))) + (testing "Search" + (let [exp {:facet_counts [] + :found 0 + :hits [] + :out_of 1 + :page 1 + :request_params + {:collection_name "companies_documents_test" + :per_page 10 + :q "Stark"} + :search_cutoff false + :search_time_ms 0} + res (sut/search settings + "companies_documents_test" + {:q "Stark" + :query_by "company_name"})] + (is (= res exp)))) + + ;; Creating test setup for multi search + (let [schema {:name "products_multi_search_test" + :fields [{:name "name" + :type "string"} + {:name "price" + :type "int32"}]}] + (sut/create-collection! settings schema) + (sut/create-document! settings "products_multi_search_test" {:id "1" + :name "shoe" + :price 75})) + + (let [schema {:name "brands_multi_search_test" + :fields [{:name "name" + :type "string"}]}] + (sut/create-collection! settings schema) + (sut/create-document! settings "brands_multi_search_test" {:id "1" :name "Nike"})) + + (testing "Multi search" + (let [exp {:results + [{:facet_counts [] + :found 1 + :hits + [{:document {:id "1" :name "shoe" :price 75} + :highlights + [{:field "name" + :matched_tokens ["shoe"] + :snippet "shoe"}] + :text_match 33514497}] + :out_of 1 + :page 1 + :request_params + {:collection_name "products_multi_search_test" :per_page 10 :q "shoe"} + :search_cutoff false + :search_time_ms 0} + {:facet_counts [] + :found 1 + :hits + [{:document {:id "1" :name "Nike"} + :highlights + [{:field "name" + :matched_tokens ["Nike"] + :snippet "Nike"}] + :text_match 33514497}] + :out_of 1 + :page 1 + :request_params + {:collection_name "brands_multi_search_test" :per_page 10 :q "Nike"} + :search_cutoff false + :search_time_ms 0}]} + res (sut/multi-search settings + {:searches [{:collection "products_multi_search_test" + :q "shoe" + :filter_by "price:=[50..120]"} + {:collection "brands_multi_search_test" + :q "Nike"}]} + {:query_by "name"})] + (is (= exp res)))) + ;; Initialize test collection for curation. (let [schema {:name "companies_curation_test" :fields [{:name "company_name" @@ -374,7 +450,7 @@ (is (= exp res)))) ;; Initialize test collection for synonyms. - (let [schema {:name "products" + (let [schema {:name "products_synonyms_test" :fields [{:name "company_name" :type "string"} {:name "num_employees" @@ -388,24 +464,24 @@ (testing "Upsert synonym" (let [exp {:id "coat-synonyms" :synonyms ["blazer" "coat" "jacket"]} res (sut/upsert-synonym! settings - "products" + "products_synonyms_test" "coat-synonyms" {:synonyms ["blazer" "coat" "jacket"]})] (is (= exp res)))) (testing "Retrieve synonym" - (let [exp {:id "coat-synonyms", :root "", :synonyms ["blazer" "coat" "jacket"]} - res (sut/retrieve-synonym settings "products" "coat-synonyms")] + (let [exp {:id "coat-synonyms" :root "" :synonyms ["blazer" "coat" "jacket"]} + res (sut/retrieve-synonym settings "products_synonyms_test" "coat-synonyms")] (is (= exp res)))) (testing "List synonyms" - (let [exp {:synonyms [{:id "coat-synonyms", :root "", :synonyms ["blazer" "coat" "jacket"]}]} - res (sut/list-synonyms settings "products")] + (let [exp {:synonyms [{:id "coat-synonyms" :root "" :synonyms ["blazer" "coat" "jacket"]}]} + res (sut/list-synonyms settings "products_synonyms_test")] (is (= exp res)))) (testing "Delete synonym" (let [exp {:id "coat-synonyms"} - res (sut/delete-synonym! settings "products" "coat-synonyms")] + res (sut/delete-synonym! settings "products_synonyms_test" "coat-synonyms")] (is (= res exp))))) (deftest client-api-key-tests