diff --git a/index_search_test.go b/index_search_test.go index e1100280..7f7b1da5 100644 --- a/index_search_test.go +++ b/index_search_test.go @@ -916,3 +916,195 @@ func TestIndex_SearchWithSort(t *testing.T) { }) } } + +func TestIndex_SearchOnNestedFileds(t *testing.T) { + type args struct { + UID string + PrimaryKey string + client *Client + query string + request SearchRequest + searchableAttribute []string + sortableAttribute []string + } + tests := []struct { + name string + args args + want *SearchResponse + }{ + { + name: "TestIndexBasicSearchOnNestedFields", + args: args{ + UID: "TestIndexBasicSearchOnNestedFields", + client: defaultClient, + query: "An awesome", + request: SearchRequest{}, + }, + want: &SearchResponse{ + Hits: []interface{}{ + map[string]interface{}{ + "id": float64(5), "title": "The Hobbit", + "info": map[string]interface{}{ + "comment": "An awesome book", + "reviewNb": float64(900), + }, + }, + }, + NbHits: 1, + Offset: 0, + Limit: 20, + ExhaustiveNbHits: false, + }, + }, + { + name: "TestIndexBasicSearchOnNestedFieldsWithCustomClient", + args: args{ + UID: "TestIndexBasicSearchOnNestedFieldsWithCustomClient", + client: customClient, + query: "An awesome", + request: SearchRequest{}, + }, + want: &SearchResponse{ + Hits: []interface{}{ + map[string]interface{}{ + "id": float64(5), "title": "The Hobbit", + "info": map[string]interface{}{ + "comment": "An awesome book", + "reviewNb": float64(900), + }, + }, + }, + NbHits: 1, + Offset: 0, + Limit: 20, + ExhaustiveNbHits: false, + }, + }, + { + name: "TestIndexSearchOnMultipleNestedFields", + args: args{ + UID: "TestIndexSearchOnMultipleNestedFields", + client: defaultClient, + query: "french", + request: SearchRequest{}, + }, + want: &SearchResponse{ + Hits: []interface{}{ + map[string]interface{}{ + "id": float64(2), "title": "Le Petit Prince", + "info": map[string]interface{}{ + "comment": "A french book", + "reviewNb": float64(600), + }, + }, + map[string]interface{}{ + "id": float64(3), "title": "Le Rouge et le Noir", + "info": map[string]interface{}{ + "comment": "Another french book", + "reviewNb": float64(700), + }, + }, + }, + NbHits: 2, + Offset: 0, + Limit: 20, + ExhaustiveNbHits: false, + }, + }, + { + name: "TestIndexSearchOnNestedFieldsWithSearchableAttribute", + args: args{ + UID: "TestIndexSearchOnNestedFieldsWithSearchableAttribute", + client: defaultClient, + query: "An awesome", + request: SearchRequest{}, + searchableAttribute: []string{ + "title", "info.comment", + }, + }, + want: &SearchResponse{ + Hits: []interface{}{ + map[string]interface{}{ + "id": float64(5), "title": "The Hobbit", + "info": map[string]interface{}{ + "comment": "An awesome book", + "reviewNb": float64(900), + }, + }, + }, + NbHits: 1, + Offset: 0, + Limit: 20, + ExhaustiveNbHits: false, + }, + }, + { + name: "TestIndexSearchOnNestedFieldsWithSortableAttribute", + args: args{ + UID: "TestIndexSearchOnNestedFieldsWithSortableAttribute", + client: defaultClient, + query: "An awesome", + request: SearchRequest{ + Sort: []string{ + "info.reviewNb:desc", + }, + }, + searchableAttribute: []string{ + "title", "info.comment", + }, + sortableAttribute: []string{ + "info.reviewNb", + }, + }, + want: &SearchResponse{ + Hits: []interface{}{ + map[string]interface{}{ + "id": float64(5), "title": "The Hobbit", + "info": map[string]interface{}{ + "comment": "An awesome book", + "reviewNb": float64(900), + }, + }, + }, + NbHits: 1, + Offset: 0, + Limit: 20, + ExhaustiveNbHits: false, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + SetUpIndexWithNestedFields(tt.args.UID) + c := tt.args.client + i := c.Index(tt.args.UID) + t.Cleanup(cleanup(c)) + + if tt.args.searchableAttribute != nil { + gotTask, err := i.UpdateSearchableAttributes(&tt.args.searchableAttribute) + require.NoError(t, err) + testWaitForTask(t, i, gotTask) + } + + if tt.args.sortableAttribute != nil { + gotTask, err := i.UpdateSortableAttributes(&tt.args.sortableAttribute) + require.NoError(t, err) + testWaitForTask(t, i, gotTask) + } + + got, err := i.Search(tt.args.query, &tt.args.request) + + require.NoError(t, err) + require.Equal(t, len(tt.want.Hits), len(got.Hits)) + for len := range got.Hits { + require.Equal(t, tt.want.Hits[len], got.Hits[len]) + } + require.Equal(t, tt.want.NbHits, got.NbHits) + require.Equal(t, tt.want.Offset, got.Offset) + require.Equal(t, tt.want.Limit, got.Limit) + require.Equal(t, tt.want.ExhaustiveNbHits, got.ExhaustiveNbHits) + require.Equal(t, tt.want.FacetsDistribution, got.FacetsDistribution) + require.Equal(t, tt.want.ExhaustiveFacetsCount, got.ExhaustiveFacetsCount) + }) + } +} diff --git a/main_test.go b/main_test.go index 534ecabb..5e7578ee 100644 --- a/main_test.go +++ b/main_test.go @@ -133,6 +133,33 @@ func SetUpBasicIndex(indexUID string) { } } +func SetUpIndexWithNestedFields(indexUID string) { + client := NewClient(ClientConfig{ + Host: "http://localhost:7700", + APIKey: masterKey, + }) + index := client.Index(indexUID) + + documents := []map[string]interface{}{ + {"id": 1, "title": "Pride and Prejudice", "info": map[string]interface{}{"comment": "A great book", "reviewNb": 50}}, + {"id": 2, "title": "Le Petit Prince", "info": map[string]interface{}{"comment": "A french book", "reviewNb": 600}}, + {"id": 3, "title": "Le Rouge et le Noir", "info": map[string]interface{}{"comment": "Another french book", "reviewNb": 700}}, + {"id": 4, "title": "Alice In Wonderland", "comment": "A weird book", "info": map[string]interface{}{"comment": "A weird book", "reviewNb": 800}}, + {"id": 5, "title": "The Hobbit", "info": map[string]interface{}{"comment": "An awesome book", "reviewNb": 900}}, + {"id": 6, "title": "Harry Potter and the Half-Blood Prince", "info": map[string]interface{}{"comment": "The best book", "reviewNb": 1000}}, + {"id": 7, "title": "The Hitchhiker's Guide to the Galaxy"}, + } + task, err := index.AddDocuments(documents) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + finalTask, _ := index.WaitForTask(task) + if finalTask.Status != "succeeded" { + os.Exit(1) + } +} + func SetUpIndexForFaceting() { client := NewClient(ClientConfig{ Host: "http://localhost:7700", diff --git a/version_test.go b/version_test.go index 3a6a0e90..a1488391 100644 --- a/version_test.go +++ b/version_test.go @@ -1,12 +1,12 @@ package meilisearch import ( - "testing" "fmt" "regexp" + "testing" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestVersion_GetQualifiedVersion(t *testing.T) {