Skip to content

Commit 28e79aa

Browse files
committed
fix: rearange args for ft.aggregate
apply should be before any groupby or sortby
1 parent 0b34b19 commit 28e79aa

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

search_commands.go

+25-16
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,12 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery
508508
if options.Timeout > 0 {
509509
queryArgs = append(queryArgs, "TIMEOUT", options.Timeout)
510510
}
511+
for _, apply := range options.Apply {
512+
queryArgs = append(queryArgs, "APPLY", apply.Field)
513+
if apply.As != "" {
514+
queryArgs = append(queryArgs, "AS", apply.As)
515+
}
516+
}
511517
if options.GroupBy != nil {
512518
for _, groupBy := range options.GroupBy {
513519
queryArgs = append(queryArgs, "GROUPBY", len(groupBy.Fields))
@@ -549,12 +555,6 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery
549555
if options.SortByMax > 0 {
550556
queryArgs = append(queryArgs, "MAX", options.SortByMax)
551557
}
552-
for _, apply := range options.Apply {
553-
queryArgs = append(queryArgs, "APPLY", apply.Field)
554-
if apply.As != "" {
555-
queryArgs = append(queryArgs, "AS", apply.As)
556-
}
557-
}
558558
if options.LimitOffset > 0 {
559559
queryArgs = append(queryArgs, "LIMIT", options.LimitOffset)
560560
}
@@ -661,11 +661,12 @@ func (cmd *AggregateCmd) readReply(rd *proto.Reader) (err error) {
661661
data, err := rd.ReadSlice()
662662
if err != nil {
663663
cmd.err = err
664-
return nil
664+
return err
665665
}
666666
cmd.val, err = ProcessAggregateResult(data)
667667
if err != nil {
668668
cmd.err = err
669+
return err
669670
}
670671
return nil
671672
}
@@ -699,6 +700,12 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st
699700
if options.Timeout > 0 {
700701
args = append(args, "TIMEOUT", options.Timeout)
701702
}
703+
for _, apply := range options.Apply {
704+
args = append(args, "APPLY", apply.Field)
705+
if apply.As != "" {
706+
args = append(args, "AS", apply.As)
707+
}
708+
}
702709
if options.GroupBy != nil {
703710
for _, groupBy := range options.GroupBy {
704711
args = append(args, "GROUPBY", len(groupBy.Fields))
@@ -740,12 +747,6 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st
740747
if options.SortByMax > 0 {
741748
args = append(args, "MAX", options.SortByMax)
742749
}
743-
for _, apply := range options.Apply {
744-
args = append(args, "APPLY", apply.Field)
745-
if apply.As != "" {
746-
args = append(args, "AS", apply.As)
747-
}
748-
}
749750
if options.LimitOffset > 0 {
750751
args = append(args, "LIMIT", options.LimitOffset)
751752
}
@@ -1693,7 +1694,8 @@ func (cmd *FTSearchCmd) readReply(rd *proto.Reader) (err error) {
16931694

16941695
// FTSearch - Executes a search query on an index.
16951696
// The 'index' parameter specifies the index to search, and the 'query' parameter specifies the search query.
1696-
// For more information, please refer to the Redis documentation:
1697+
// For more information, please refer to the Redis documentation about [FT.SEARCH].
1698+
//
16971699
// [FT.SEARCH]: (https://redis.io/commands/ft.search/)
16981700
func (c cmdable) FTSearch(ctx context.Context, index string, query string) *FTSearchCmd {
16991701
args := []interface{}{"FT.SEARCH", index, query}
@@ -1704,6 +1706,12 @@ func (c cmdable) FTSearch(ctx context.Context, index string, query string) *FTSe
17041706

17051707
type SearchQuery []interface{}
17061708

1709+
// FTSearchQuery - Executes a search query on an index with additional options.
1710+
// The 'index' parameter specifies the index to search, the 'query' parameter specifies the search query,
1711+
// and the 'options' parameter specifies additional options for the search.
1712+
// For more information, please refer to the Redis documentation about [FT.SEARCH].
1713+
//
1714+
// [FT.SEARCH]: (https://redis.io/commands/ft.search/)
17071715
func FTSearchQuery(query string, options *FTSearchOptions) SearchQuery {
17081716
queryArgs := []interface{}{query}
17091717
if options != nil {
@@ -1816,7 +1824,8 @@ func FTSearchQuery(query string, options *FTSearchOptions) SearchQuery {
18161824
// FTSearchWithArgs - Executes a search query on an index with additional options.
18171825
// The 'index' parameter specifies the index to search, the 'query' parameter specifies the search query,
18181826
// and the 'options' parameter specifies additional options for the search.
1819-
// For more information, please refer to the Redis documentation:
1827+
// For more information, please refer to the Redis documentation about [FT.SEARCH].
1828+
//
18201829
// [FT.SEARCH]: (https://redis.io/commands/ft.search/)
18211830
func (c cmdable) FTSearchWithArgs(ctx context.Context, index string, query string, options *FTSearchOptions) *FTSearchCmd {
18221831
args := []interface{}{"FT.SEARCH", index, query}
@@ -1908,7 +1917,7 @@ func (c cmdable) FTSearchWithArgs(ctx context.Context, index string, query strin
19081917
}
19091918
}
19101919
if options.SortByWithCount {
1911-
args = append(args, "WITHCOUT")
1920+
args = append(args, "WITHCOUNT")
19121921
}
19131922
}
19141923
if options.LimitOffset >= 0 && options.Limit > 0 {

search_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,31 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
640640
Expect(res.Rows[0].Fields["t2"]).To(BeEquivalentTo("world"))
641641
})
642642

643+
It("should FTAggregate apply and groupby", Label("search", "ftaggregate"), func() {
644+
text1 := &redis.FieldSchema{FieldName: "PrimaryKey", FieldType: redis.SearchFieldTypeText, Sortable: true}
645+
num1 := &redis.FieldSchema{FieldName: "CreatedDateTimeUTC", FieldType: redis.SearchFieldTypeNumeric, Sortable: true}
646+
val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{}, text1, num1).Result()
647+
Expect(err).NotTo(HaveOccurred())
648+
Expect(val).To(BeEquivalentTo("OK"))
649+
WaitForIndexing(client, "idx1")
650+
651+
// 6 feb
652+
client.HSet(ctx, "doc1", "PrimaryKey", "9::362330", "CreatedDateTimeUTC", "1738823999")
653+
654+
// 12 feb
655+
client.HSet(ctx, "doc2", "PrimaryKey", "9::362329", "CreatedDateTimeUTC", "1739342399")
656+
client.HSet(ctx, "doc3", "PrimaryKey", "9::362329", "CreatedDateTimeUTC", "1739353199")
657+
658+
reducer := redis.FTAggregateReducer{Reducer: redis.SearchCount}
659+
660+
options := &redis.FTAggregateOptions{
661+
Apply: []redis.FTAggregateApply{{Field: "@CreatedDateTimeUTC /(60*60*24)", As: "TimestampAsDay"}},
662+
GroupBy: []redis.FTAggregateGroupBy{{Fields: []interface{}{"@TimestampAsDay"}, Reduce: []redis.FTAggregateReducer{reducer}}},
663+
}
664+
res, err := client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result()
665+
Expect(err).NotTo(HaveOccurred())
666+
})
667+
643668
It("should FTAggregate apply", Label("search", "ftaggregate"), func() {
644669
text1 := &redis.FieldSchema{FieldName: "PrimaryKey", FieldType: redis.SearchFieldTypeText, Sortable: true}
645670
num1 := &redis.FieldSchema{FieldName: "CreatedDateTimeUTC", FieldType: redis.SearchFieldTypeNumeric, Sortable: true}

0 commit comments

Comments
 (0)