Skip to content

Commit d9d08e0

Browse files
authored
Merge pull request #290 from meilisearch/support-nested-field
Ensure nested field support
2 parents 8445009 + f76c285 commit d9d08e0

File tree

3 files changed

+221
-2
lines changed

3 files changed

+221
-2
lines changed

index_search_test.go

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,3 +916,195 @@ func TestIndex_SearchWithSort(t *testing.T) {
916916
})
917917
}
918918
}
919+
920+
func TestIndex_SearchOnNestedFileds(t *testing.T) {
921+
type args struct {
922+
UID string
923+
PrimaryKey string
924+
client *Client
925+
query string
926+
request SearchRequest
927+
searchableAttribute []string
928+
sortableAttribute []string
929+
}
930+
tests := []struct {
931+
name string
932+
args args
933+
want *SearchResponse
934+
}{
935+
{
936+
name: "TestIndexBasicSearchOnNestedFields",
937+
args: args{
938+
UID: "TestIndexBasicSearchOnNestedFields",
939+
client: defaultClient,
940+
query: "An awesome",
941+
request: SearchRequest{},
942+
},
943+
want: &SearchResponse{
944+
Hits: []interface{}{
945+
map[string]interface{}{
946+
"id": float64(5), "title": "The Hobbit",
947+
"info": map[string]interface{}{
948+
"comment": "An awesome book",
949+
"reviewNb": float64(900),
950+
},
951+
},
952+
},
953+
NbHits: 1,
954+
Offset: 0,
955+
Limit: 20,
956+
ExhaustiveNbHits: false,
957+
},
958+
},
959+
{
960+
name: "TestIndexBasicSearchOnNestedFieldsWithCustomClient",
961+
args: args{
962+
UID: "TestIndexBasicSearchOnNestedFieldsWithCustomClient",
963+
client: customClient,
964+
query: "An awesome",
965+
request: SearchRequest{},
966+
},
967+
want: &SearchResponse{
968+
Hits: []interface{}{
969+
map[string]interface{}{
970+
"id": float64(5), "title": "The Hobbit",
971+
"info": map[string]interface{}{
972+
"comment": "An awesome book",
973+
"reviewNb": float64(900),
974+
},
975+
},
976+
},
977+
NbHits: 1,
978+
Offset: 0,
979+
Limit: 20,
980+
ExhaustiveNbHits: false,
981+
},
982+
},
983+
{
984+
name: "TestIndexSearchOnMultipleNestedFields",
985+
args: args{
986+
UID: "TestIndexSearchOnMultipleNestedFields",
987+
client: defaultClient,
988+
query: "french",
989+
request: SearchRequest{},
990+
},
991+
want: &SearchResponse{
992+
Hits: []interface{}{
993+
map[string]interface{}{
994+
"id": float64(2), "title": "Le Petit Prince",
995+
"info": map[string]interface{}{
996+
"comment": "A french book",
997+
"reviewNb": float64(600),
998+
},
999+
},
1000+
map[string]interface{}{
1001+
"id": float64(3), "title": "Le Rouge et le Noir",
1002+
"info": map[string]interface{}{
1003+
"comment": "Another french book",
1004+
"reviewNb": float64(700),
1005+
},
1006+
},
1007+
},
1008+
NbHits: 2,
1009+
Offset: 0,
1010+
Limit: 20,
1011+
ExhaustiveNbHits: false,
1012+
},
1013+
},
1014+
{
1015+
name: "TestIndexSearchOnNestedFieldsWithSearchableAttribute",
1016+
args: args{
1017+
UID: "TestIndexSearchOnNestedFieldsWithSearchableAttribute",
1018+
client: defaultClient,
1019+
query: "An awesome",
1020+
request: SearchRequest{},
1021+
searchableAttribute: []string{
1022+
"title", "info.comment",
1023+
},
1024+
},
1025+
want: &SearchResponse{
1026+
Hits: []interface{}{
1027+
map[string]interface{}{
1028+
"id": float64(5), "title": "The Hobbit",
1029+
"info": map[string]interface{}{
1030+
"comment": "An awesome book",
1031+
"reviewNb": float64(900),
1032+
},
1033+
},
1034+
},
1035+
NbHits: 1,
1036+
Offset: 0,
1037+
Limit: 20,
1038+
ExhaustiveNbHits: false,
1039+
},
1040+
},
1041+
{
1042+
name: "TestIndexSearchOnNestedFieldsWithSortableAttribute",
1043+
args: args{
1044+
UID: "TestIndexSearchOnNestedFieldsWithSortableAttribute",
1045+
client: defaultClient,
1046+
query: "An awesome",
1047+
request: SearchRequest{
1048+
Sort: []string{
1049+
"info.reviewNb:desc",
1050+
},
1051+
},
1052+
searchableAttribute: []string{
1053+
"title", "info.comment",
1054+
},
1055+
sortableAttribute: []string{
1056+
"info.reviewNb",
1057+
},
1058+
},
1059+
want: &SearchResponse{
1060+
Hits: []interface{}{
1061+
map[string]interface{}{
1062+
"id": float64(5), "title": "The Hobbit",
1063+
"info": map[string]interface{}{
1064+
"comment": "An awesome book",
1065+
"reviewNb": float64(900),
1066+
},
1067+
},
1068+
},
1069+
NbHits: 1,
1070+
Offset: 0,
1071+
Limit: 20,
1072+
ExhaustiveNbHits: false,
1073+
},
1074+
},
1075+
}
1076+
for _, tt := range tests {
1077+
t.Run(tt.name, func(t *testing.T) {
1078+
SetUpIndexWithNestedFields(tt.args.UID)
1079+
c := tt.args.client
1080+
i := c.Index(tt.args.UID)
1081+
t.Cleanup(cleanup(c))
1082+
1083+
if tt.args.searchableAttribute != nil {
1084+
gotTask, err := i.UpdateSearchableAttributes(&tt.args.searchableAttribute)
1085+
require.NoError(t, err)
1086+
testWaitForTask(t, i, gotTask)
1087+
}
1088+
1089+
if tt.args.sortableAttribute != nil {
1090+
gotTask, err := i.UpdateSortableAttributes(&tt.args.sortableAttribute)
1091+
require.NoError(t, err)
1092+
testWaitForTask(t, i, gotTask)
1093+
}
1094+
1095+
got, err := i.Search(tt.args.query, &tt.args.request)
1096+
1097+
require.NoError(t, err)
1098+
require.Equal(t, len(tt.want.Hits), len(got.Hits))
1099+
for len := range got.Hits {
1100+
require.Equal(t, tt.want.Hits[len], got.Hits[len])
1101+
}
1102+
require.Equal(t, tt.want.NbHits, got.NbHits)
1103+
require.Equal(t, tt.want.Offset, got.Offset)
1104+
require.Equal(t, tt.want.Limit, got.Limit)
1105+
require.Equal(t, tt.want.ExhaustiveNbHits, got.ExhaustiveNbHits)
1106+
require.Equal(t, tt.want.FacetsDistribution, got.FacetsDistribution)
1107+
require.Equal(t, tt.want.ExhaustiveFacetsCount, got.ExhaustiveFacetsCount)
1108+
})
1109+
}
1110+
}

