@@ -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+ }
0 commit comments