Skip to content

Commit fd47606

Browse files
committed
fix (redis#3220)
* fixed the calculation bug of the count of load params
1 parent f273a42 commit fd47606

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

search_commands.go

+15
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ type FTAggregateApply struct {
231231

232232
type FTAggregateLoad struct {
233233
Field string
234+
As string
234235
}
235236

236237
type FTAggregateWithCursor struct {
@@ -497,9 +498,16 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery
497498
}
498499
if options.Load != nil {
499500
queryArgs = append(queryArgs, "LOAD", len(options.Load))
501+
index, count := len(queryArgs)-1, 0
500502
for _, load := range options.Load {
501503
queryArgs = append(queryArgs, load.Field)
504+
count++
505+
if load.As != "" {
506+
queryArgs = append(queryArgs, "AS", load.As)
507+
count += 2
508+
}
502509
}
510+
queryArgs[index] = count
503511
}
504512
if options.Timeout > 0 {
505513
queryArgs = append(queryArgs, "TIMEOUT", options.Timeout)
@@ -684,9 +692,16 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st
684692
}
685693
if options.Load != nil {
686694
args = append(args, "LOAD", len(options.Load))
695+
index, count := len(args)-1, 0
687696
for _, load := range options.Load {
688697
args = append(args, load.Field)
698+
count++
699+
if load.As != "" {
700+
args = append(args, "AS", load.As)
701+
count += 2
702+
}
689703
}
704+
args[index] = count
690705
}
691706
if options.Timeout > 0 {
692707
args = append(args, "TIMEOUT", options.Timeout)

search_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,17 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
637637
Expect(err).NotTo(HaveOccurred())
638638
Expect(res.Rows[0].Fields["t2"]).To(BeEquivalentTo("world"))
639639

640+
options = &redis.FTAggregateOptions{Load: []redis.FTAggregateLoad{{Field: "t2", As: "t2alias"}}}
641+
res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result()
642+
Expect(err).NotTo(HaveOccurred())
643+
Expect(res.Rows[0].Fields["t2alias"]).To(BeEquivalentTo("world"))
644+
645+
options = &redis.FTAggregateOptions{Load: []redis.FTAggregateLoad{{Field: "t1"}, {Field: "t2", As: "t2alias"}}}
646+
res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result()
647+
Expect(err).NotTo(HaveOccurred())
648+
Expect(res.Rows[0].Fields["t1"]).To(BeEquivalentTo("hello"))
649+
Expect(res.Rows[0].Fields["t2alias"]).To(BeEquivalentTo("world"))
650+
640651
options = &redis.FTAggregateOptions{LoadAll: true}
641652
res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result()
642653
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)