@@ -2,6 +2,8 @@ package redis_test
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "strconv"
5
7
"time"
6
8
7
9
. "github.com/bsm/ginkgo/v2"
@@ -640,6 +642,62 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
640
642
Expect (res .Rows [0 ].Fields ["t2" ]).To (BeEquivalentTo ("world" ))
641
643
})
642
644
645
+ FIt ("should FTAggregate with scorer and addscores" , Label ("search" , "ftaggregate" ), func () {
646
+ title := & redis.FieldSchema {FieldName : "title" , FieldType : redis .SearchFieldTypeText , Sortable : false }
647
+ description := & redis.FieldSchema {FieldName : "description" , FieldType : redis .SearchFieldTypeText , Sortable : false }
648
+ val , err := client .FTCreate (ctx , "idx1" , & redis.FTCreateOptions {OnHash : true , Prefix : []interface {}{"product:" }}, title , description ).Result ()
649
+ Expect (err ).NotTo (HaveOccurred ())
650
+ Expect (val ).To (BeEquivalentTo ("OK" ))
651
+ WaitForIndexing (client , "idx1" )
652
+
653
+ client .HSet (ctx , "product:1" , "title" , "New Gaming Laptop" , "description" , "this is not a desktop" )
654
+ client .HSet (ctx , "product:2" , "title" , "Super Old Not Gaming Laptop" , "description" , "this laptop is not a new laptop but it is a laptop" )
655
+ client .HSet (ctx , "product:3" , "title" , "Office PC" , "description" , "office desktop pc" )
656
+
657
+ options := & redis.FTAggregateOptions {
658
+ AddScores : true ,
659
+ Scorer : "BM25" ,
660
+ SortBy : []redis.FTAggregateSortBy {{
661
+ FieldName : "@__score" ,
662
+ Desc : true ,
663
+ }},
664
+ }
665
+
666
+ res , err := client .FTAggregateWithArgs (ctx , "idx1" , "laptop" , options ).Result ()
667
+ Expect (err ).NotTo (HaveOccurred ())
668
+ Expect (res ).ToNot (BeNil ())
669
+ Expect (len (res .Rows )).To (BeEquivalentTo (2 ))
670
+ score1 , err := strconv .ParseFloat (fmt .Sprintf ("%s" , res .Rows [0 ].Fields ["__score" ]), 64 )
671
+ Expect (err ).NotTo (HaveOccurred ())
672
+ score2 , err := strconv .ParseFloat (fmt .Sprintf ("%s" , res .Rows [1 ].Fields ["__score" ]), 64 )
673
+ Expect (err ).NotTo (HaveOccurred ())
674
+ Expect (score1 ).To (BeNumerically (">" , score2 ))
675
+
676
+ optionsDM := & redis.FTAggregateOptions {
677
+ AddScores : true ,
678
+ Scorer : "DISMAX" ,
679
+ SortBy : []redis.FTAggregateSortBy {{
680
+ FieldName : "@__score" ,
681
+ Desc : true ,
682
+ }},
683
+ }
684
+
685
+ resDM , err := client .FTAggregateWithArgs (ctx , "idx1" , "laptop" , optionsDM ).Result ()
686
+ Expect (err ).NotTo (HaveOccurred ())
687
+ Expect (resDM ).ToNot (BeNil ())
688
+ Expect (len (resDM .Rows )).To (BeEquivalentTo (2 ))
689
+ score1DM , err := strconv .ParseFloat (fmt .Sprintf ("%s" , resDM .Rows [0 ].Fields ["__score" ]), 64 )
690
+ Expect (err ).NotTo (HaveOccurred ())
691
+ score2DM , err := strconv .ParseFloat (fmt .Sprintf ("%s" , resDM .Rows [1 ].Fields ["__score" ]), 64 )
692
+ Expect (err ).NotTo (HaveOccurred ())
693
+ Expect (score1DM ).To (BeNumerically (">" , score2DM ))
694
+
695
+ Expect (score1DM ).To (BeEquivalentTo (float64 (4 )))
696
+ Expect (score2DM ).To (BeEquivalentTo (float64 (1 )))
697
+ Expect (score1 ).NotTo (BeEquivalentTo (score1DM ))
698
+ Expect (score2 ).NotTo (BeEquivalentTo (score2DM ))
699
+ })
700
+
643
701
It ("should FTAggregate apply and groupby" , Label ("search" , "ftaggregate" ), func () {
644
702
text1 := & redis.FieldSchema {FieldName : "PrimaryKey" , FieldType : redis .SearchFieldTypeText , Sortable : true }
645
703
num1 := & redis.FieldSchema {FieldName : "CreatedDateTimeUTC" , FieldType : redis .SearchFieldTypeNumeric , Sortable : true }
@@ -721,7 +779,6 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
721
779
Expect (res .Rows [0 ].Fields ["age" ]).To (BeEquivalentTo ("19" ))
722
780
Expect (res .Rows [1 ].Fields ["age" ]).To (BeEquivalentTo ("25" ))
723
781
}
724
-
725
782
})
726
783
727
784
It ("should FTSearch SkipInitialScan" , Label ("search" , "ftsearch" ), func () {
0 commit comments