18
18
19
19
namespace NRedisStack . Tests ;
20
20
21
- public class ExaplesTests : AbstractNRedisStackTest , IDisposable
21
+ public class ExampleTests : AbstractNRedisStackTest , IDisposable
22
22
{
23
23
private readonly ITestOutputHelper testOutputHelper ;
24
24
Mock < IDatabase > _mock = new Mock < IDatabase > ( ) ;
25
25
private readonly string key = "EXAMPLES_TESTS" ;
26
- public ExaplesTests ( RedisFixture redisFixture , ITestOutputHelper testOutputHelper ) : base ( redisFixture )
26
+ public ExampleTests ( RedisFixture redisFixture , ITestOutputHelper testOutputHelper ) : base ( redisFixture )
27
27
{
28
28
this . testOutputHelper = testOutputHelper ;
29
29
}
@@ -41,7 +41,7 @@ public void HSETandSearch()
41
41
42
42
// Get a reference to the database and for search commands:
43
43
// var db = redis.GetDatabase();
44
- var db = redisFixture . Redis . GetDatabase ( ) ;
44
+ var db = redisFixture . Redis . GetDatabase ( ) ;
45
45
db . Execute ( "FLUSHALL" ) ;
46
46
var ft = db . FT ( ) ;
47
47
@@ -186,7 +186,7 @@ public async Task PipelineWithAsync()
186
186
187
187
// Get a reference to the database
188
188
// var db = redis.GetDatabase();
189
- var db = redisFixture . Redis . GetDatabase ( ) ;
189
+ var db = redisFixture . Redis . GetDatabase ( ) ;
190
190
db . Execute ( "FLUSHALL" ) ;
191
191
// Setup pipeline connection
192
192
@@ -286,7 +286,7 @@ public void TestJsonConvert()
286
286
// ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
287
287
// IDatabase db = redis.GetDatabase();
288
288
289
- var db = redisFixture . Redis . GetDatabase ( ) ;
289
+ var db = redisFixture . Redis . GetDatabase ( ) ;
290
290
db . Execute ( "FLUSHALL" ) ;
291
291
ISearchCommands ft = db . FT ( ) ;
292
292
IJsonCommands json = db . JSON ( ) ;
@@ -578,7 +578,7 @@ public void BasicJsonExamplesTest()
578
578
{
579
579
// ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
580
580
// IDatabase db = redis.GetDatabase();
581
- var db = redisFixture . Redis . GetDatabase ( ) ;
581
+ var db = redisFixture . Redis . GetDatabase ( ) ;
582
582
db . Execute ( "FLUSHALL" ) ;
583
583
IJsonCommands json = db . JSON ( ) ;
584
584
@@ -833,7 +833,7 @@ public void AdvancedJsonExamplesTest()
833
833
{
834
834
// ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
835
835
// IDatabase db = redis.GetDatabase();
836
- var db = redisFixture . Redis . GetDatabase ( ) ;
836
+ var db = redisFixture . Redis . GetDatabase ( ) ;
837
837
db . Execute ( "FLUSHALL" ) ;
838
838
IJsonCommands json = db . JSON ( ) ;
839
839
@@ -967,7 +967,7 @@ public void BasicQueryOperationsTest()
967
967
{
968
968
// ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
969
969
// IDatabase db = redis.GetDatabase();
970
- var db = redisFixture . Redis . GetDatabase ( ) ;
970
+ var db = redisFixture . Redis . GetDatabase ( ) ;
971
971
db . Execute ( "FLUSHALL" ) ;
972
972
IJsonCommands json = db . JSON ( ) ;
973
973
ISearchCommands ft = db . FT ( ) ;
@@ -1127,22 +1127,39 @@ public void AdvancedQueryOperationsTest()
1127
1127
{
1128
1128
// ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
1129
1129
// IDatabase db = redis.GetDatabase();
1130
- var db = redisFixture . Redis . GetDatabase ( ) ;
1130
+ var db = redisFixture . Redis . GetDatabase ( ) ;
1131
1131
db . Execute ( "FLUSHALL" ) ;
1132
1132
IJsonCommands json = db . JSON ( ) ;
1133
1133
ISearchCommands ft = db . FT ( ) ;
1134
1134
1135
1135
// Vector Similarity Search (VSS)
1136
1136
// Data load:
1137
- db . HashSet ( "vec:1" , "vector" , ( new float [ ] { 1f , 1f , 1f , 1f } ) . SelectMany ( BitConverter . GetBytes ) . ToArray ( ) ) ;
1138
- db . HashSet ( "vec:2" , "vector" , ( new float [ ] { 2f , 2f , 2f , 2f } ) . SelectMany ( BitConverter . GetBytes ) . ToArray ( ) ) ;
1139
- db . HashSet ( "vec:3" , "vector" , ( new float [ ] { 3f , 3f , 3f , 3f } ) . SelectMany ( BitConverter . GetBytes ) . ToArray ( ) ) ;
1140
- db . HashSet ( "vec:5" , "vector" , ( new float [ ] { 4f , 4f , 4f , 4f } ) . SelectMany ( BitConverter . GetBytes ) . ToArray ( ) ) ;
1137
+ db . HashSet ( "vec:1" , new HashEntry [ ]
1138
+ {
1139
+ new ( "vector" , ( new float [ ] { 1f , 1f , 1f , 1f } ) . SelectMany ( BitConverter . GetBytes ) . ToArray ( ) ) ,
1140
+ new ( "tag" , "A" )
1141
+ } ) ;
1142
+ db . HashSet ( "vec:2" , new HashEntry [ ]
1143
+ {
1144
+ new ( "vector" , ( new float [ ] { 2f , 2f , 2f , 2f } ) . SelectMany ( BitConverter . GetBytes ) . ToArray ( ) ) ,
1145
+ new ( "tag" , "A" )
1146
+ } ) ;
1147
+ db . HashSet ( "vec:3" , new HashEntry [ ]
1148
+ {
1149
+ new ( "vector" , ( new float [ ] { 3f , 3f , 3f , 3f } ) . SelectMany ( BitConverter . GetBytes ) . ToArray ( ) ) ,
1150
+ new ( "tag" , "B" )
1151
+ } ) ;
1152
+ db . HashSet ( "vec:4" , new HashEntry [ ]
1153
+ {
1154
+ new ( "vector" , ( new float [ ] { 4f , 4f , 4f , 4f } ) . SelectMany ( BitConverter . GetBytes ) . ToArray ( ) ) ,
1155
+ new ( "tag" , "A" )
1156
+ } ) ;
1141
1157
1142
1158
// Index creation:
1143
1159
try { ft . DropIndex ( "vss_idx" ) ; } catch { } ;
1144
1160
Assert . True ( ft . Create ( "vss_idx" , new FTCreateParams ( ) . On ( IndexDataType . HASH ) . Prefix ( "vec:" ) ,
1145
1161
new Schema ( )
1162
+ . AddTagField ( "tag" )
1146
1163
. AddVectorField ( "vector" , VectorField . VectorAlgo . FLAT ,
1147
1164
new Dictionary < string , object > ( )
1148
1165
{
@@ -1156,7 +1173,7 @@ public void AdvancedQueryOperationsTest()
1156
1173
Thread . Sleep ( 2000 ) ;
1157
1174
1158
1175
// Search:
1159
- float [ ] vec = new [ ] { 2f , 2f , 3f , 3f } ;
1176
+ float [ ] vec = new [ ] { 2f , 3f , 3f , 3f } ;
1160
1177
var res = ft . Search ( "vss_idx" ,
1161
1178
new Query ( "*=>[KNN 2 @vector $query_vec]" )
1162
1179
. AddParam ( "query_vec" , vec . SelectMany ( BitConverter . GetBytes ) . ToArray ( ) )
@@ -1176,12 +1193,37 @@ public void AdvancedQueryOperationsTest()
1176
1193
1177
1194
HashSet < string > expectedResSet = new HashSet < string > ( )
1178
1195
{
1179
- "id: vec:2 , score: 2 " ,
1180
- "id: vec:3 , score: 2 " ,
1196
+ "id: vec:3 , score: 1 " ,
1197
+ "id: vec:2 , score: 3 " ,
1181
1198
} ;
1182
1199
1183
1200
Assert . Equal ( expectedResSet , resSet ) ;
1184
1201
1202
+ // hybrid query - search only documents with tag A:
1203
+ res = ft . Search ( "vss_idx" ,
1204
+ new Query ( "@tag:{A}=>[KNN 2 @vector $query_vec]" )
1205
+ . AddParam ( "query_vec" , vec . SelectMany ( BitConverter . GetBytes ) . ToArray ( ) )
1206
+ . SetSortBy ( "__vector_score" )
1207
+ . Dialect ( 2 ) ) ;
1208
+
1209
+ resSet . Clear ( ) ;
1210
+ foreach ( var doc in res . Documents )
1211
+ {
1212
+ foreach ( var item in doc . GetProperties ( ) )
1213
+ {
1214
+ if ( item . Key == "__vector_score" )
1215
+ {
1216
+ resSet . Add ( $ "id: { doc . Id } , score: { item . Value } ") ;
1217
+ }
1218
+ }
1219
+ }
1220
+
1221
+ expectedResSet = new HashSet < string > ( )
1222
+ {
1223
+ "id: vec:2, score: 3" ,
1224
+ "id: vec:4, score: 7" ,
1225
+ } ;
1226
+
1185
1227
//Advanced Search Queries:
1186
1228
// data load:
1187
1229
json . Set ( "warehouse:1" , "$" , new
@@ -1348,4 +1390,4 @@ private static void SortAndCompare(List<string> expectedList, List<string> res)
1348
1390
Assert . Equal ( expectedList [ i ] , res [ i ] . ToString ( ) ) ;
1349
1391
}
1350
1392
}
1351
- }
1393
+ }
0 commit comments