1
1
package redis .clients .jedis .modules .search ;
2
2
3
+ import static java .util .Collections .singletonMap ;
3
4
import static org .junit .Assert .*;
4
5
import static redis .clients .jedis .search .RediSearchUtil .toStringMap ;
5
6
@@ -451,10 +452,11 @@ public void testHNSWVVectorSimilarity() {
451
452
client .hset ("b" , "v" , "aaaabaaa" );
452
453
client .hset ("c" , "v" , "aaaaabaa" );
453
454
454
- Query query = new Query ("*=>[KNN 2 @v $vec]" )
455
+ Query query = new Query ("*=>[KNN 2 @v $vec]" )
455
456
.addParam ("vec" , "aaaaaaaa" )
456
457
.setSortBy ("__v_score" , true )
457
- .returnFields ("__v_score" );
458
+ .returnFields ("__v_score" )
459
+ .dialect (2 );
458
460
Document doc1 = client .ftSearch (index , query ).getDocuments ().get (0 );
459
461
assertEquals ("a" , doc1 .getId ());
460
462
assertEquals ("0" , doc1 .get ("__v_score" ));
@@ -474,15 +476,139 @@ public void testFlatVectorSimilarity() {
474
476
client .hset ("b" , "v" , "aaaabaaa" );
475
477
client .hset ("c" , "v" , "aaaaabaa" );
476
478
477
- Query query = new Query ("*=>[KNN 2 @v $vec]" )
479
+ Query query = new Query ("*=>[KNN 2 @v $vec]" )
478
480
.addParam ("vec" , "aaaaaaaa" )
479
481
.setSortBy ("__v_score" , true )
480
- .returnFields ("__v_score" );
482
+ .returnFields ("__v_score" )
483
+ .dialect (2 );
481
484
Document doc1 = client .ftSearch (index , query ).getDocuments ().get (0 );
482
485
assertEquals ("a" , doc1 .getId ());
483
486
assertEquals ("0" , doc1 .get ("__v_score" ));
484
487
}
485
488
489
+ @ Test
490
+ public void testDialectConfig () {
491
+ // confirm default
492
+ assertEquals (singletonMap ("DEFAULT_DIALECT" , "1" ), client .ftConfigGet ("DEFAULT_DIALECT" ));
493
+
494
+ assertEquals ("OK" , client .ftConfigSet ("DEFAULT_DIALECT" , "2" ));
495
+ assertEquals (singletonMap ("DEFAULT_DIALECT" , "2" ), client .ftConfigGet ("DEFAULT_DIALECT" ));
496
+
497
+ try {
498
+ client .ftConfigSet ("DEFAULT_DIALECT" , "0" );
499
+ fail ();
500
+ } catch (JedisDataException ex ) {
501
+ }
502
+
503
+ try {
504
+ client .ftConfigSet ("DEFAULT_DIALECT" , "3" );
505
+ fail ();
506
+ } catch (JedisDataException ex ) {
507
+ }
508
+
509
+ // Restore to default
510
+ assertEquals ("OK" , client .ftConfigSet ("DEFAULT_DIALECT" , "1" ));
511
+ }
512
+
513
+ @ Test
514
+ public void testDialectsWithFTExplain () throws Exception {
515
+ Map <String , Object > attr = new HashMap <>();
516
+ attr .put ("TYPE" , "FLOAT32" );
517
+ attr .put ("DIM" , 2 );
518
+ attr .put ("DISTANCE_METRIC" , "L2" );
519
+
520
+ Schema sc = new Schema ()
521
+ .addFlatVectorField ("v" , attr )
522
+ .addTagField ("title" )
523
+ .addTextField ("t1" , 1.0 )
524
+ .addTextField ("t2" , 1.0 )
525
+ .addNumericField ("num" );
526
+ assertEquals ("OK" , client .ftCreate (index , IndexOptions .defaultOptions (), sc ));
527
+
528
+ client .hset ("1" , "t1" , "hello" );
529
+
530
+ String q = "(*)" ;
531
+ Query query = new Query (q ).dialect (1 );
532
+ try {
533
+ client .ftExplain (index , query );
534
+ fail ();
535
+ } catch (JedisDataException e ) {
536
+ assertTrue ("Should contain 'Syntax error'" , e .getMessage ().contains ("Syntax error" ));
537
+ }
538
+ query = new Query (q ).dialect (2 );
539
+ assertTrue ("Should contain 'WILDCARD'" , client .ftExplain (index , query ).contains ("WILDCARD" ));
540
+
541
+ q = "$hello" ;
542
+ query = new Query (q ).dialect (1 );
543
+ try {
544
+ client .ftExplain (index , query );
545
+ fail ();
546
+ } catch (JedisDataException e ) {
547
+ assertTrue ("Should contain 'Syntax error'" , e .getMessage ().contains ("Syntax error" ));
548
+ }
549
+ query = new Query (q ).dialect (2 ).addParam ("hello" , "hello" );
550
+ assertTrue ("Should contain 'UNION {\n hello\n +hello(expanded)\n }\n '" ,
551
+ client .ftExplain (index , query ).contains ("UNION {\n hello\n +hello(expanded)\n }\n " ));
552
+
553
+ q = "@title:(@num:[0 10])" ;
554
+ query = new Query (q ).dialect (1 );
555
+ assertTrue ("Should contain 'NUMERIC {0.000000 <= @num <= 10.000000}'" ,
556
+ client .ftExplain (index , query ).contains ("NUMERIC {0.000000 <= @num <= 10.000000}" ));
557
+ query = new Query (q ).dialect (2 );
558
+ try {
559
+ client .ftExplain (index , query );
560
+ fail ();
561
+ } catch (JedisDataException e ) {
562
+ assertTrue ("Should contain 'Syntax error'" , e .getMessage ().contains ("Syntax error" ));
563
+ }
564
+
565
+ q = "@t1:@t2:@t3:hello" ;
566
+ query = new Query (q ).dialect (1 );
567
+ assertTrue ("Should contain '@NULL:UNION {\n @NULL:hello\n @NULL:+hello(expanded)\n }\n '" ,
568
+ client .ftExplain (index , query ).contains ("@NULL:UNION {\n @NULL:hello\n @NULL:+hello(expanded)\n }\n " ));
569
+ query = new Query (q ).dialect (2 );
570
+ try {
571
+ client .ftExplain (index , query );
572
+ fail ();
573
+ } catch (JedisDataException e ) {
574
+ assertTrue ("Should contain 'Syntax error'" , e .getMessage ().contains ("Syntax error" ));
575
+ }
576
+
577
+ q = "@title:{foo}}}}}" ;
578
+ query = new Query (q ).dialect (1 );
579
+ assertTrue ("Should contain 'TAG:@title {\n foo\n }\n '" ,
580
+ client .ftExplain (index , query ).contains ("TAG:@title {\n foo\n }\n " ));
581
+ query = new Query (q ).dialect (2 );
582
+ try {
583
+ client .ftExplain (index , query );
584
+ fail ();
585
+ } catch (JedisDataException e ) {
586
+ assertTrue ("Should contain 'Syntax error'" , e .getMessage ().contains ("Syntax error" ));
587
+ }
588
+
589
+ q = "*=>[KNN 10 @v $BLOB]" ;
590
+ query = new Query (q ).addParam ("BLOB" , "aaaa" ).dialect (1 );
591
+ try {
592
+ client .ftExplain (index , query );
593
+ fail ();
594
+ } catch (JedisDataException e ) {
595
+ assertTrue ("Should contain 'Syntax error'" , e .getMessage ().contains ("Syntax error" ));
596
+ }
597
+ query = new Query (q ).addParam ("BLOB" , "aaaa" ).dialect (2 );
598
+ assertTrue ("Should contain '{K=10 nearest vector'" , client .ftExplain (index , query ).contains ("{K=10 nearest vector" ));
599
+
600
+ q = "*=>[knn $K @vec_field $BLOB as score]" ;
601
+ query = new Query (q ).addParam ("BLOB" , "aaaa" ).addParam ("K" , "10" ).dialect (1 );
602
+ try {
603
+ client .ftExplain (index , query );
604
+ fail ();
605
+ } catch (JedisDataException e ) {
606
+ assertTrue ("Should contain 'Syntax error'" , e .getMessage ().contains ("Syntax error" ));
607
+ }
608
+ query = new Query (q ).addParam ("BLOB" , "aaaa" ).addParam ("K" , "10" ).dialect (2 );
609
+ assertTrue ("Should contain '{K=10 nearest vector'" , client .ftExplain (index , query ).contains ("{K=10 nearest vector" ));
610
+ }
611
+
486
612
@ Test
487
613
public void testQueryParams () {
488
614
Schema sc = new Schema ().addNumericField ("numval" );
@@ -492,7 +618,7 @@ public void testQueryParams() {
492
618
client .hset ("2" , "numval" , "2" );
493
619
client .hset ("3" , "numval" , "3" );
494
620
495
- Query query = new Query ("@numval:[$min $max]" ).addParam ("min" , 1 ).addParam ("max" , 2 );
621
+ Query query = new Query ("@numval:[$min $max]" ).addParam ("min" , 1 ).addParam ("max" , 2 ). dialect ( 2 ) ;
496
622
assertEquals (2 , client .ftSearch (index , query ).getTotalResults ());
497
623
}
498
624
@@ -1218,8 +1344,8 @@ public void returnWithFieldNames() throws Exception {
1218
1344
addDocument ("doc" , map );
1219
1345
1220
1346
// Query
1221
- SearchResult res = client .ftSearch (index , new Query ()
1222
- .returnFields (FieldName .of ("a" ), FieldName .of ("b" ).as ("d" )));
1347
+ SearchResult res = client .ftSearch (index ,
1348
+ new Query () .returnFields (FieldName .of ("a" ), FieldName .of ("b" ).as ("d" )));
1223
1349
assertEquals (1 , res .getTotalResults ());
1224
1350
Document doc = res .getDocuments ().get (0 );
1225
1351
assertEquals ("value1" , doc .get ("a" ));
@@ -1285,7 +1411,7 @@ public void config() throws Exception {
1285
1411
@ Test
1286
1412
public void configOnTimeout () throws Exception {
1287
1413
assertEquals ("OK" , client .ftConfigSet ("ON_TIMEOUT" , "fail" ));
1288
- assertEquals (Collections . singletonMap ("ON_TIMEOUT" , "fail" ), client .ftConfigGet ("ON_TIMEOUT" ));
1414
+ assertEquals (singletonMap ("ON_TIMEOUT" , "fail" ), client .ftConfigGet ("ON_TIMEOUT" ));
1289
1415
1290
1416
try {
1291
1417
client .ftConfigSet ("ON_TIMEOUT" , "null" );
0 commit comments