From fedae2cfd6f809daff5c5c328a06ca94ed300171 Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Tue, 9 Jun 2020 01:07:53 +0200 Subject: [PATCH 01/13] Implement faceting settings routes and facetsDistribution --- apis.go | 6 +++++ client_search.go | 4 ++++ client_settings.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++ types.go | 13 ++++++----- 4 files changed, 74 insertions(+), 5 deletions(-) diff --git a/apis.go b/apis.go index b3b83f3c..8dcce6da 100644 --- a/apis.go +++ b/apis.go @@ -162,6 +162,12 @@ type APISettings interface { GetAcceptNewFields() (*bool, error) UpdateAcceptNewFields(bool) (*AsyncUpdateID, error) + + GetAttributesForFaceting() (*[]string, error) + + UpdateAttributesForFaceting([]string) (*AsyncUpdateID, error) + + ResetAttributesForFaceting() (*AsyncUpdateID, error) } // APIStats retrieve statistic over all indexes or a specific index id. diff --git a/client_search.go b/client_search.go index b37d6a3b..fb7b05a7 100644 --- a/client_search.go +++ b/client_search.go @@ -1,6 +1,7 @@ package meilisearch import ( + "fmt" "net/http" "net/url" "strconv" @@ -51,6 +52,9 @@ func (c clientSearch) Search(request SearchRequest) (*SearchResponse, error) { if request.Matches { values.Add("matches", strconv.FormatBool(request.Matches)) } + if len(request.FacetsDistribution) != 0 { + values.Add("facetsDistribution", fmt.Sprintf("[\"%s\"]", strings.Join(request.FacetsDistribution, "\",\""))) + } req := internalRequest{ endpoint: "/indexes/" + c.indexUID + "/search?" + values.Encode(), diff --git a/client_settings.go b/client_settings.go index f8af2fb8..d54e2e61 100644 --- a/client_settings.go +++ b/client_settings.go @@ -451,3 +451,59 @@ func (c clientSettings) UpdateAcceptNewFields(request bool) (resp *AsyncUpdateID return resp, nil } + +func (c clientSettings) GetAttributesForFaceting() (resp *[]string, err error) { + resp = &[]string{} + req := internalRequest{ + endpoint: "/indexes/" + c.indexUID + "/settings/attributes-for-faceting", + method: http.MethodGet, + withRequest: nil, + withResponse: resp, + acceptedStatusCodes: []int{http.StatusOK}, + functionName: "GetAttributesForFaceting", + apiName: "Settings", + } + + if err := c.client.executeRequest(req); err != nil { + return nil, err + } + return resp, nil +} + +func (c clientSettings) UpdateAttributesForFaceting(request []string) (resp *AsyncUpdateID, err error) { + resp = &AsyncUpdateID{} + req := internalRequest{ + endpoint: "/indexes/" + c.indexUID + "/settings/attributes-for-faceting", + method: http.MethodPost, + withRequest: &request, + withResponse: resp, + acceptedStatusCodes: []int{http.StatusAccepted}, + functionName: "GetAttributesForFaceting", + apiName: "Documents", + } + + if err := c.client.executeRequest(req); err != nil { + return nil, err + } + + return resp, nil +} + +func (c clientSettings) ResetAttributesForFaceting() (resp *AsyncUpdateID, err error) { + resp = &AsyncUpdateID{} + req := internalRequest{ + endpoint: "/indexes/" + c.indexUID + "/settings/attributes-for-faceting", + method: http.MethodDelete, + withRequest: nil, + withResponse: resp, + acceptedStatusCodes: []int{http.StatusAccepted}, + functionName: "GetAttributesForFaceting", + apiName: "Documents", + } + + if err := c.client.executeRequest(req); err != nil { + return nil, err + } + + return resp, nil +} diff --git a/types.go b/types.go index 40819a7e..80a91d61 100644 --- a/types.go +++ b/types.go @@ -158,15 +158,18 @@ type SearchRequest struct { AttributesToHighlight []string Filters string Matches bool + FacetsDistribution []string + FacetFilters []string } // SearchResponse is the response body for search method type SearchResponse struct { - Hits []interface{} `json:"hits"` - Offset int64 `json:"offset"` - Limit int64 `json:"limit"` - ProcessingTimeMs int64 `json:"processingTimeMs"` - Query string `json:"query"` + Hits []interface{} `json:"hits"` + Offset int64 `json:"offset"` + Limit int64 `json:"limit"` + ProcessingTimeMs int64 `json:"processingTimeMs"` + Query string `json:"query"` + FacetsDistribution interface{} `json:"facetsDistribution"` } // ListDocumentsRequest is the request body for list documents method From be96c66e72c3308e5df435b826fca1aeb76fc8c9 Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Tue, 9 Jun 2020 01:12:57 +0200 Subject: [PATCH 02/13] function name and api name mistake --- client_settings.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client_settings.go b/client_settings.go index d54e2e61..d8b21ddc 100644 --- a/client_settings.go +++ b/client_settings.go @@ -478,8 +478,8 @@ func (c clientSettings) UpdateAttributesForFaceting(request []string) (resp *Asy withRequest: &request, withResponse: resp, acceptedStatusCodes: []int{http.StatusAccepted}, - functionName: "GetAttributesForFaceting", - apiName: "Documents", + functionName: "UpdateAttributesForFaceting", + apiName: "Settings", } if err := c.client.executeRequest(req); err != nil { @@ -497,8 +497,8 @@ func (c clientSettings) ResetAttributesForFaceting() (resp *AsyncUpdateID, err e withRequest: nil, withResponse: resp, acceptedStatusCodes: []int{http.StatusAccepted}, - functionName: "GetAttributesForFaceting", - apiName: "Documents", + functionName: "ResetAttributesForFaceting", + apiName: "Settings", } if err := c.client.executeRequest(req); err != nil { From 7c327c3cab70b3d6a146d490ec7043c553946b9f Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Wed, 10 Jun 2020 17:52:45 +0200 Subject: [PATCH 03/13] Implement facetFilters --- client_search.go | 28 ++++++++++++++++++++++++++++ types.go | 15 ++++++++------- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/client_search.go b/client_search.go index fb7b05a7..9f8dea80 100644 --- a/client_search.go +++ b/client_search.go @@ -55,6 +55,10 @@ func (c clientSearch) Search(request SearchRequest) (*SearchResponse, error) { if len(request.FacetsDistribution) != 0 { values.Add("facetsDistribution", fmt.Sprintf("[\"%s\"]", strings.Join(request.FacetsDistribution, "\",\""))) } + if request.FacetFilters != nil { + facetFiltersToStr := facetFiltersToStr(request.FacetFilters) + values.Add("facetFilters", facetFiltersToStr) + } req := internalRequest{ endpoint: "/indexes/" + c.indexUID + "/search?" + values.Encode(), @@ -80,3 +84,27 @@ func (c clientSearch) IndexID() string { func (c clientSearch) Client() *Client { return c.client } + +func facetFiltersToStr(i interface{}) string { + facetsToStr := "" + switch v := i.(type) { + case []string: + for _, slice := range v { + stringSlice := slice + facetsToStr += fmt.Sprintf("\"%s\",", stringSlice) + } + facetsToStr = fmt.Sprintf("[%s]", facetsToStr[:len(facetsToStr)-1]) + case [][]string: + for _, mainSlice := range v { + facetsToStr += "[" + for _, slice := range mainSlice { + stringSlice := slice + facetsToStr += fmt.Sprintf("\"%s\",", stringSlice) + } + facetsToStr = facetsToStr[:len(facetsToStr)-1] + "]," + + } + facetsToStr = fmt.Sprintf("[%s]", facetsToStr[:len(facetsToStr)-1]) + } + return (facetsToStr) +} diff --git a/types.go b/types.go index 80a91d61..7396baca 100644 --- a/types.go +++ b/types.go @@ -159,17 +159,18 @@ type SearchRequest struct { Filters string Matches bool FacetsDistribution []string - FacetFilters []string + FacetFilters interface{} } // SearchResponse is the response body for search method type SearchResponse struct { - Hits []interface{} `json:"hits"` - Offset int64 `json:"offset"` - Limit int64 `json:"limit"` - ProcessingTimeMs int64 `json:"processingTimeMs"` - Query string `json:"query"` - FacetsDistribution interface{} `json:"facetsDistribution"` + Hits []interface{} `json:"hits"` + Offset int64 `json:"offset"` + Limit int64 `json:"limit"` + ProcessingTimeMs int64 `json:"processingTimeMs"` + Query string `json:"query"` + FacetsDistribution interface{} `json:"facetsDistribution,omitempty"` + ExhaustiveFacetsCount interface{} `json:"exhaustiveFacetsCount,omitempty"` } // ListDocumentsRequest is the request body for list documents method From c13081c53e04bf53227ecd7e3c50ba1a941e39d8 Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Wed, 10 Jun 2020 18:32:39 +0200 Subject: [PATCH 04/13] Improve search tests --- client_documents_test.go | 6 +++++ client_search_test.go | 57 +++++++++++++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/client_documents_test.go b/client_documents_test.go index 9ef3d024..48a787ac 100644 --- a/client_documents_test.go +++ b/client_documents_test.go @@ -9,6 +9,12 @@ type docTest struct { Name string `json:"name"` } +type docTestBooks struct { + Book_id int `json:"book_id"` + Title string `json:"title"` + Tag string `json:"tag"` +} + func TestClientDocuments_Get(t *testing.T) { var indexUID = "TestClientDocuments_Get" diff --git a/client_search_test.go b/client_search_test.go index adeaacc3..eb6f23e1 100644 --- a/client_search_test.go +++ b/client_search_test.go @@ -1,6 +1,7 @@ package meilisearch import ( + "fmt" "testing" ) @@ -21,10 +22,14 @@ func TestClientSearch_Search(t *testing.T) { updateIDRes, err := client. Documents(indexUID). - AddOrUpdate([]docTest{ - {"0", "J'adore les citrons"}, - {"1", "Les citrons c'est la vie"}, - {"2", "Les ponchos c'est bien !"}, + AddOrUpdate([]docTestBooks{ + {Book_id: 123, Title: "Pride and Prejudice", Tag: "Nice book"}, + {Book_id: 456, Title: "Le Petit Prince", Tag: "Nice book"}, + {Book_id: 1, Title: "Alice In Wonderland", Tag: "Nice book"}, + {Book_id: 1344, Title: "The Hobbit", Tag: "Nice book"}, + {Book_id: 4, Title: "Harry Potter and the Half-Blood Prince", Tag: "Interesting book"}, + {Book_id: 42, Title: "The Hitchhiker's Guide to the Galaxy", Tag: "Interesting book"}, + {Book_id: 24, Title: "You are a princess", Tag: "Interesting book"}, }) if err != nil { @@ -33,9 +38,47 @@ func TestClientSearch_Search(t *testing.T) { client.DefaultWaitForPendingUpdate(indexUID, updateIDRes) + // Test basic search + resp, err := client.Search(indexUID).Search(SearchRequest{ - Query: "citrons", - Limit: 10, + Query: "prince", + }) + + if err != nil { + t.Fatal(err) + } + + if len(resp.Hits) != 3 { + fmt.Println(resp) + t.Fatal("number of hits should be equal to 3") + } + + // Test basic search with limit + + resp, err = client.Search(indexUID).Search(SearchRequest{ + Query: "prince", + Limit: 1, + }) + + if err != nil { + t.Fatal(err) + } + + if len(resp.Hits) != 1 { + fmt.Println(resp) + t.Fatal("number of hits should be equal to 1") + } + title := resp.Hits[0].(map[string]interface{})["title"] + if title != "Le Petit Prince" { + fmt.Println(resp) + t.Fatal("Should have found: Le Petit Prince") + } + + // Test basic search with offset + + resp, err = client.Search(indexUID).Search(SearchRequest{ + Query: "prince", + Offset: 1, }) if err != nil { @@ -43,6 +86,8 @@ func TestClientSearch_Search(t *testing.T) { } if len(resp.Hits) != 2 { + fmt.Println(resp) t.Fatal("number of hits should be equal to 2") } + } From 902c005e4890a926aad02fe4ca82a7e84b0d9127 Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Thu, 11 Jun 2020 00:23:34 +0200 Subject: [PATCH 05/13] Improve search tests II --- client_search_test.go | 78 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/client_search_test.go b/client_search_test.go index eb6f23e1..71d5bc90 100644 --- a/client_search_test.go +++ b/client_search_test.go @@ -90,4 +90,82 @@ func TestClientSearch_Search(t *testing.T) { t.Fatal("number of hits should be equal to 2") } + // Test basic search with offset + + resp, err = client.Search(indexUID).Search(SearchRequest{ + Query: "prince", + Offset: 1, + }) + + if err != nil { + t.Fatal(err) + } + + if len(resp.Hits) != 2 { + fmt.Println(resp) + t.Fatal("number of hits should be equal to 2") + } + + // Test basic search with attributesToRetrieve + + resp, err = client.Search(indexUID).Search(SearchRequest{ + Query: "prince", + AttributesToRetrieve: []string{"book_id", "title"}, + }) + + if err != nil { + t.Fatal(err) + } + + if resp.Hits[0].(map[string]interface{})["title"] == nil { + fmt.Println(resp) + t.Fatal("attributesToRetrieve: Couldn't retrieve field in response") + } + if resp.Hits[0].(map[string]interface{})["tag"] != nil { + fmt.Println(resp) + t.Fatal("attributesToRetrieve: Retrieve unrequested field in response") + } + + // Test basic search with attributesToCrop + + resp, err = client.Search(indexUID).Search(SearchRequest{ + Query: "to", + AttributesToCrop: []string{"title"}, + CropLength: 7, + }) + + if err != nil { + t.Fatal(err) + } + + if resp.Hits[0].(map[string]interface{})["title"] == nil { + fmt.Println(resp) + t.Fatal("attributesToCrop: Couldn't retrieve field in response") + } + formatted := resp.Hits[0].(map[string]interface{})["_formatted"] + if formatted.(map[string]interface{})["title"] != "Guide to the" { + fmt.Println(resp) + t.Fatal("attributesToCrop: CropLength didn't work as expected") + } + + // Test basic search with filters + + resp, err = client.Search(indexUID).Search(SearchRequest{ + Query: "and", + Filters: "tag = \"Nice book\"", + }) + + if err != nil { + t.Fatal(err) + } + + if len(resp.Hits) != 1 { + fmt.Println(resp) + t.Fatal("filters: Unable to filter properly") + } + if resp.Hits[0].(map[string]interface{})["title"] != "Pride and Prejudice" { + fmt.Println(resp) + t.Fatal("filters: Unable to filter properly") + } + } From e1d00de5a45eaf9a96346387fd107be757064a9b Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Thu, 11 Jun 2020 00:56:04 +0200 Subject: [PATCH 06/13] Improve search tests III: matches and facetsDistribution --- client_search_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/client_search_test.go b/client_search_test.go index 71d5bc90..aa673025 100644 --- a/client_search_test.go +++ b/client_search_test.go @@ -2,6 +2,7 @@ package meilisearch import ( "fmt" + "reflect" "testing" ) @@ -168,4 +169,50 @@ func TestClientSearch_Search(t *testing.T) { t.Fatal("filters: Unable to filter properly") } + // Test basic search with matches + + resp, err = client.Search(indexUID).Search(SearchRequest{ + Query: "and", + Matches: true, + }) + + if err != nil { + t.Fatal(err) + } + + if resp.Hits[0].(map[string]interface{})["_matchesInfo"] == nil { + fmt.Println(resp) + t.Fatal("matches: Mathes info not found") + } + + // Test basic search with facetsDistribution + + r2, err := client.Settings(indexUID).UpdateAttributesForFaceting([]string{"tag"}) + + if err != nil { + t.Fatal(err) + } + + client.DefaultWaitForPendingUpdate(indexUID, r2) + + resp, err = client.Search(indexUID).Search(SearchRequest{ + Query: "prince", + FacetsDistribution: []string{"*"}, + }) + + if err != nil { + t.Fatal(err) + } + + tagCount := resp.FacetsDistribution.(map[string]interface{})["tag"] + + if len(tagCount.(map[string]interface{})) != 2 { + fmt.Println(tagCount.(map[string]interface{})) + t.Fatal("facetsDistribution: Wrong count of facet options") + } + + if tagCount.(map[string]interface{})["interesting book"] != float64(2) { + fmt.Println(reflect.TypeOf(tagCount.(map[string]interface{})["interesting book"])) + t.Fatal("facetsDistribution: Wrong count on facetDistribution") + } } From dbc6238bc2a522a6bc025c375cdf0fe7b3edbc6c Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Thu, 11 Jun 2020 01:07:32 +0200 Subject: [PATCH 07/13] Improve search tests IV: facetFilters --- client_search_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/client_search_test.go b/client_search_test.go index aa673025..b355cd8d 100644 --- a/client_search_test.go +++ b/client_search_test.go @@ -215,4 +215,57 @@ func TestClientSearch_Search(t *testing.T) { fmt.Println(reflect.TypeOf(tagCount.(map[string]interface{})["interesting book"])) t.Fatal("facetsDistribution: Wrong count on facetDistribution") } + + r2, _ = client.Settings(indexUID).ResetAttributesForFaceting() + client.DefaultWaitForPendingUpdate(indexUID, r2) + + // Test basic search with facetFilters + + r2, err = client.Settings(indexUID).UpdateAttributesForFaceting([]string{"tag", "title"}) + + if err != nil { + t.Fatal(err) + } + + client.DefaultWaitForPendingUpdate(indexUID, r2) + + resp, err = client.Search(indexUID).Search(SearchRequest{ + Query: "prince", + FacetFilters: []string{"tag:interesting book"}, + }) + if err != nil { + fmt.Println("Error:", err) + } + + if len(resp.Hits) != 2 { + fmt.Println(resp) + t.Fatal("facetsFilters: Error on single attribute facet search") + } + + resp, err = client.Search(indexUID).Search(SearchRequest{ + Query: "prince", + FacetFilters: []string{"tag:interesting book", "tag:nice book"}, + }) + if err != nil { + fmt.Println("Error:", err) + } + + if len(resp.Hits) != 0 { + fmt.Println(resp) + t.Fatal("facetsFilters: Error on 'AND' in attribute facet search") + } + + resp, err = client.Search(indexUID).Search(SearchRequest{ + Query: "prince", + FacetFilters: [][]string{{"tag:interesting book", "tag:nice book"}}, + }) + if err != nil { + fmt.Println("Error:", err) + } + + if len(resp.Hits) != 3 { + fmt.Println(resp) + t.Fatal("facetsFilters: Error on 'OR' in attribute facet search") + } + } From 35af4d526b51c4a9ff3fa716dd3ad74c15a08936 Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Thu, 11 Jun 2020 01:09:44 +0200 Subject: [PATCH 08/13] Remove reflect --- client_search_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/client_search_test.go b/client_search_test.go index b355cd8d..8e608af0 100644 --- a/client_search_test.go +++ b/client_search_test.go @@ -2,7 +2,6 @@ package meilisearch import ( "fmt" - "reflect" "testing" ) @@ -212,7 +211,7 @@ func TestClientSearch_Search(t *testing.T) { } if tagCount.(map[string]interface{})["interesting book"] != float64(2) { - fmt.Println(reflect.TypeOf(tagCount.(map[string]interface{})["interesting book"])) + fmt.Println(tagCount.(map[string]interface{})["interesting book"]) t.Fatal("facetsDistribution: Wrong count on facetDistribution") } From 4358dd1c5a9e25a2435cd37640e8334c51daf3d5 Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Thu, 11 Jun 2020 03:12:30 +0200 Subject: [PATCH 09/13] Add tests for faceting routes: settings and get/update/reset attributesForFaceting --- client_settings_test.go | 119 +++++++++++++++++++++++++++++++++++++--- types.go | 15 ++--- 2 files changed, 119 insertions(+), 15 deletions(-) diff --git a/client_settings_test.go b/client_settings_test.go index 4300cb46..eb2184d6 100644 --- a/client_settings_test.go +++ b/client_settings_test.go @@ -1,6 +1,7 @@ package meilisearch import ( + "fmt" "reflect" "testing" ) @@ -27,13 +28,14 @@ func TestClientSettings_GetAll(t *testing.T) { } expected := Settings{ - RankingRules: []string{"typo", "words", "proximity", "attribute", "wordsPosition", "exactness"}, - DistinctAttribute: nil, - SearchableAttributes: []string{}, - DisplayedAttributes: []string{}, - StopWords: []string{}, - Synonyms: map[string][]string{}, - AcceptNewFields: true, + RankingRules: []string{"typo", "words", "proximity", "attribute", "wordsPosition", "exactness"}, + DistinctAttribute: nil, + SearchableAttributes: []string{}, + DisplayedAttributes: []string{}, + StopWords: []string{}, + Synonyms: map[string][]string{}, + AcceptNewFields: true, + AttributesForFaceting: []string{}, } if !reflect.DeepEqual(*settingsRes, expected) { @@ -64,7 +66,8 @@ func TestClientSettings_UpdateAll(t *testing.T) { Synonyms: map[string][]string{ "car": []string{"automobile"}, }, - AcceptNewFields: false, + AcceptNewFields: false, + AttributesForFaceting: []string{"title"}, } updateIDRes, err := client.Settings(indexUID).UpdateAll(settings) @@ -616,3 +619,103 @@ func TestClientSettings_UpdateAcceptNewFields(t *testing.T) { client.DefaultWaitForPendingUpdate(indexUID, updateIDRes) } + +func TestClientSettings_GetAttributesForFaceting(t *testing.T) { + var indexUID = "TestClientSettings_GetAttributesForFaceting" + + var client = NewClient(Config{ + Host: "http://localhost:7700", + }) + + _, err := client.Indexes().Create(CreateIndexRequest{ + UID: indexUID, + }) + + if err != nil { + t.Fatal(err) + } + + AttributesForFacetingRes, err := client.Settings(indexUID).GetAttributesForFaceting() + + if err != nil { + t.Fatal(err) + } + + if reflect.DeepEqual(*AttributesForFacetingRes, nil) { + t.Fatal("getAttributesForFaceting: Error getting attributesForFaceting on empty index") + } +} + +func TestClientSettings_UpdateAttributesForFaceting(t *testing.T) { + var indexUID = "TestClientSettings_UpdateAttributesForFaceting" + + var client = NewClient(Config{ + Host: "http://localhost:7700", + }) + + attributesForFaceting := []string{"tag", "title"} + + _, err := client.Indexes().Create(CreateIndexRequest{ + UID: indexUID, + }) + + if err != nil { + t.Fatal(err) + } + + updateIDRes, err := client.Settings(indexUID).UpdateAttributesForFaceting(attributesForFaceting) + + if err != nil { + t.Fatal(err) + } + + client.DefaultWaitForPendingUpdate(indexUID, updateIDRes) + r, _ := client.Settings(indexUID).GetAttributesForFaceting() + if !reflect.DeepEqual(*r, attributesForFaceting) { + fmt.Println(*r) + t.Fatal("updateAttributesForFaceting: Error getting attributesForFaceting after update") + } +} + +func TestClientSettings_ResetAttributesForFaceting(t *testing.T) { + var indexUID = "TestClientSettings_ResetAttributesForFaceting" + + var client = NewClient(Config{ + Host: "http://localhost:7700", + }) + + attributesForFaceting := []string{"tag", "title"} + + _, err := client.Indexes().Create(CreateIndexRequest{ + UID: indexUID, + }) + + if err != nil { + t.Fatal(err) + } + + updateIDRes, err := client.Settings(indexUID).UpdateAttributesForFaceting(attributesForFaceting) + + if err != nil { + t.Fatal(err) + } + + client.DefaultWaitForPendingUpdate(indexUID, updateIDRes) + r, _ := client.Settings(indexUID).GetAttributesForFaceting() + if !reflect.DeepEqual(*r, attributesForFaceting) { + fmt.Println(*r) + t.Fatal("resetAttributesForFaceting: Error getting attributesForFaceting after update") + } + + updateIDRes, err = client.Settings(indexUID).ResetAttributesForFaceting() + + if err != nil { + t.Fatal(err) + } + + client.DefaultWaitForPendingUpdate(indexUID, updateIDRes) + r, _ = client.Settings(indexUID).GetAttributesForFaceting() + if reflect.DeepEqual(*r, nil) { + t.Fatal("resetAttributesForFaceting: Error getting attributesForFaceting after reset") + } +} diff --git a/types.go b/types.go index 7396baca..69bc010c 100644 --- a/types.go +++ b/types.go @@ -20,13 +20,14 @@ type Index struct { // Settings is the type that represent the settings in MeiliSearch type Settings struct { - RankingRules []string `json:"rankingRules,omitempty"` - DistinctAttribute *string `json:"distinctAttribute,omitempty"` - SearchableAttributes []string `json:"searchableAttributes,omitempty"` - DisplayedAttributes []string `json:"displayedAttributes,omitempty"` - StopWords []string `json:"stopWords,omitempty"` - Synonyms map[string][]string `json:"synonyms,omitempty"` - AcceptNewFields bool `json:"acceptNewFields,omitempty"` + RankingRules []string `json:"rankingRules,omitempty"` + DistinctAttribute *string `json:"distinctAttribute,omitempty"` + SearchableAttributes []string `json:"searchableAttributes,omitempty"` + DisplayedAttributes []string `json:"displayedAttributes,omitempty"` + StopWords []string `json:"stopWords,omitempty"` + Synonyms map[string][]string `json:"synonyms,omitempty"` + AcceptNewFields bool `json:"acceptNewFields,omitempty"` + AttributesForFaceting []string `json:"attributesForFaceting,omitempty"` } // Version is the type that represent the versions in MeiliSearch From 55d3d716b3480d3b0e9238f312a1c650755bdb57 Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Thu, 11 Jun 2020 12:12:15 +0200 Subject: [PATCH 10/13] Fix typos MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clémentine Urquizar --- types.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types.go b/types.go index 69bc010c..16f90934 100644 --- a/types.go +++ b/types.go @@ -18,7 +18,7 @@ type Index struct { PrimaryKey string `json:"primaryKey,omitempty"` } -// Settings is the type that represent the settings in MeiliSearch +// Settings is the type that represents the settings in MeiliSearch type Settings struct { RankingRules []string `json:"rankingRules,omitempty"` DistinctAttribute *string `json:"distinctAttribute,omitempty"` @@ -30,7 +30,7 @@ type Settings struct { AttributesForFaceting []string `json:"attributesForFaceting,omitempty"` } -// Version is the type that represent the versions in MeiliSearch +// Version is the type that represents the versions in MeiliSearch type Version struct { CommitSha string `json:"commitSha"` BuildDate time.Time `json:"buildDate"` From 45d36c40b539dc342a28442cc90482a5fcb99213 Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Thu, 11 Jun 2020 12:43:28 +0200 Subject: [PATCH 11/13] More test improvements --- client_search_test.go | 71 +++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/client_search_test.go b/client_search_test.go index 8e608af0..7ea5bca4 100644 --- a/client_search_test.go +++ b/client_search_test.go @@ -20,17 +20,19 @@ func TestClientSearch_Search(t *testing.T) { t.Fatal(err) } + booksTest := []docTestBooks{ + {Book_id: 123, Title: "Pride and Prejudice", Tag: "Nice book"}, + {Book_id: 456, Title: "Le Petit Prince", Tag: "Nice book"}, + {Book_id: 1, Title: "Alice In Wonderland", Tag: "Nice book"}, + {Book_id: 1344, Title: "The Hobbit", Tag: "Nice book"}, + {Book_id: 4, Title: "Harry Potter and the Half-Blood Prince", Tag: "Interesting book"}, + {Book_id: 42, Title: "The Hitchhiker's Guide to the Galaxy", Tag: "Interesting book"}, + {Book_id: 24, Title: "You are a princess", Tag: "Interesting book"}, + } + updateIDRes, err := client. Documents(indexUID). - AddOrUpdate([]docTestBooks{ - {Book_id: 123, Title: "Pride and Prejudice", Tag: "Nice book"}, - {Book_id: 456, Title: "Le Petit Prince", Tag: "Nice book"}, - {Book_id: 1, Title: "Alice In Wonderland", Tag: "Nice book"}, - {Book_id: 1344, Title: "The Hobbit", Tag: "Nice book"}, - {Book_id: 4, Title: "Harry Potter and the Half-Blood Prince", Tag: "Interesting book"}, - {Book_id: 42, Title: "The Hitchhiker's Guide to the Galaxy", Tag: "Interesting book"}, - {Book_id: 24, Title: "You are a princess", Tag: "Interesting book"}, - }) + AddOrUpdate(booksTest) if err != nil { t.Fatal(err) @@ -50,7 +52,12 @@ func TestClientSearch_Search(t *testing.T) { if len(resp.Hits) != 3 { fmt.Println(resp) - t.Fatal("number of hits should be equal to 3") + t.Fatal("Basic search: number of hits should be equal to 3") + } + title := resp.Hits[0].(map[string]interface{})["title"] + if title != booksTest[1].Title { + fmt.Println(resp) + t.Fatalf("Basic search: should have found %s\n", booksTest[1].Title) } // Test basic search with limit @@ -66,12 +73,12 @@ func TestClientSearch_Search(t *testing.T) { if len(resp.Hits) != 1 { fmt.Println(resp) - t.Fatal("number of hits should be equal to 1") + t.Fatal("Search offset: number of hits should be equal to 1") } - title := resp.Hits[0].(map[string]interface{})["title"] - if title != "Le Petit Prince" { + title = resp.Hits[0].(map[string]interface{})["title"] + if title != booksTest[1].Title { fmt.Println(resp) - t.Fatal("Should have found: Le Petit Prince") + t.Fatalf("Basic search: should have found %s\n", booksTest[1].Title) } // Test basic search with offset @@ -89,21 +96,27 @@ func TestClientSearch_Search(t *testing.T) { fmt.Println(resp) t.Fatal("number of hits should be equal to 2") } - - // Test basic search with offset - - resp, err = client.Search(indexUID).Search(SearchRequest{ - Query: "prince", - Offset: 1, - }) - - if err != nil { - t.Fatal(err) - } - - if len(resp.Hits) != 2 { - fmt.Println(resp) - t.Fatal("number of hits should be equal to 2") + retrievedTitles := []string{ + fmt.Sprint(resp.Hits[0].(map[string]interface{})["title"]), + fmt.Sprint(resp.Hits[1].(map[string]interface{})["title"]), + } + expectedTitles := []string{ + booksTest[4].Title, + booksTest[6].Title, + } + + for title := range expectedTitles { + found := false + for retrievedTitle := range retrievedTitles { + if title == retrievedTitle { + found = true + break + } + } + if !found { + fmt.Println(resp) + t.Fatal("Search offset: should have found 'Harry Potter and the Half-Blood Prince'") + } } // Test basic search with attributesToRetrieve From 8ba0a6c0b43cd79f665c18137da29460f5d03430 Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Fri, 12 Jun 2020 02:31:06 +0200 Subject: [PATCH 12/13] Use %q for format text quoting and fix variable naming with GO convention --- client_documents_test.go | 6 +++--- client_search.go | 6 +++--- client_search_test.go | 14 +++++++------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/client_documents_test.go b/client_documents_test.go index 48a787ac..a6140fcf 100644 --- a/client_documents_test.go +++ b/client_documents_test.go @@ -10,9 +10,9 @@ type docTest struct { } type docTestBooks struct { - Book_id int `json:"book_id"` - Title string `json:"title"` - Tag string `json:"tag"` + BookId int `json:"book_id"` + Title string `json:"title"` + Tag string `json:"tag"` } func TestClientDocuments_Get(t *testing.T) { diff --git a/client_search.go b/client_search.go index 9f8dea80..f3c56451 100644 --- a/client_search.go +++ b/client_search.go @@ -53,7 +53,7 @@ func (c clientSearch) Search(request SearchRequest) (*SearchResponse, error) { values.Add("matches", strconv.FormatBool(request.Matches)) } if len(request.FacetsDistribution) != 0 { - values.Add("facetsDistribution", fmt.Sprintf("[\"%s\"]", strings.Join(request.FacetsDistribution, "\",\""))) + values.Add("facetsDistribution", fmt.Sprintf("[%q]", strings.Join(request.FacetsDistribution, "\",\""))) } if request.FacetFilters != nil { facetFiltersToStr := facetFiltersToStr(request.FacetFilters) @@ -91,7 +91,7 @@ func facetFiltersToStr(i interface{}) string { case []string: for _, slice := range v { stringSlice := slice - facetsToStr += fmt.Sprintf("\"%s\",", stringSlice) + facetsToStr += fmt.Sprintf("%q,", stringSlice) } facetsToStr = fmt.Sprintf("[%s]", facetsToStr[:len(facetsToStr)-1]) case [][]string: @@ -99,7 +99,7 @@ func facetFiltersToStr(i interface{}) string { facetsToStr += "[" for _, slice := range mainSlice { stringSlice := slice - facetsToStr += fmt.Sprintf("\"%s\",", stringSlice) + facetsToStr += fmt.Sprintf("%q,", stringSlice) } facetsToStr = facetsToStr[:len(facetsToStr)-1] + "]," diff --git a/client_search_test.go b/client_search_test.go index 7ea5bca4..6f47148e 100644 --- a/client_search_test.go +++ b/client_search_test.go @@ -21,13 +21,13 @@ func TestClientSearch_Search(t *testing.T) { } booksTest := []docTestBooks{ - {Book_id: 123, Title: "Pride and Prejudice", Tag: "Nice book"}, - {Book_id: 456, Title: "Le Petit Prince", Tag: "Nice book"}, - {Book_id: 1, Title: "Alice In Wonderland", Tag: "Nice book"}, - {Book_id: 1344, Title: "The Hobbit", Tag: "Nice book"}, - {Book_id: 4, Title: "Harry Potter and the Half-Blood Prince", Tag: "Interesting book"}, - {Book_id: 42, Title: "The Hitchhiker's Guide to the Galaxy", Tag: "Interesting book"}, - {Book_id: 24, Title: "You are a princess", Tag: "Interesting book"}, + {BookId: 123, Title: "Pride and Prejudice", Tag: "Nice book"}, + {BookId: 456, Title: "Le Petit Prince", Tag: "Nice book"}, + {BookId: 1, Title: "Alice In Wonderland", Tag: "Nice book"}, + {BookId: 1344, Title: "The Hobbit", Tag: "Nice book"}, + {BookId: 4, Title: "Harry Potter and the Half-Blood Prince", Tag: "Interesting book"}, + {BookId: 42, Title: "The Hitchhiker's Guide to the Galaxy", Tag: "Interesting book"}, + {BookId: 24, Title: "You are a princess", Tag: "Interesting book"}, } updateIDRes, err := client. From 75d9d8a6278d59d277ac04e73b0b0d66a892ffbe Mon Sep 17 00:00:00 2001 From: Samuel Jimenez Date: Fri, 12 Jun 2020 16:24:11 +0200 Subject: [PATCH 13/13] BookId => BookID --- client_documents_test.go | 2 +- client_search_test.go | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/client_documents_test.go b/client_documents_test.go index a6140fcf..170fbc65 100644 --- a/client_documents_test.go +++ b/client_documents_test.go @@ -10,7 +10,7 @@ type docTest struct { } type docTestBooks struct { - BookId int `json:"book_id"` + BookID int `json:"book_id"` Title string `json:"title"` Tag string `json:"tag"` } diff --git a/client_search_test.go b/client_search_test.go index 6f47148e..0c033534 100644 --- a/client_search_test.go +++ b/client_search_test.go @@ -21,13 +21,13 @@ func TestClientSearch_Search(t *testing.T) { } booksTest := []docTestBooks{ - {BookId: 123, Title: "Pride and Prejudice", Tag: "Nice book"}, - {BookId: 456, Title: "Le Petit Prince", Tag: "Nice book"}, - {BookId: 1, Title: "Alice In Wonderland", Tag: "Nice book"}, - {BookId: 1344, Title: "The Hobbit", Tag: "Nice book"}, - {BookId: 4, Title: "Harry Potter and the Half-Blood Prince", Tag: "Interesting book"}, - {BookId: 42, Title: "The Hitchhiker's Guide to the Galaxy", Tag: "Interesting book"}, - {BookId: 24, Title: "You are a princess", Tag: "Interesting book"}, + {BookID: 123, Title: "Pride and Prejudice", Tag: "Nice book"}, + {BookID: 456, Title: "Le Petit Prince", Tag: "Nice book"}, + {BookID: 1, Title: "Alice In Wonderland", Tag: "Nice book"}, + {BookID: 1344, Title: "The Hobbit", Tag: "Nice book"}, + {BookID: 4, Title: "Harry Potter and the Half-Blood Prince", Tag: "Interesting book"}, + {BookID: 42, Title: "The Hitchhiker's Guide to the Galaxy", Tag: "Interesting book"}, + {BookID: 24, Title: "You are a princess", Tag: "Interesting book"}, } updateIDRes, err := client.