main_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,33 @@ func SetUpBasicIndex(indexUID string) {
133133
}
134134
}
135135

136+
func SetUpIndexWithNestedFields(indexUID string) {
137+
client := NewClient(ClientConfig{
138+
Host: "http://localhost:7700",
139+
APIKey: masterKey,
140+
})
141+
index := client.Index(indexUID)
142+
143+
documents := []map[string]interface{}{
144+
{"id": 1, "title": "Pride and Prejudice", "info": map[string]interface{}{"comment": "A great book", "reviewNb": 50}},
145+
{"id": 2, "title": "Le Petit Prince", "info": map[string]interface{}{"comment": "A french book", "reviewNb": 600}},
146+
{"id": 3, "title": "Le Rouge et le Noir", "info": map[string]interface{}{"comment": "Another french book", "reviewNb": 700}},
147+
{"id": 4, "title": "Alice In Wonderland", "comment": "A weird book", "info": map[string]interface{}{"comment": "A weird book", "reviewNb": 800}},
148+
{"id": 5, "title": "The Hobbit", "info": map[string]interface{}{"comment": "An awesome book", "reviewNb": 900}},
149+
{"id": 6, "title": "Harry Potter and the Half-Blood Prince", "info": map[string]interface{}{"comment": "The best book", "reviewNb": 1000}},
150+
{"id": 7, "title": "The Hitchhiker's Guide to the Galaxy"},
151+
}
152+
task, err := index.AddDocuments(documents)
153+
if err != nil {
154+
fmt.Println(err)
155+
os.Exit(1)
156+
}
157+
finalTask, _ := index.WaitForTask(task)
158+
if finalTask.Status != "succeeded" {
159+
os.Exit(1)
160+
}
161+
}
162+
136163
func SetUpIndexForFaceting() {
137164
client := NewClient(ClientConfig{
138165
Host: "http://localhost:7700",

version_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package meilisearch
22

33
import (
4-
"testing"
54
"fmt"
65
"regexp"
6+
"testing"
77

8-
"github.com/stretchr/testify/require"
98
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
1010
)
1111

1212
func TestVersion_GetQualifiedVersion(t *testing.T) {

0 commit comments

Comments
 (0)