/
test_vecsim.py
2086 lines (1751 loc) · 131 KB
/
test_vecsim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import random
from RLTest import Env
from common import *
from includes import *
from random import randrange
'''************* Helper methods for vecsim tests ************'''
EPSILON = 1e-8
# Helper method for comparing expected vs. results of KNN query, where the only
# returned field except for the doc id is the vector distance
def assert_query_results(env, expected_res, actual_res, error_msg='', data_type='FLOAT32'):
# Assert that number of returned results from the query is as expected
env.assertEqual(expected_res[0], actual_res[0], depth=1, message=error_msg)
for i in range(1, len(expected_res), 2):
# For each result, assert its id and its distance (use float equality)
env.assertEqual(expected_res[i], actual_res[i], depth=1, message=error_msg)
if data_type == 'FLOAT32':
env.assertAlmostEqual(expected_res[i+1][1], float(actual_res[i+1][1]), 1E-6, depth=1, message=error_msg)
else: # data type is float64, expect higher precision
env.assertAlmostEqual(expected_res[i+1][1], float(actual_res[i+1][1]), 1E-9, depth=1, message=error_msg)
def load_vectors_to_redis(env, n_vec, query_vec_index, vec_size, data_type='FLOAT32'):
conn = getConnectionByEnv(env)
np.random.seed(10)
for i in range(n_vec):
vector = create_np_array_typed(np.random.rand(vec_size), data_type)
if i == query_vec_index:
query_vec = vector
conn.execute_command('HSET', i, 'vector', vector.tobytes())
return query_vec
def get_vecsim_memory(env, index_key, field_name):
return float(to_dict(env.cmd("FT.DEBUG", "VECSIM_INFO", index_key, field_name))["MEMORY"])/0x100000
def get_vecsim_index_size(env, index_key, field_name):
return int(to_dict(env.cmd("FT.DEBUG", "VECSIM_INFO", index_key, field_name))["INDEX_SIZE"])
def load_vectors_with_texts_into_redis(con, vector_field, dim, num_vectors, data_type='FLOAT32'):
id_vec_list = []
p = con.pipeline(transaction=False)
for i in range(1, num_vectors+1):
vector = create_np_array_typed([i]*dim, data_type)
p.execute_command('HSET', i, vector_field, vector.tobytes(), 't', 'text value')
id_vec_list.append((i, vector))
p.execute()
return id_vec_list
def execute_hybrid_query(env, query_string, query_data, non_vector_field, sort_by_vector=True, sort_by_non_vector_field=False,
hybrid_mode='HYBRID_BATCHES'):
if sort_by_vector:
ret = env.expect('FT.SEARCH', 'idx', query_string,
'SORTBY', '__v_score',
'PARAMS', 2, 'vec_param', query_data.tobytes(),
'RETURN', 2, '__v_score', non_vector_field, 'LIMIT', 0, 10)
else:
if sort_by_non_vector_field:
ret = env.expect('FT.SEARCH', 'idx', query_string, 'WITHSCORES',
'SORTBY', non_vector_field,
'PARAMS', 2, 'vec_param', query_data.tobytes(),
'RETURN', 2, non_vector_field, '__v_score', 'LIMIT', 0, 10)
else:
ret = env.expect('FT.SEARCH', 'idx', query_string, 'WITHSCORES',
'PARAMS', 2, 'vec_param', query_data.tobytes(),
'RETURN', 2, non_vector_field, '__v_score', 'LIMIT', 0, 10)
# in cluster mode, we send `_FT.DEBUG' to the local shard.
prefix = '_' if env.isCluster() else ''
env.assertEqual(to_dict(env.cmd(prefix+"FT.DEBUG", "VECSIM_INFO", "idx", "v"))['LAST_SEARCH_MODE'], hybrid_mode, depth=1)
return ret
'''******************* vecsim tests *****************************'''
def test_sanity_cosine():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
conn = getConnectionByEnv(env)
index_types = ['FLAT', 'HNSW']
data_types = ['FLOAT32', 'FLOAT64']
score_field_syntaxs = ['AS dist]', ']=>{$yield_distance_as:dist}']
for index_type in index_types:
for data_type in data_types:
for i, score_field_syntax in enumerate(score_field_syntaxs):
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', index_type, '6', 'TYPE', data_type,
'DIM', '2', 'DISTANCE_METRIC', 'COSINE').ok()
conn.execute_command('HSET', 'a', 'v', create_np_array_typed([0.1, 0.1], data_type).tobytes())
conn.execute_command('HSET', 'b', 'v', create_np_array_typed([0.1, 0.2], data_type).tobytes())
conn.execute_command('HSET', 'c', 'v', create_np_array_typed([0.1, 0.3], data_type).tobytes())
conn.execute_command('HSET', 'd', 'v', create_np_array_typed([0.1, 0.4], data_type).tobytes())
query_vec = create_np_array_typed([0.1, 0.1], data_type)
# Compute the expected distances from the query vector using scipy.spatial
expected_res = [4, 'a', ['dist', spatial.distance.cosine(np.array([0.1, 0.1]), query_vec)],
'b', ['dist', spatial.distance.cosine(np.array([0.1, 0.2]), query_vec)],
'c', ['dist', spatial.distance.cosine(np.array([0.1, 0.3]), query_vec)],
'd', ['dist', spatial.distance.cosine(np.array([0.1, 0.4]), query_vec)]]
actual_res = env.expect('FT.SEARCH', 'idx', f'*=>[KNN 4 @v $blob {score_field_syntax}', 'PARAMS', '2',
'blob', query_vec.tobytes(), 'SORTBY', 'dist', 'RETURN', '1', 'dist').res
assert_query_results(env, expected_res, actual_res, error_msg=f"{index_type, data_type}", data_type=data_type)
if i==1: # range query can use only query attributes as score field syntax
range_dist = spatial.distance.cosine(np.array([0.1, 0.4]), query_vec) + EPSILON
actual_res = env.expect('FT.SEARCH', 'idx', f'@v:[VECTOR_RANGE {range_dist} $blob {score_field_syntax}', 'PARAMS', '2',
'blob', query_vec.tobytes(), 'SORTBY', 'dist', 'RETURN', '1', 'dist').res
assert_query_results(env, expected_res, actual_res, error_msg=f"{index_type, data_type}", data_type=data_type)
# Rerun with a different query vector
query_vec = create_np_array_typed([0.1, 0.2], data_type)
expected_res = [4, 'b', ['dist', spatial.distance.cosine(np.array([0.1, 0.2]), query_vec)],
'c', ['dist', spatial.distance.cosine(np.array([0.1, 0.3]), query_vec)],
'd', ['dist', spatial.distance.cosine(np.array([0.1, 0.4]), query_vec)],
'a', ['dist', spatial.distance.cosine(np.array([0.1, 0.1]), query_vec)]]
actual_res = env.expect('FT.SEARCH', 'idx', f'*=>[KNN 4 @v $blob {score_field_syntax}', 'PARAMS', '2',
'blob', query_vec.tobytes(), 'SORTBY', 'dist', 'RETURN', '1', 'dist').res
assert_query_results(env, expected_res, actual_res, error_msg=f"{index_type, data_type}", data_type=data_type)
if i==1: # range query can use only query attributes as score field syntax
range_dist = spatial.distance.cosine(np.array([0.1, 0.1]), query_vec) + EPSILON
actual_res = env.expect('FT.SEARCH', 'idx', f'@v:[VECTOR_RANGE {range_dist} $blob {score_field_syntax}', 'PARAMS', '2',
'blob', query_vec.tobytes(), 'SORTBY', 'dist', 'RETURN', '1', 'dist').res
assert_query_results(env, expected_res, actual_res, error_msg=f"{index_type, data_type}", data_type=data_type)
# Delete one vector and search again
conn.execute_command('DEL', 'b')
# Expect to get only 3 results (the same as before but without 'b')
expected_res = [3, 'c', ['dist', spatial.distance.cosine(np.array([0.1, 0.3]), query_vec)],
'd', ['dist', spatial.distance.cosine(np.array([0.1, 0.4]), query_vec)],
'a', ['dist', spatial.distance.cosine(np.array([0.1, 0.1]), query_vec)]]
actual_res = env.expect('FT.SEARCH', 'idx', '*=>[KNN 4 @v $blob AS dist]', 'PARAMS', '2',
'blob', query_vec.tobytes(), 'SORTBY', 'dist', 'RETURN', '1', 'dist').res
assert_query_results(env, expected_res, actual_res, error_msg=f"{index_type, data_type}", data_type=data_type)
if i==1:
# Test range query
range_dist = spatial.distance.cosine(np.array([0.1, 0.1]), query_vec) + EPSILON
actual_res = env.expect('FT.SEARCH', 'idx', f'@v:[VECTOR_RANGE {range_dist} $blob]=>{{$yield_distance_as: dist}}',
'PARAMS', '2', 'blob', query_vec.tobytes(), 'SORTBY', 'dist', 'RETURN', '1', 'dist').res
assert_query_results(env, expected_res, actual_res, error_msg=f"{index_type, data_type}", data_type=data_type)
conn.execute_command('FT.DROPINDEX', 'idx', 'DD')
def test_sanity_l2():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
conn = getConnectionByEnv(env)
index_types = ['FLAT', 'HNSW']
data_types = ['FLOAT32', 'FLOAT64']
for index_type in index_types:
for data_type in data_types:
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', index_type, '6', 'TYPE', data_type,
'DIM', '2', 'DISTANCE_METRIC', 'L2').ok()
conn.execute_command('HSET', 'a', 'v', create_np_array_typed([0.1, 0.1], data_type).tobytes())
conn.execute_command('HSET', 'b', 'v', create_np_array_typed([0.1, 0.2], data_type).tobytes())
conn.execute_command('HSET', 'c', 'v', create_np_array_typed([0.1, 0.3], data_type).tobytes())
conn.execute_command('HSET', 'd', 'v', create_np_array_typed([0.1, 0.4], data_type).tobytes())
query_vec = create_np_array_typed([0.1, 0.1], data_type)
# Compute the expected distances from the query vector using scipy.spatial
expected_res = [4, 'a', ['dist', spatial.distance.sqeuclidean(np.array([0.1, 0.1]), query_vec)],
'b', ['dist', spatial.distance.sqeuclidean(np.array([0.1, 0.2]), query_vec)],
'c', ['dist', spatial.distance.sqeuclidean(np.array([0.1, 0.3]), query_vec)],
'd', ['dist', spatial.distance.sqeuclidean(np.array([0.1, 0.4]), query_vec)]]
actual_res = env.expect('FT.SEARCH', 'idx', '*=>[KNN 4 @v $blob]=>{$yield_distance_as: dist}', 'PARAMS', '2',
'blob', query_vec.tobytes(), 'SORTBY', 'dist', 'RETURN', '1', 'dist').res
assert_query_results(env, expected_res, actual_res, error_msg=f"{index_type, data_type}", data_type=data_type)
# Test range query
range_dist = spatial.distance.sqeuclidean(np.array([0.1, 0.4]), query_vec) + EPSILON
actual_res = env.expect('FT.SEARCH', 'idx', f'@v:[VECTOR_RANGE {range_dist} $blob]=>{{$yield_distance_as: dist}}',
'PARAMS', '2', 'blob', query_vec.tobytes(), 'SORTBY', 'dist', 'RETURN', '1', 'dist').res
assert_query_results(env, expected_res, actual_res, error_msg=f"{index_type, data_type}", data_type=data_type)
# Rerun with a different query vector
query_vec = create_np_array_typed([0.1, 0.19], data_type)
expected_res = [4, 'b', ['dist', spatial.distance.sqeuclidean(np.array([0.1, 0.2]), query_vec)],
'a', ['dist', spatial.distance.sqeuclidean(np.array([0.1, 0.1]), query_vec)],
'c', ['dist', spatial.distance.sqeuclidean(np.array([0.1, 0.3]), query_vec)],
'd', ['dist', spatial.distance.sqeuclidean(np.array([0.1, 0.4]), query_vec)]]
actual_res = env.expect('FT.SEARCH', 'idx', '*=>[KNN 4 @v $blob AS dist]', 'PARAMS', '2',
'blob', query_vec.tobytes(), 'SORTBY', 'dist', 'RETURN', '1', 'dist').res
assert_query_results(env, expected_res, actual_res, error_msg=f"{index_type, data_type}", data_type=data_type)
# Test range query
range_dist = spatial.distance.sqeuclidean(np.array([0.1, 0.4]), query_vec) + EPSILON
actual_res = env.expect('FT.SEARCH', 'idx', f'@v:[VECTOR_RANGE {range_dist} $blob]=>{{$yield_distance_as: dist}}',
'PARAMS', '2', 'blob', query_vec.tobytes(), 'SORTBY', 'dist', 'RETURN', '1', 'dist').res
assert_query_results(env, expected_res, actual_res, error_msg=f"{index_type, data_type}", data_type=data_type)
# Delete one vector and search again
conn.execute_command('DEL', 'b')
# Expect to get only 3 results (the same as before but without 'b')
expected_res = [3, 'a', ['dist', spatial.distance.sqeuclidean(np.array([0.1, 0.1]), query_vec)],
'c', ['dist', spatial.distance.sqeuclidean(np.array([0.1, 0.3]), query_vec)],
'd', ['dist', spatial.distance.sqeuclidean(np.array([0.1, 0.4]), query_vec)]]
actual_res = env.expect('FT.SEARCH', 'idx', '*=>[KNN 4 @v $blob AS dist]', 'PARAMS', '2',
'blob', query_vec.tobytes(), 'SORTBY', 'dist', 'RETURN', '1', 'dist').res
assert_query_results(env, expected_res, actual_res, error_msg=f"{index_type, data_type}", data_type=data_type)
# Test range query
range_dist = spatial.distance.sqeuclidean(np.array([0.1, 0.4]), query_vec) + EPSILON
actual_res = env.expect('FT.SEARCH', 'idx', f'@v:[VECTOR_RANGE {range_dist} $blob]=>{{$yield_distance_as: dist}}',
'PARAMS', '2', 'blob', query_vec.tobytes(), 'SORTBY', 'dist', 'RETURN', '1', 'dist').res
assert_query_results(env, expected_res, actual_res, error_msg=f"{index_type, data_type}", data_type=data_type)
conn.execute_command('FT.DROPINDEX', 'idx', 'DD')
def test_del_reuse():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
def del_insert(env):
conn = getConnectionByEnv(env)
conn.execute_command('DEL', 'a')
conn.execute_command('DEL', 'b')
conn.execute_command('DEL', 'c')
conn.execute_command('DEL', 'd')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 4 @v $b]', 'PARAMS', '2', 'b', 'abcdefgh').equal([0])
res = [''.join(random.choice(str(x).lower()) for x in range(8)),
''.join(random.choice(str(x).lower()) for x in range(8)),
''.join(random.choice(str(x).lower()) for x in range(8)),
''.join(random.choice(str(x).lower()) for x in range(8))]
conn.execute_command('HSET', 'a', 'v', res[0])
conn.execute_command('HSET', 'b', 'v', res[1])
conn.execute_command('HSET', 'c', 'v', res[2])
conn.execute_command('HSET', 'd', 'v', res[3])
return res
# test start
conn = getConnectionByEnv(env)
conn.execute_command('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '6', 'TYPE', 'FLOAT32', 'DIM', '2','DISTANCE_METRIC', 'L2')
vecs = del_insert(env)
res = [4, 'a', ['v', vecs[0]], 'b', ['v', vecs[1]], 'c', ['v', vecs[2]], 'd', ['v', vecs[3]]]
env.expect('FT.SEARCH', 'idx', '*=>[KNN 4 @v $b]', 'PARAMS', '2', 'b', 'abcdefgh', 'RETURN', '1', 'v').equal(res)
vecs = del_insert(env)
res = [4, 'a', ['v', vecs[0]], 'b', ['v', vecs[1]], 'c', ['v', vecs[2]], 'd', ['v', vecs[3]]]
env.expect('FT.SEARCH', 'idx', '*=>[KNN 4 @v $b]', 'PARAMS', '2', 'b', 'abcdefgh', 'RETURN', '1', 'v').equal(res)
vecs = del_insert(env)
res = [4, 'a', ['v', vecs[0]], 'b', ['v', vecs[1]], 'c', ['v', vecs[2]], 'd', ['v', vecs[3]]]
env.expect('FT.SEARCH', 'idx', '*=>[KNN 4 @v $b]', 'PARAMS', '2', 'b', 'abcdefgh', 'RETURN', '1', 'v').equal(res)
# test for issue https://github.com/RediSearch/RediSearch/pull/2705
def test_update_with_bad_value():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
conn = getConnectionByEnv(env)
conn.execute_command('FT.CREATE', 'idx', 'ON', 'JSON',
'SCHEMA', '$.v', 'AS', 'vec', 'VECTOR', 'FLAT', '6', 'TYPE', 'FLOAT32', 'DIM', '2','DISTANCE_METRIC', 'L2')
conn.execute_command('FT.CREATE', 'idx2',
'SCHEMA', 'vec', 'VECTOR', 'FLAT', '6', 'TYPE', 'FLOAT32', 'DIM', '2','DISTANCE_METRIC', 'L2')
res = [1, 'doc:1', ['$', '{"v":[1,3]}']]
# Add doc contains a vector to the index
env.assertOk(conn.execute_command('JSON.SET', 'doc:1', '$', '{"v":[1,2]}'))
# Override with bad vector value (wrong blob size)
env.assertEqual(conn.execute_command('JSON.ARRINSERT', 'doc:1', '$.v', '2', '3'), [3])
# Override again with legal vector value
env.assertEqual(conn.execute_command('JSON.ARRPOP', 'doc:1', '$.v', '1'), ['2'])
env.assertEqual(conn.execute_command('JSON.GET', 'doc:1', '$.v[*]'), '[1,3]')
env.assertEqual(conn.execute_command('JSON.ARRLEN', 'doc:1', '$.v'), [2])
waitForIndex(env, 'idx')
# before the issue fix, the second query will result in empty result, as the first vector value was not deleted when
# its value was override with a bad value
env.expect('FT.SEARCH', 'idx', '*').equal(res)
env.expect('FT.SEARCH', 'idx', '*=>[KNN 1 @vec $B]', 'PARAMS', '2', 'B', '????????', 'RETURN', '1', '$').equal(res)
res = [1, 'h1', ['vec', '????>>>>']]
# Add doc contains a vector to the index
env.assertEqual(conn.execute_command('HSET', 'h1', 'vec', '????????'), 1)
# Override with bad vector value (wrong blob size)
env.assertEqual(conn.execute_command('HSET', 'h1', 'vec', 'bad-val'), 0)
# Override again with legal vector value
env.assertEqual(conn.execute_command('HSET', 'h1', 'vec', '????>>>>'), 0)
waitForIndex(env, 'idx2')
# before the issue fix, the second query will result in empty result, as the first vector value was not deleted when
# its value was override with a bad value
env.expect('FT.SEARCH', 'idx2', '*').equal(res)
env.expect('FT.SEARCH', 'idx2', '*=>[KNN 1 @vec $B]', 'PARAMS', '2', 'B', '????????', 'RETURN', '1', 'vec').equal(res)
def test_create():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
env.skipOnCluster()
conn = getConnectionByEnv(env)
# Test for INT32, INT64 as well when support for these types is added.
for data_type in VECSIM_DATA_TYPES:
conn.execute_command('FT.CREATE', 'idx1', 'SCHEMA', 'v_HNSW', 'VECTOR', 'HNSW', '14', 'TYPE', data_type,
'DIM', '1024', 'DISTANCE_METRIC', 'COSINE', 'INITIAL_CAP', '10', 'M', '16',
'EF_CONSTRUCTION', '200', 'EF_RUNTIME', '10')
conn.execute_command('FT.CREATE', 'idx2', 'SCHEMA', 'v_FLAT', 'VECTOR', 'FLAT', '8', 'TYPE', data_type,
'DIM', '1024', 'DISTANCE_METRIC', 'L2', 'INITIAL_CAP', '10')
for _ in env.retry_with_rdb_reload():
info = [['identifier', 'v_HNSW', 'attribute', 'v_HNSW', 'type', 'VECTOR']]
assertInfoField(env, 'idx1', 'attributes', info)
info_data_HNSW = conn.execute_command("FT.DEBUG", "VECSIM_INFO", "idx1", "v_HNSW")
env.assertEqual(info_data_HNSW[:-5], ['ALGORITHM', 'HNSW', 'TYPE', data_type, 'DIMENSION', 1024, 'METRIC', 'COSINE', 'IS_MULTI_VALUE', 0, 'INDEX_SIZE', 0, 'INDEX_LABEL_COUNT', 0, 'M', 16, 'EF_CONSTRUCTION', 200, 'EF_RUNTIME', 10, 'MAX_LEVEL', -1, 'ENTRYPOINT', -1, 'MEMORY'])
# skip the memory value
env.assertEqual(info_data_HNSW[-4:], ['LAST_SEARCH_MODE', 'EMPTY_MODE', 'EPSILON', '0.01'])
info_data_FLAT = conn.execute_command("FT.DEBUG", "VECSIM_INFO", "idx2", "v_FLAT")
env.assertEqual(info_data_FLAT[:-3], ['ALGORITHM', 'FLAT', 'TYPE', data_type, 'DIMENSION', 1024, 'METRIC', 'L2', 'IS_MULTI_VALUE', 0, 'INDEX_SIZE', 0, 'INDEX_LABEL_COUNT', 0, 'BLOCK_SIZE', 1024, 'MEMORY'])
# skip the memory value
env.assertEqual(info_data_FLAT[-2:], ['LAST_SEARCH_MODE', 'EMPTY_MODE'])
conn.execute_command('FT.DROP', 'idx1')
conn.execute_command('FT.DROP', 'idx2')
def test_create_multiple_vector_fields():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
env.skipOnCluster()
dim = 2
conn = getConnectionByEnv(env)
# Create index with 2 vector fields, where the first is a prefix of the second.
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '6', 'TYPE', 'FLOAT32', 'DIM', dim, 'DISTANCE_METRIC', 'COSINE',
'v_flat', 'VECTOR', 'FLAT', '6', 'TYPE', 'FLOAT32', 'DIM', dim, 'DISTANCE_METRIC', 'L2').ok()
# Validate each index type.
info_data = to_dict(conn.execute_command("FT.DEBUG", "VECSIM_INFO", "idx", "v"))
env.assertEqual(info_data['ALGORITHM'], 'HNSW')
info_data = to_dict(conn.execute_command("FT.DEBUG", "VECSIM_INFO", "idx", "v_flat"))
env.assertEqual(info_data['ALGORITHM'], 'FLAT')
# Insert one vector only to each index, validate it was inserted only to the right index.
conn.execute_command('HSET', 'a', 'v', 'aaaaaaaa')
info_data = to_dict(env.cmd("FT.DEBUG", "VECSIM_INFO", "idx", "v"))
env.assertEqual(info_data['INDEX_SIZE'], 1)
info_data = to_dict(env.cmd("FT.DEBUG", "VECSIM_INFO", "idx", "v_flat"))
env.assertEqual(info_data['INDEX_SIZE'], 0)
conn.execute_command('HSET', 'b', 'v_flat', 'bbbbbbbb')
info_data = to_dict(env.cmd("FT.DEBUG", "VECSIM_INFO", "idx", "v"))
env.assertEqual(info_data['INDEX_SIZE'], 1)
info_data = to_dict(env.cmd("FT.DEBUG", "VECSIM_INFO", "idx", "v_flat"))
env.assertEqual(info_data ['INDEX_SIZE'], 1)
# Search in every index once, validate it was performed only to the right index.
env.cmd('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b]', 'PARAMS', '2', 'b', 'abcdefgh')
info_data = to_dict(env.cmd("FT.DEBUG", "VECSIM_INFO", "idx", "v"))
env.assertEqual(info_data['LAST_SEARCH_MODE'], 'STANDARD_KNN')
info_data = to_dict(env.cmd("FT.DEBUG", "VECSIM_INFO", "idx", "v_flat"))
env.assertEqual(info_data['LAST_SEARCH_MODE'], 'EMPTY_MODE')
def test_create_errors():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
conn = getConnectionByEnv(env)
# missing init args
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR').error().contains('Bad arguments for vector similarity algorithm')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'FLAT').error().contains('Bad arguments for vector similarity number of parameters')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'FLAT', '6').error().contains('Expected 6 parameters but got 0')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'FLAT', '1').error().contains('Bad number of arguments for vector similarity index: got 1 but expected even number')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'FLAT', '2', 'SIZE').error().contains('Bad arguments for algorithm FLAT: SIZE')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'FLAT', '2', 'TYPE').error().contains('Bad arguments for vector similarity FLAT index type')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'FLAT', '4', 'TYPE', 'FLOAT32', 'DIM').error().contains('Bad arguments for vector similarity FLAT index dim')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'FLAT', '4', 'DIM', '1024', 'DISTANCE_METRIC', 'IP').error().contains('Missing mandatory parameter: cannot create FLAT index without specifying TYPE argument')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'FLAT', '4', 'TYPE', 'FLOAT32', 'DISTANCE_METRIC', 'IP').error().contains('Missing mandatory parameter: cannot create FLAT index without specifying DIM argument')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'FLAT', '4', 'TYPE', 'FLOAT32', 'DIM', '1024').error().contains('Missing mandatory parameter: cannot create FLAT index without specifying DISTANCE_METRIC argument')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'FLAT', '6', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC').error().contains('Bad arguments for vector similarity FLAT index metric')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW').error().contains('Bad arguments for vector similarity number of parameters')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '6').error().contains('Expected 6 parameters but got 0')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '1').error().contains('Bad number of arguments for vector similarity index: got 1 but expected even number')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '2', 'SIZE').error().contains('Bad arguments for algorithm HNSW: SIZE')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '2', 'TYPE').error().contains('Bad arguments for vector similarity HNSW index type')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '4', 'TYPE', 'FLOAT32', 'DIM').error().contains('Bad arguments for vector similarity HNSW index dim')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '4', 'DIM', '1024', 'DISTANCE_METRIC', 'IP').error().contains('Missing mandatory parameter: cannot create HNSW index without specifying TYPE argument')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '4', 'TYPE', 'FLOAT32', 'DISTANCE_METRIC', 'IP').error().contains('Missing mandatory parameter: cannot create HNSW index without specifying DIM argument')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '4', 'TYPE', 'FLOAT32', 'DIM', '1024').error().contains('Missing mandatory parameter: cannot create HNSW index without specifying DISTANCE_METRIC argument')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '6', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC').error().contains('Bad arguments for vector similarity HNSW index metric')
# invalid init args
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '6', 'TYPE', 'DOUBLE', 'DIM', '1024', 'DISTANCE_METRIC', 'IP').error().contains('Bad arguments for vector similarity HNSW index type')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '6', 'TYPE', 'FLOAT32', 'DIM', 'str', 'DISTANCE_METRIC', 'IP').error().contains('Bad arguments for vector similarity HNSW index dim')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '6', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC', 'REDIS').error().contains('Bad arguments for vector similarity HNSW index metric')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'REDIS', '6', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC', 'IP').error().contains('Bad arguments for vector similarity algorithm')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'FLAT', '10', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC', 'IP', 'INITIAL_CAP', 'str', 'BLOCK_SIZE', '16') \
.error().contains('Bad arguments for vector similarity FLAT index initial cap')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'FLAT', '10', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC', 'IP', 'INITIAL_CAP', '10', 'BLOCK_SIZE', 'str') \
.error().contains('Bad arguments for vector similarity FLAT index blocksize')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '12', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC', 'IP', 'INITIAL_CAP', 'str', 'M', '16', 'EF_CONSTRUCTION', '200') \
.error().contains('Bad arguments for vector similarity HNSW index initial cap')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '12', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC', 'IP', 'INITIAL_CAP', '100', 'M', 'str', 'EF_CONSTRUCTION', '200') \
.error().contains('Bad arguments for vector similarity HNSW index m')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '12', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC', 'IP', 'INITIAL_CAP', '100', 'M', '16', 'EF_CONSTRUCTION', 'str') \
.error().contains('Bad arguments for vector similarity HNSW index efConstruction')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '12', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC', 'IP', 'INITIAL_CAP', '100', 'M', '16', 'EF_RUNTIME', 'str') \
.error().contains('Bad arguments for vector similarity HNSW index efRuntime')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '12', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC', 'IP', 'INITIAL_CAP', '100', 'M', '16', 'EF_RUNTIME', '14.3') \
.error().contains('Bad arguments for vector similarity HNSW index efRuntime')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '12', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC', 'IP', 'INITIAL_CAP', '100', 'M', '16', 'EF_RUNTIME', '-10') \
.error().contains('Bad arguments for vector similarity HNSW index efRuntime')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '12', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC', 'IP', 'INITIAL_CAP', '100', 'M', '16', 'EPSILON', 'str') \
.error().contains('Bad arguments for vector similarity HNSW index epsilon')
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '12', 'TYPE', 'FLOAT32', 'DIM', '1024', 'DISTANCE_METRIC', 'IP', 'INITIAL_CAP', '100', 'M', '16', 'EPSILON', '-1') \
.error().contains('Bad arguments for vector similarity HNSW index epsilon')
def test_search_errors():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
conn = getConnectionByEnv(env)
conn.execute_command('FT.CREATE', 'idx', 'SCHEMA', 's', 'TEXT', 't', 'TAG', 'SORTABLE',
'v', 'VECTOR', 'HNSW', '12', 'TYPE', VECSIM_DATA_TYPES[0], 'DIM', '2', 'DISTANCE_METRIC', 'COSINE',
'INITIAL_CAP', '10', 'M', '16', 'EF_CONSTRUCTION', '200',
'v_flat', 'VECTOR', 'FLAT', '6', 'TYPE', VECSIM_DATA_TYPES[1], 'DIM', '2', 'DISTANCE_METRIC', 'L2')
conn.execute_command('HSET', 'a', 'v', create_np_array_typed([10]*2, VECSIM_DATA_TYPES[0]).tobytes(),
'v_flat', create_np_array_typed([10]*2, VECSIM_DATA_TYPES[1]).tobytes(), 's', 'hello')
conn.execute_command('HSET', 'b', 'v', create_np_array_typed([20]*2, VECSIM_DATA_TYPES[0]).tobytes(),
'v_flat', create_np_array_typed([20]*2, VECSIM_DATA_TYPES[1]).tobytes(), 's', "hello")
conn.execute_command('HSET', 'c', 'v', create_np_array_typed([30]*2, VECSIM_DATA_TYPES[0]).tobytes(),
'v_flat', create_np_array_typed([30]*2, VECSIM_DATA_TYPES[1]).tobytes(), 's', "hello")
conn.execute_command('HSET', 'd', 'v', create_np_array_typed([40]*2, VECSIM_DATA_TYPES[0]).tobytes(),
'v_flat', create_np_array_typed([40]*2, VECSIM_DATA_TYPES[1]).tobytes(), 's', "hello")
env.expect('FT.SEARCH', 'idx', '*=>[REDIS 4 @v $b]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Syntax error')
env.expect('FT.SEARCH', 'idx', '*=>[KNN str @v $b]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Syntax error')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b]', 'PARAMS', '2', 'b', 'abcdefg').error().contains('Error parsing vector similarity query: query vector blob size (7) does not match index\'s expected size (8).')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b]', 'PARAMS', '2', 'b', 'abcdefghi').error().contains('Error parsing vector similarity query: query vector blob size (9) does not match index\'s expected size (8).')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v_flat $b]', 'PARAMS', '2', 'b', 'abcdefghabcdefg').error().contains('Error parsing vector similarity query: query vector blob size (15) does not match index\'s expected size (16).')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v_flat $b]', 'PARAMS', '2', 'b', 'abcdefghabcdefghi').error().contains('Error parsing vector similarity query: query vector blob size (17) does not match index\'s expected size (16).')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @t $b]', 'PARAMS', '2', 'b', 'abcdefgh').equal([0]) # wrong field
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b AS v]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Property `v` already exists in schema')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b AS s]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Property `s` already exists in schema')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b AS t]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Property `t` already exists in schema')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b AS $score]', 'PARAMS', '4', 'score', 't', 'b', 'abcdefgh').error().contains('Property `t` already exists in schema')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b]=>{$yield_distance_as:v;}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Property `v` already exists in schema')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b EF_RUNTIME -42]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Invalid value was given')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b EF_RUNTIME 2.71828]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Invalid value was given')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b EF_RUNTIME 5 EF_RUNTIME 6]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Parameter was specified twice')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b EF_FUNTIME 30]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Invalid option')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b]=>{$EF_RUNTIME: 5; $EF_RUNTIME: 6;}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Parameter was specified twice')
# ef_runtime is invalid for FLAT index.
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v_flat $b EF_RUNTIME 30]', 'PARAMS', '2', 'b', 'abcdefghabcdefgh').error().contains('Error parsing vector similarity parameters: Invalid option')
# Hybrid attributes with non-hybrid query is invalid.
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b BATCH_SIZE 100]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: hybrid query attributes were sent for a non-hybrid query')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b HYBRID_POLICY ADHOC_BF]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: hybrid query attributes were sent for a non-hybrid query')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b HYBRID_POLICY BATCHES]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: hybrid query attributes were sent for a non-hybrid query')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b HYBRID_POLICY BATCHES BATCH_SIZE 100]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: hybrid query attributes were sent for a non-hybrid query')
# Invalid hybrid attributes.
env.expect('FT.SEARCH', 'idx', '@s:hello=>[KNN 2 @v $b BATCH_SIZE 0]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Invalid value was given')
env.expect('FT.SEARCH', 'idx', '@s:hello=>[KNN 2 @v $b BATCH_SIZE -6]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Invalid value was given')
env.expect('FT.SEARCH', 'idx', '@s:hello=>[KNN 2 @v $b BATCH_SIZE 34_not_a_number]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Invalid value was given')
env.expect('FT.SEARCH', 'idx', '@s:hello=>[KNN 2 @v $b BATCH_SIZE 8 BATCH_SIZE 0]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Parameter was specified twice')
env.expect('FT.SEARCH', 'idx', '@s:hello=>[KNN 2 @v $b HYBRID_POLICY bad_policy]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('invalid hybrid policy was given')
env.expect('FT.SEARCH', 'idx', '@s:hello=>[KNN 2 @v $b]=>{$HYBRID_POLICY: bad_policy;}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('invalid hybrid policy was given')
# Invalid hybrid attributes combinations.
env.expect('FT.SEARCH', 'idx', '@s:hello=>[KNN 2 @v $b HYBRID_POLICY ADHOC_BF BATCH_SIZE 100]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains("Error parsing vector similarity parameters: 'batch size' is irrelevant for 'ADHOC_BF' policy")
env.expect('FT.SEARCH', 'idx', '@s:hello=>[KNN 2 @v $b HYBRID_POLICY ADHOC_BF EF_RUNTIME 100]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains("Error parsing vector similarity parameters: 'EF_RUNTIME' is irrelevant for 'ADHOC_BF' policy")
# Invalid query combination with query attributes syntax.
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b AS score]=>{$yield_distance_as:score2;}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Distance field was specified twice for vector query: score and score2')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b EF_RUNTIME 100]=>{$EF_RUNTIME:200;}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Parameter was specified twice')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b AS $score_1]=>{$yield_distance_as:$score_2;}', 'PARAMS', '6', 'b', 'abcdefgh', 'score_1', 'score_1_val', 'score_2', 'score_2_val').error().contains('Distance field was specified twice for vector query: score_1_val and score_2_val')
env.expect('FT.SEARCH', 'idx', 'hello=>[KNN 2 @v $b AS score]=>{$yield_distance_as:__v_score;}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Distance field was specified twice for vector query: score and __v_score')
env.expect('FT.SEARCH', 'idx', 'hello=>[KNN 2 @v $b AS score]=>{$yield_distance_as:score;}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Distance field was specified twice for vector query: score and score')
# Invalid range queries
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]', 'PARAMS', '2', 'b', 'abcdefg').error().contains('Error parsing vector similarity query: query vector blob size (7) does not match index\'s expected size (8).')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]', 'PARAMS', '2', 'b', 'abcdefghi').error().contains('Error parsing vector similarity query: query vector blob size (9) does not match index\'s expected size (8).')
env.expect('FT.SEARCH', 'idx', '@bad:[vector_range 0.1 $b]', 'PARAMS', '2', 'b', 'abcdefgh').equal([0]) # wrong field
env.expect('FT.SEARCH', 'idx', '@v:[vector_range -1 $b]', 'PARAMS', '2', 'b', 'abcdefgh').error().equal('Error parsing vector similarity query: negative radius (-1) given in a range query')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]=>{$yield_distance_as:t}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Property `t` already exists in schema')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]=>{$yield_distance_as:dist} @v:[vector_range 0.2 $b]=>{$yield_distance_as:dist}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Property `dist` specified more than once')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]=>{$yield_distance_as:$dist}', 'PARAMS', '4', 'b', 'abcdefgh', 'dist', 't').error().contains('Property `t` already exists in schema')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]=>{$EF_RUNTIME:10}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Invalid option')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]=>{$HYBRID_POLICY:BATCHES}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: hybrid query attributes were sent for a non-hybrid query')
# Invalid epsilon param for range queries
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]=>{$EPSILON: -1}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Invalid value was given')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]=>{$EPSILON: 0}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Invalid value was given')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]=>{$EPSILON: not_a_num}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Invalid value was given')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]=>{$EPSILON: 0.1; $EPSILON: 0.2}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Parameter was specified twice')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]=>{$EPSILON: 0.1; $EF_RUNTIME: 20}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: Invalid option')
# epsilon is invalid for non-range queries, and also for flat index.
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b EPSILON 2.71828]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: range query attributes were sent for a non-range query')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b]=>{$EPSILON: 2.71828}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: range query attributes were sent for a non-range query')
env.expect('FT.SEARCH', 'idx', '@s:hello=>[KNN 2 @v $b]=>{$EPSILON: 0.1}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Error parsing vector similarity parameters: range query attributes were sent for a non-range query')
env.expect('FT.SEARCH', 'idx', '@v_flat:[vector_range 0.1 $b]=>{$epsilon:0.1}', 'PARAMS', '2', 'b', 'abcdefghabcdefgh').equal('Error parsing vector similarity parameters: Invalid option')
def test_with_fields():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
conn = getConnectionByEnv(env)
dimension = 128
qty = 100
conn.execute_command('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '6', 'TYPE', 'FLOAT32', 'DIM', dimension, 'DISTANCE_METRIC', 'L2', 't', 'TEXT')
load_vectors_with_texts_into_redis(conn, 'v', dimension, qty)
for _ in env.retry_with_rdb_reload():
waitForIndex(env, 'idx')
query_data = np.float32(np.random.random((1, dimension)))
res = conn.execute_command('FT.SEARCH', 'idx', '*=>[KNN 100 @v $vec_param AS score]',
'SORTBY', 'score', 'PARAMS', 2, 'vec_param', query_data.tobytes(),
'RETURN', 2, 'score', 't')
res_nocontent = conn.execute_command('FT.SEARCH', 'idx', '*=>[KNN 100 @v $vec_param AS score]',
'SORTBY', 'score', 'PARAMS', 2, 'vec_param', query_data.tobytes(),
'NOCONTENT')
dist_range = dimension * qty**2 # distance from query vector to the furthest vector in the index.
res_range = conn.execute_command('FT.SEARCH', 'idx', '@v:[VECTOR_RANGE $r $vec_param]=>{$YIELD_DISTANCE_AS:score}',
'SORTBY', 'score', 'PARAMS', 4, 'vec_param', query_data.tobytes(), 'r', dist_range,
'RETURN', 2, 'score', 't')
env.assertEqual(res[1::2], res_nocontent[1:])
env.assertEqual('t', res[2][2])
# TODO: in coordinator, the first field that indicates the number of total results in 10 when running
# KNN query instead of 100 (but not for range) - should be fixed
env.assertEqual(res[1:], res_range[1:])
def test_memory_info():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
# This test flow adds two vectors and deletes them. The test checks for memory increase in Redis and RediSearch upon insertion and decrease upon delete.
conn = getConnectionByEnv(env)
dimension = 128
index_key = 'idx'
vector_field = 'v'
# Create index. Flat index implementation will free memory when deleting vectors, so it is a good candidate for this test with respect to memory consumption.
conn.execute_command('FT.CREATE', index_key, 'SCHEMA', vector_field, 'VECTOR', 'FLAT', '8', 'TYPE', 'FLOAT32', 'DIM', dimension, 'DISTANCE_METRIC', 'L2', 'BLOCK_SiZE', '1')
# Verify redis memory >= redisearch index memory
if not env.isCluster():
vecsim_memory = get_vecsim_memory(env, index_key=index_key, field_name=vector_field)
redisearch_memory = get_redisearch_vector_index_memory(env, index_key=index_key)
redis_memory = get_redis_memory_in_mb(env)
if not env.isCluster():
env.assertEqual(redisearch_memory, vecsim_memory)
env.assertLessEqual(redisearch_memory, redis_memory)
vector = np.float32(np.random.random((1, dimension)))
# Add vector.
conn.execute_command('HSET', 1, vector_field, vector.tobytes())
# Verify current memory readings > previous memory readings.
cur_redisearch_memory = get_redisearch_vector_index_memory(env, index_key=index_key)
env.assertLessEqual(redisearch_memory, cur_redisearch_memory)
redis_memory = get_redis_memory_in_mb(env)
redisearch_memory = cur_redisearch_memory
# Verify redis memory >= redisearch index memory
env.assertLessEqual(redisearch_memory, redis_memory)
if not env.isCluster():
cur_vecsim_memory = get_vecsim_memory(env, index_key=index_key, field_name=vector_field)
env.assertLessEqual(vecsim_memory, cur_vecsim_memory)
vecsim_memory = cur_vecsim_memory
#verify vecsim memory == redisearch memory
env.assertEqual(cur_vecsim_memory, cur_redisearch_memory)
# Add vector.
conn.execute_command('HSET', 2, vector_field, vector.tobytes())
# Verify current memory readings > previous memory readings.
cur_redisearch_memory = get_redisearch_vector_index_memory(env, index_key=index_key)
env.assertLessEqual(redisearch_memory, cur_redisearch_memory)
redis_memory = get_redis_memory_in_mb(env)
redisearch_memory = cur_redisearch_memory
# Verify redis memory >= redisearch index memory
env.assertLessEqual(redisearch_memory, redis_memory)
if not env.isCluster():
cur_vecsim_memory = get_vecsim_memory(env, index_key=index_key, field_name=vector_field)
env.assertLessEqual(vecsim_memory, cur_vecsim_memory)
vecsim_memory = cur_vecsim_memory
#verify vecsim memory == redisearch memory
env.assertEqual(cur_vecsim_memory, cur_redisearch_memory)
# Delete vector
conn.execute_command('DEL', 2)
# Verify current memory readings < previous memory readings.
cur_redisearch_memory = get_redisearch_vector_index_memory(env, index_key=index_key)
env.assertLessEqual(cur_redisearch_memory, redisearch_memory)
redis_memory = get_redis_memory_in_mb(env)
redisearch_memory = cur_redisearch_memory
# Verify redis memory >= redisearch index memory
env.assertLessEqual(redisearch_memory, redis_memory)
if not env.isCluster():
cur_vecsim_memory = get_vecsim_memory(env, index_key=index_key, field_name=vector_field)
env.assertLessEqual(cur_vecsim_memory, vecsim_memory)
vecsim_memory = cur_vecsim_memory
#verify vecsim memory == redisearch memory
env.assertEqual(cur_vecsim_memory, cur_redisearch_memory)
# Delete vector
conn.execute_command('DEL', 1)
# Verify current memory readings < previous memory readings.
cur_redisearch_memory = get_redisearch_vector_index_memory(env, index_key=index_key)
env.assertLessEqual(cur_redisearch_memory, redisearch_memory)
redis_memory = get_redis_memory_in_mb(env)
redisearch_memory = cur_redisearch_memory
# Verify redis memory >= redisearch index memory
env.assertLessEqual(redisearch_memory, redis_memory)
if not env.isCluster():
cur_vecsim_memory = get_vecsim_memory(env, index_key=index_key, field_name=vector_field)
env.assertLessEqual(cur_vecsim_memory, vecsim_memory)
vecsim_memory = cur_vecsim_memory
#verify vecsim memory == redisearch memory
env.assertEqual(cur_vecsim_memory, cur_redisearch_memory)
def test_hybrid_query_batches_mode_with_text():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
conn = getConnectionByEnv(env)
# Index size is chosen so that batches mode will be selected by the heuristics.
dim = 2
index_size = 6000 * env.shardsCount
for data_type in VECSIM_DATA_TYPES:
env.expect('FT.CREATE', f'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '6', 'TYPE', data_type,
'DIM', dim, 'DISTANCE_METRIC', 'L2', 't', 'TEXT').ok()
load_vectors_with_texts_into_redis(conn, 'v', dim, index_size, data_type)
query_data = create_np_array_typed([index_size] * dim, data_type)
# Expect to find no result (internally, build the child iterator as empty iterator).
env.expect('FT.SEARCH', 'idx', '(nothing)=>[KNN 10 @v $vec_param]', 'PARAMS', 2, 'vec_param', query_data.tobytes()).equal([0])
expected_res = [10]
# Expect to get result in reverse order to the id, starting from the max id in the index.
for i in range(10):
expected_res.append(str(index_size-i))
expected_res.append(['__v_score', str(dim*i**2), 't', 'text value'])
execute_hybrid_query(env, '(@t:(text value))=>[KNN 10 @v $vec_param]', query_data, 't').equal(expected_res)
execute_hybrid_query(env, '(text value)=>[KNN 10 @v $vec_param]', query_data, 't').equal(expected_res)
execute_hybrid_query(env, '("text value")=>[KNN 10 @v $vec_param]', query_data, 't').equal(expected_res)
# Change the text value to 'other' for 20% of the vectors (with ids 5, 10, ..., index_size)
for i in range(1, int(index_size/5) + 1):
vector = create_np_array_typed([5*i] * dim, data_type)
conn.execute_command('HSET', 5*i, 'v', vector.tobytes(), 't', 'other')
# Expect to get only vector that passes the filter (i.e, has "other" in t field)
expected_res = [10]
for i in range(10):
expected_res.append(str(index_size-5*i))
expected_res.append(['__v_score', str(dim*(5*i)**2), 't', 'other'])
execute_hybrid_query(env, '(other)=>[KNN 10 @v $vec_param]', query_data, 't').equal(expected_res)
# Expect empty score for the intersection (disjoint sets of results)
# The hybrid policy changes to ad hoc after the first batch
execute_hybrid_query(env, '(@t:other text)=>[KNN 10 @v $vec_param]', query_data, 't',
hybrid_mode='HYBRID_BATCHES_TO_ADHOC_BF').equal([0])
# Expect the same results as in above ('other AND NOT text')
execute_hybrid_query(env, '(@t:other -text)=>[KNN 10 @v $vec_param]', query_data, 't').equal(expected_res)
# Test with union - expect that all docs will pass the filter.
expected_res = [10]
for i in range(10):
expected_res.append(str(index_size-i))
expected_res.append(['__v_score', str(dim*i**2), 't', 'other' if i % 5 == 0 else 'text value'])
execute_hybrid_query(env, '(@t:other|text)=>[KNN 10 @v $vec_param]', query_data, 't').equal(expected_res)
# Expect for top 10 results from vector search that still has the original text "text value".
expected_res = [10]
res_count = 0
for i in range(13):
# The desired ids are the top 10 ids that do not divide by 5.
if (index_size-i) % 5 == 0:
continue
expected_res.append(str(index_size-i))
expected_res.append(['__v_score', str(dim*i**2), 't', 'text value'])
res_count += 1
if res_count == 10:
break
execute_hybrid_query(env, '(te*)=>[KNN 10 @v $vec_param]', query_data, 't').equal(expected_res)
# This time the fuzzy matching should return only documents with the original 'text value'.
execute_hybrid_query(env, '(%test%)=>[KNN 10 @v $vec_param]', query_data, 't').equal(expected_res)
execute_hybrid_query(env, '(-(@t:other))=>[KNN 10 @v $vec_param]', query_data, 't').equal(expected_res)
# Test with invalid wildcard (less than 2 chars before the wildcard)
env.expect('FT.SEARCH', 'idx', '(t*)=>[KNN 10 @v $vec_param]', 'PARAMS', 2, 'vec_param', query_data.tobytes()).equal([0])
# Intersect valid with invalid iterators in intersection (should return 0 results as well)
env.expect('FT.SEARCH', 'idx', '(@t:t* @t:text)=>[KNN 10 @v $vec_param]', 'PARAMS', 2, 'vec_param', query_data.tobytes()).equal([0])
conn.execute_command('FT.DROPINDEX', 'idx', 'DD')
def test_hybrid_query_batches_mode_with_tags():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
conn = getConnectionByEnv(env)
# Index size is chosen so that batches mode will be selected by the heuristics.
dim = 2
index_size = 6000 * env.shardsCount
for data_type in VECSIM_DATA_TYPES:
conn.execute_command('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '8', 'TYPE', data_type,
'DIM', dim, 'DISTANCE_METRIC', 'L2', 'EF_RUNTIME', 100, 'tags', 'TAG')
p = conn.pipeline(transaction=False)
for i in range(1, index_size+1):
vector = create_np_array_typed([i]*dim, data_type)
p.execute_command('HSET', i, 'v', vector.tobytes(), 'tags', 'hybrid')
p.execute()
query_data = create_np_array_typed([index_size/2]*dim, data_type)
expected_res = [10]
# Expect to get result which are around index_size/2, closer results will come before (secondary sorting by id).
expected_res.extend([str(int(index_size/2)), ['__v_score', str(0), 'tags', 'hybrid']])
for i in range(1, 10):
expected_res.append(str(int(index_size/2 + (-1*(i+1)/2 if i % 2 else i/2))))
expected_res.append(['__v_score', str((dim*(int((i+1)/2)**2))), 'tags', 'hybrid'])
execute_hybrid_query(env, '(@tags:{hybrid})=>[KNN 10 @v $vec_param]', query_data, 'tags').equal(expected_res)
execute_hybrid_query(env, '(@tags:{nothing})=>[KNN 10 @v $vec_param]', query_data, 'tags').equal([0])
execute_hybrid_query(env, '(@tags:{hybrid} @text:hello)=>[KNN 10 @v $vec_param]', query_data, 'tags').equal([0])
# Change the tag values to 'different, tag' for vectors with ids 5, 10, 20, ..., 6000)
for i in range(1, int(index_size/5) + 1):
vector = create_np_array_typed([5*i]*dim, data_type)
conn.execute_command('HSET', 5*i, 'v', vector.tobytes(), 'tags', 'different, tag')
expected_res = [10]
# Expect to get result which are around index_size/2 that divide by 5, closer results
# will come before (secondary sorting by id).
expected_res.extend([str(int(index_size/2)), ['__v_score', str(0), 'tags', 'different, tag']])
for i in range(1, 10):
expected_res.append(str(int(index_size/2) + (-1*int((5*i+5)/2) if i % 2 else int(5*i/2))))
expected_res.append(['__v_score', str(dim*(5*int((i+1)/2))**2), 'tags', 'different, tag'])
execute_hybrid_query(env, '(@tags:{different})=>[KNN 10 @v $vec_param]', query_data, 'tags').equal(expected_res)
# Expect for top 10 results from vector search that still has the original text "text value".
expected_res = [10]
res_count = 0
for i in range(index_size):
# The desired ids are the top 10 ids that do not divide by 5.
if (int(index_size/2) + int((i+1)/2)) % 5 == 0:
continue
expected_res.append(str(int(index_size/2) + (-1*int((i+1)/2) if i % 2 else int(i/2))))
expected_res.append(['__v_score', str(dim*int((i+1)/2)**2), 'tags', 'hybrid'])
res_count += 1
if res_count == 10:
break
execute_hybrid_query(env, '(@tags:{hybrid})=>[KNN 10 @v $vec_param]', query_data, 'tags').equal(expected_res)
execute_hybrid_query(env, '(@tags:{hy*})=>[KNN 10 @v $vec_param]', query_data, 'tags').equal(expected_res)
# Search with tag list. Expect that docs with 'hybrid' will have lower score (1 vs 2), since they are more frequent.
expected_res = [10]
expected_res.extend([str(int(index_size/2) - 5), '2', ['__v_score', str(dim*5**2), 'tags', 'different, tag'],
str(int(index_size/2)), '2', ['__v_score', str(0), 'tags', 'different, tag']])
for i in range(1, 10):
if i == 5: # ids that divide by 5 were already inserted.
continue
expected_res.extend([str(int(index_size/2) - 5 + i), '1'])
expected_res.append(['__v_score', str(dim*abs(5-i)**2), 'tags', 'hybrid'])
execute_hybrid_query(env, '(@tags:{hybrid|tag})=>[KNN 10 @v $vec_param]', query_data, 'tags',
sort_by_vector=False).equal(expected_res)
conn.execute_command('FT.DROPINDEX', 'idx', 'DD')
def test_hybrid_query_with_numeric():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
conn = getConnectionByEnv(env)
dim = 2
index_size = 6000 * env.shardsCount
for data_type in VECSIM_DATA_TYPES:
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '8', 'TYPE', data_type,
'DIM', dim, 'DISTANCE_METRIC', 'L2', 'EF_RUNTIME', 1000, 'num', 'NUMERIC').ok()
p = conn.pipeline(transaction=False)
for i in range(1, index_size+1):
vector = create_np_array_typed([i]*dim, data_type)
p.execute_command('HSET', i, 'v', vector.tobytes(), 'num', i)
p.execute()
query_data = create_np_array_typed([index_size]*dim, data_type)
expected_res = [10]
# Expect to get result in reverse order to the id, starting from the max id in the index.
for i in range(10):
expected_res.append(str(index_size-i))
expected_res.append(['__v_score', str(dim*i**2), 'num', str(index_size-i)])
execute_hybrid_query(env, '(@num:[0 {}])=>[KNN 10 @v $vec_param]'.format(index_size), query_data, 'num').equal(expected_res)
execute_hybrid_query(env, '(@num:[0 inf])=>[KNN 10 @v $vec_param]', query_data, 'num').equal(expected_res)
# Expect that no result will pass the filter.
execute_hybrid_query(env, '(@num:[0 0.5])=>[KNN 10 @v $vec_param]', query_data, 'num').equal([0])
# Expect to get results with maximum numeric value of the top 100 id in the shard.
lower_bound_num = 100 * env.shardsCount
expected_res = [10]
for i in range(10):
expected_res.append(str(index_size-lower_bound_num-i))
expected_res.append(['__v_score', str(dim*(lower_bound_num+i)**2), 'num', str(index_size-lower_bound_num-i)])
# We switch from batches to ad-hoc BF mode during the run.
execute_hybrid_query(env, '(@num:[-inf {}])=>[KNN 10 @v $vec_param]'.format(index_size-lower_bound_num), query_data, 'num',
hybrid_mode='HYBRID_BATCHES_TO_ADHOC_BF').equal(expected_res)
execute_hybrid_query(env, '(@num:[-inf {}] | @num:[{} {}])=>[KNN 10 @v $vec_param]'
.format(lower_bound_num, index_size-2*lower_bound_num, index_size-lower_bound_num), query_data, 'num',
hybrid_mode='HYBRID_BATCHES_TO_ADHOC_BF').equal(expected_res)
# Expect for 5 results only (45-49), this will use ad-hoc BF since ratio between docs that pass the filter to
# index size is low.
expected_res = [5]
expected_res.extend([str(50-i) for i in range(1, 6)])
env.expect('FT.SEARCH', 'idx', '(@num:[45 (50])=>[KNN 10 @v $vec_param]',
'SORTBY', '__v_score', 'PARAMS', 2, 'vec_param', query_data.tobytes(), 'RETURN', 0).equal(expected_res)
prefix = "_" if env.isCluster() else ""
env.assertEqual(to_dict(env.cmd(prefix+"FT.DEBUG", "VECSIM_INFO", "idx", "v"))['LAST_SEARCH_MODE'], 'HYBRID_ADHOC_BF')
conn.execute_command('FT.DROPINDEX', 'idx', 'DD')
def test_hybrid_query_with_geo():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
conn = getConnectionByEnv(env)
dim = 2
for data_type in VECSIM_DATA_TYPES:
env.expect('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '8', 'TYPE', data_type,
'DIM', dim, 'DISTANCE_METRIC', 'L2', 'EF_RUNTIME', 100, 'coordinate', 'GEO').ok()
index_size = 1000 # for this index size, ADHOC BF mode will always be selected by the heuristics.
p = conn.pipeline(transaction=False)
for i in range(1, index_size+1):
vector = create_np_array_typed([i]*dim, data_type)
p.execute_command('HSET', i, 'v', vector.tobytes(), 'coordinate', str(i/100)+","+str(i/100))
p.execute()
if not env.isCluster():
env.assertEqual(get_vecsim_index_size(env, 'idx', 'v'), index_size)
query_data = create_np_array_typed([index_size]*dim, data_type)
# Expect that ids 1-31 will pass the geo filter, and that the top 10 from these will return.
expected_res = [10]
for i in range(10):
expected_res.append(str(31-i))
expected_res.append(['coordinate', str((31-i)/100)+","+str((31-i)/100)])
env.expect('FT.SEARCH', 'idx', '(@coordinate:[0.0 0.0 50 km])=>[KNN 10 @v $vec_param]',
'SORTBY', '__v_score', 'PARAMS', 2, 'vec_param', query_data.tobytes(), 'RETURN', 1, 'coordinate').equal(expected_res)
# Expect that no results will pass the filter
execute_hybrid_query(env, '(@coordinate:[-1.0 -1.0 1 m])=>[KNN 10 @v $vec_param]', query_data, 'coordinate',
hybrid_mode='HYBRID_ADHOC_BF').equal([0])
conn.execute_command('FT.DROPINDEX', 'idx', 'DD')
def test_hybrid_query_batches_mode_with_complex_queries():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
conn = getConnectionByEnv(env)
dimension = 4
index_size = 6000 * env.shardsCount
for data_type in VECSIM_DATA_TYPES:
conn.execute_command('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '8', 'TYPE', data_type,
'DIM', dimension, 'DISTANCE_METRIC', 'L2', 'EF_RUNTIME', 100, 'num', 'NUMERIC',
't1', 'TEXT', 't2', 'TEXT')
p = conn.pipeline(transaction=False)
close_vector = create_np_array_typed([1]*dimension, data_type)
distant_vector = create_np_array_typed([10]*dimension, data_type)
conn.execute_command('HSET', 1, 'v', close_vector.tobytes(), 'num', 1, 't1', 'text value', 't2', 'hybrid query')
conn.execute_command('HSET', 2, 'v', distant_vector.tobytes(), 'num', 2, 't1', 'text value', 't2', 'hybrid query')
conn.execute_command('HSET', 3, 'v', distant_vector.tobytes(), 'num', 3, 't1', 'other', 't2', 'hybrid query')
conn.execute_command('HSET', 4, 'v', close_vector.tobytes(), 'num', 4, 't1', 'other', 't2', 'hybrid query')
for i in range(5, index_size+1):
further_vector = create_np_array_typed([i]*dimension, data_type)
p.execute_command('HSET', i, 'v', further_vector.tobytes(), 'num', i, 't1', 'text value', 't2', 'hybrid query')
p.execute()
expected_res_1 = [2, '1', '5']
# Search for the "close_vector" that some the vector in the index contain. The batch of vectors should start with
# ids 1, 4. The intersection "child iterator" has two children - intersection iterator (@t2:(hybrid query))
# and not iterator (-@t1:other). When the hybrid iterator will perform "skipTo(4)" for the child iterator,
# the inner intersection iterator will skip to 4, but the not iterator will stay at 2 (4 is not a valid id).
# The child iterator will point to 2, and return NOT_FOUND. This test makes sure that the hybrid iterator can
# handle this situation (without going into infinite loop).
env.expect('FT.SEARCH', 'idx', '(@t2:(hybrid query) -@t1:other)=>[KNN 2 @v $vec_param]',
'SORTBY', '__v_score', 'LIMIT', 0, 2,
'PARAMS', 2, 'vec_param', close_vector.tobytes(),
'RETURN', 0).equal(expected_res_1)
prefix = "_" if env.isCluster() else ""
env.assertEqual(to_dict(env.cmd(prefix+"FT.DEBUG", "VECSIM_INFO", "idx", "v"))['LAST_SEARCH_MODE'], 'HYBRID_BATCHES')
# test modifier list
expected_res_2 = [10, '10', '11', '12', '13', '14', '15', '16', '17', '18', '19']
env.expect('FT.SEARCH', 'idx', '(@t1|t2:(value text) @num:[10 30])=>[KNN 10 @v $vec_param]',
'SORTBY', '__v_score',
'PARAMS', 2, 'vec_param', close_vector.tobytes(),
'RETURN', 0).equal(expected_res_2)
# test with query attributes
env.expect('FT.SEARCH', 'idx', '(@t1|t2:(value text)=>{$inorder: true} @num:[10 30])=>[KNN 10 @v $vec_param]',
'SORTBY', '__v_score',
'WITHSCORES',
'PARAMS', 2, 'vec_param', close_vector.tobytes(),
'RETURN', 2, 't1', 't2').equal([0])
conn.execute_command('FT.DROPINDEX', 'idx', 'DD')
def test_hybrid_query_non_vector_score():
env = Env(moduleArgs='DEFAULT_DIALECT 2')
conn = getConnectionByEnv(env)
dimension = 128
qty = 100
conn.execute_command('FT.CREATE', 'idx', 'SCHEMA', 'v', 'VECTOR', 'HNSW', '6', 'TYPE', 'FLOAT32',
'DIM', dimension, 'DISTANCE_METRIC', 'L2', 't', 'TEXT')
load_vectors_with_texts_into_redis(conn, 'v', dimension, qty)
# Change the text value to 'other' for 10 vectors (with id 10, 20, ..., 100)
for i in range(1, 11):
vector = np.float32([10*i for j in range(dimension)])
conn.execute_command('HSET', 10*i, 'v', vector.tobytes(), 't', 'other')
query_data = np.float32([qty for j in range(dimension)])
# All documents should match, so TOP 10 takes the 10 with the largest ids. Since we sort by default score
# and "value" is optional, expect that 100 will come first, and the rest will be sorted by id in ascending order.
expected_res_1 = [10,
'100', '3', ['__v_score', '0', 't', 'other'],
'91', '2', ['__v_score', '10368', 't', 'text value'],
'92', '2', ['__v_score', '8192', 't', 'text value'],
'93', '2', ['__v_score', '6272', 't', 'text value'],
'94', '2', ['__v_score', '4608', 't', 'text value'],
'95', '2', ['__v_score', '3200', 't', 'text value'],
'96', '2', ['__v_score', '2048', 't', 'text value'],
'97', '2', ['__v_score', '1152', 't', 'text value'],
'98', '2', ['__v_score', '512', 't', 'text value'],
'99', '2', ['__v_score', '128', 't', 'text value']]
execute_hybrid_query(env, '((text ~value)|other)=>[KNN 10 @v $vec_param]', query_data, 't', sort_by_vector=False,
hybrid_mode='HYBRID_ADHOC_BF').equal(expected_res_1)
execute_hybrid_query(env, '((text ~value)|other)=>[KNN 10 @v $vec_param]', query_data, 't', sort_by_vector=False,
sort_by_non_vector_field=True, hybrid_mode='HYBRID_ADHOC_BF').equal(expected_res_1)
# Same as above, but here we use fuzzy for 'text'
expected_res_2 = [10,
'100', '3', ['__v_score', '0', 't', 'other'],
'91', '1', ['__v_score', '10368', 't', 'text value'],
'92', '1', ['__v_score', '8192', 't', 'text value'],
'93', '1', ['__v_score', '6272', 't', 'text value'],
'94', '1', ['__v_score', '4608', 't', 'text value'],
'95', '1', ['__v_score', '3200', 't', 'text value'],
'96', '1', ['__v_score', '2048', 't', 'text value'],
'97', '1', ['__v_score', '1152', 't', 'text value'],
'98', '1', ['__v_score', '512', 't', 'text value'],
'99', '1', ['__v_score', '128', 't', 'text value']]
execute_hybrid_query(env, '(%test%|other)=>[KNN 10 @v $vec_param]', query_data, 't', sort_by_vector=False,
hybrid_mode='HYBRID_ADHOC_BF').equal(expected_res_2)
execute_hybrid_query(env, '(%test%|other)=>[KNN 10 @v $vec_param]', query_data, 't', sort_by_vector=False,
sort_by_non_vector_field=True, hybrid_mode='HYBRID_ADHOC_BF').equal(expected_res_2)
# use TFIDF.DOCNORM scorer
expected_res_3 = [10,
'100', '3', ['__v_score', '0', 't', 'other'],
'91', '0.33333333333333331', ['__v_score', '10368', 't', 'text value'],
'92', '0.33333333333333331', ['__v_score', '8192', 't', 'text value'],
'93', '0.33333333333333331', ['__v_score', '6272', 't', 'text value'],
'94', '0.33333333333333331', ['__v_score', '4608', 't', 'text value'],
'95', '0.33333333333333331', ['__v_score', '3200', 't', 'text value'],
'96', '0.33333333333333331', ['__v_score', '2048', 't', 'text value'],
'97', '0.33333333333333331', ['__v_score', '1152', 't', 'text value'],
'98', '0.33333333333333331', ['__v_score', '512', 't', 'text value'],
'99', '0.33333333333333331', ['__v_score', '128', 't', 'text value']]
res = env.cmd('FT.SEARCH', 'idx', '(text|other)=>[KNN 10 @v $vec_param]', 'SCORER', 'TFIDF.DOCNORM', 'WITHSCORES',
'PARAMS', 2, 'vec_param', query_data.tobytes(),
'RETURN', 2, 't', '__v_score', 'LIMIT', 0, 10)
compare_lists(env, res, expected_res_3, delta=0.01)
# Those scorers are scoring per shard.
if not env.isCluster():
# use BM25 scorer
expected_res_4 = [10, '100', '0.72815531789441912', ['__v_score', '0', 't', 'other'], '91', '0.24271843929813972', ['__v_score', '10368', 't', 'text value'], '92', '0.24271843929813972', ['__v_score', '8192', 't', 'text value'], '93', '0.24271843929813972', ['__v_score', '6272', 't', 'text value'], '94', '0.24271843929813972', ['__v_score', '4608', 't', 'text value'], '95', '0.24271843929813972', ['__v_score', '3200', 't', 'text value'], '96', '0.24271843929813972', ['__v_score', '2048', 't', 'text value'], '97', '0.24271843929813972', ['__v_score', '1152', 't', 'text value'], '98', '0.24271843929813972', ['__v_score', '512', 't', 'text value'], '99', '0.24271843929813972', ['__v_score', '128', 't', 'text value']]
res = env.cmd('FT.SEARCH', 'idx', '(text|other)=>[KNN 10 @v $vec_param]', 'SCORER', 'BM25', 'WITHSCORES',
'PARAMS', 2, 'vec_param', query_data.tobytes(),
'RETURN', 2, 't', '__v_score', 'LIMIT', 0, 10)
compare_lists(env, res, expected_res_4, delta=0.01)
# use DISMAX scorer
expected_res_5 = [10, '91', '1', ['__v_score', '10368', 't', 'text value'], '92', '1', ['__v_score', '8192', 't', 'text value'], '93', '1', ['__v_score', '6272', 't', 'text value'], '94', '1', ['__v_score', '4608', 't', 'text value'], '95', '1', ['__v_score', '3200', 't', 'text value'], '96', '1', ['__v_score', '2048', 't', 'text value'], '97', '1', ['__v_score', '1152', 't', 'text value'], '98', '1', ['__v_score', '512', 't', 'text value'], '99', '1', ['__v_score', '128', 't', 'text value'], '100', '1', ['__v_score', '0', 't', 'other']]
env.expect('FT.SEARCH', 'idx', '(text|other)=>[KNN 10 @v $vec_param]', 'SCORER', 'DISMAX', 'WITHSCORES',
'PARAMS', 2, 'vec_param', query_data.tobytes(),
'RETURN', 2, 't', '__v_score', 'LIMIT', 0, 10).equal(expected_res_5)
# use DOCSCORE scorer
env.expect('FT.SEARCH', 'idx', '(text|other)=>[KNN 10 @v $vec_param]', 'SCORER', 'DOCSCORE', 'WITHSCORES',