diff --git a/client_search.go b/client_search.go index 434a9481..193f9aae 100644 --- a/client_search.go +++ b/client_search.go @@ -23,7 +23,9 @@ func (c clientSearch) Search(request SearchRequest) (*SearchResponse, error) { request.Limit = 20 } - searchPostRequestParams["q"] = request.Query + if !request.PlaceholderSearch { + searchPostRequestParams["q"] = request.Query + } if request.Filters != "" { searchPostRequestParams["filters"] = request.Filters } diff --git a/client_search_test.go b/client_search_test.go index c1b790e2..b0ffcac7 100644 --- a/client_search_test.go +++ b/client_search_test.go @@ -60,6 +60,36 @@ func TestClientSearch_Search(t *testing.T) { t.Fatalf("Basic search: wrong number of hits, should have 3, got %d\n", resp.NbHits) } + // Test basic empty search + + resp, err = client.Search(indexUID).Search(SearchRequest{ + Query: "", + }) + + if err != nil { + t.Fatal(err) + } + + if len(resp.Hits) != 0 { + fmt.Println(resp) + t.Fatal("Basic search: empty search should return 0 results") + } + + // Test basic placeholder search + + resp, err = client.Search(indexUID).Search(SearchRequest{ + PlaceholderSearch: true, + }) + + if err != nil { + t.Fatal(err) + } + + if len(resp.Hits) != len(booksTest) { + fmt.Println(resp) + t.Fatal("Basic placeholder search: should return placeholder results") + } + // Test basic search with limit resp, err = client.Search(indexUID).Search(SearchRequest{ @@ -81,6 +111,22 @@ func TestClientSearch_Search(t *testing.T) { t.Fatalf("Basic search: should have found %s\n", booksTest[1].Title) } + // Test basic placeholder search with limit + + resp, err = client.Search(indexUID).Search(SearchRequest{ + PlaceholderSearch: true, + Limit: 3, + }) + + if err != nil { + t.Fatal(err) + } + + if len(resp.Hits) != 3 { + fmt.Println(resp) + t.Fatal("Basic placeholder search with limit: should return 3 results") + } + // Test basic search with offset resp, err = client.Search(indexUID).Search(SearchRequest{ diff --git a/types.go b/types.go index 4cb084f7..fffd4102 100644 --- a/types.go +++ b/types.go @@ -126,6 +126,7 @@ type SearchRequest struct { Matches bool FacetsDistribution []string FacetFilters interface{} + PlaceholderSearch bool } // SearchResponse is the response body for search method