Skip to content

Commit 7e517ec

Browse files
bitsarkndyakovofekshenawa
authored
fix(search&aggregate):fix error overwrite and typo #3220 (#3224)
* fix (#3220) * LOAD has NO AS param(https://redis.io/docs/latest/commands/ft.aggregate/) * fix typo: WITHCOUT -> WITHCOUNT * fix (#3220): * Compatible with known RediSearch issue in test * fix (#3220) * fixed the calculation bug of the count of load params * test should not include special condition * return errors when they occur --------- Co-authored-by: Nedyalko Dyakov <[email protected]> Co-authored-by: ofekshenawa <[email protected]>
1 parent c29d399 commit 7e517ec

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

search_commands.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,16 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery
514514
}
515515
if options.Load != nil {
516516
queryArgs = append(queryArgs, "LOAD", len(options.Load))
517+
index, count := len(queryArgs)-1, 0
517518
for _, load := range options.Load {
518519
queryArgs = append(queryArgs, load.Field)
520+
count++
519521
if load.As != "" {
520522
queryArgs = append(queryArgs, "AS", load.As)
523+
count += 2
521524
}
522525
}
526+
queryArgs[index] = count
523527
}
524528

525529
if options.Timeout > 0 {
@@ -677,12 +681,10 @@ func (cmd *AggregateCmd) String() string {
677681
func (cmd *AggregateCmd) readReply(rd *proto.Reader) (err error) {
678682
data, err := rd.ReadSlice()
679683
if err != nil {
680-
cmd.err = err
681684
return err
682685
}
683686
cmd.val, err = ProcessAggregateResult(data)
684687
if err != nil {
685-
cmd.err = err
686688
return err
687689
}
688690
return nil
@@ -713,12 +715,16 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st
713715
}
714716
if options.Load != nil {
715717
args = append(args, "LOAD", len(options.Load))
718+
index, count := len(args)-1, 0
716719
for _, load := range options.Load {
717720
args = append(args, load.Field)
721+
count++
718722
if load.As != "" {
719723
args = append(args, "AS", load.As)
724+
count += 2
720725
}
721726
}
727+
args[index] = count
722728
}
723729
if options.Timeout > 0 {
724730
args = append(args, "TIMEOUT", options.Timeout)
@@ -1420,7 +1426,7 @@ func (cmd *FTInfoCmd) readReply(rd *proto.Reader) (err error) {
14201426
}
14211427
cmd.val, err = parseFTInfo(data)
14221428
if err != nil {
1423-
cmd.err = err
1429+
return err
14241430
}
14251431

14261432
return nil
@@ -1513,12 +1519,11 @@ func (cmd *FTSpellCheckCmd) RawResult() (interface{}, error) {
15131519
func (cmd *FTSpellCheckCmd) readReply(rd *proto.Reader) (err error) {
15141520
data, err := rd.ReadSlice()
15151521
if err != nil {
1516-
cmd.err = err
1517-
return nil
1522+
return err
15181523
}
15191524
cmd.val, err = parseFTSpellCheck(data)
15201525
if err != nil {
1521-
cmd.err = err
1526+
return err
15221527
}
15231528
return nil
15241529
}
@@ -1702,12 +1707,11 @@ func (cmd *FTSearchCmd) RawResult() (interface{}, error) {
17021707
func (cmd *FTSearchCmd) readReply(rd *proto.Reader) (err error) {
17031708
data, err := rd.ReadSlice()
17041709
if err != nil {
1705-
cmd.err = err
1706-
return nil
1710+
return err
17071711
}
17081712
cmd.val, err = parseFTSearch(data, cmd.options.NoContent, cmd.options.WithScores, cmd.options.WithPayloads, cmd.options.WithSortKeys)
17091713
if err != nil {
1710-
cmd.err = err
1714+
return err
17111715
}
17121716
return nil
17131717
}

search_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
269269
Expect(err).NotTo(HaveOccurred())
270270
Expect(res1.Total).To(BeEquivalentTo(int64(1)))
271271

272+
_, err = client.FTSearch(ctx, "idx_not_exist", "only in the body").Result()
273+
Expect(err).To(HaveOccurred())
272274
})
273275

274276
It("should FTSpellCheck", Label("search", "ftcreate", "ftsearch", "ftspellcheck"), func() {
@@ -643,11 +645,25 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
643645
Expect(err).NotTo(HaveOccurred())
644646
Expect(res.Rows[0].Fields["t2"]).To(BeEquivalentTo("world"))
645647

648+
options = &redis.FTAggregateOptions{Load: []redis.FTAggregateLoad{{Field: "t2", As: "t2alias"}}}
649+
res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result()
650+
Expect(err).NotTo(HaveOccurred())
651+
Expect(res.Rows[0].Fields["t2alias"]).To(BeEquivalentTo("world"))
652+
653+
options = &redis.FTAggregateOptions{Load: []redis.FTAggregateLoad{{Field: "t1"}, {Field: "t2", As: "t2alias"}}}
654+
res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result()
655+
Expect(err).NotTo(HaveOccurred())
656+
Expect(res.Rows[0].Fields["t1"]).To(BeEquivalentTo("hello"))
657+
Expect(res.Rows[0].Fields["t2alias"]).To(BeEquivalentTo("world"))
658+
646659
options = &redis.FTAggregateOptions{LoadAll: true}
647660
res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result()
648661
Expect(err).NotTo(HaveOccurred())
649662
Expect(res.Rows[0].Fields["t1"]).To(BeEquivalentTo("hello"))
650663
Expect(res.Rows[0].Fields["t2"]).To(BeEquivalentTo("world"))
664+
665+
_, err = client.FTAggregateWithArgs(ctx, "idx_not_exist", "*", &redis.FTAggregateOptions{}).Result()
666+
Expect(err).To(HaveOccurred())
651667
})
652668

653669
It("should FTAggregate with scorer and addscores", Label("search", "ftaggregate", "NonRedisEnterprise"), func() {
@@ -1268,6 +1284,7 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
12681284
val, err = client.FTCreate(ctx, "idx_hash", ftCreateOptions, schema...).Result()
12691285
Expect(err).NotTo(HaveOccurred())
12701286
Expect(val).To(Equal("OK"))
1287+
WaitForIndexing(client, "idx_hash")
12711288

12721289
ftSearchOptions := &redis.FTSearchOptions{
12731290
DialectVersion: 4,

0 commit comments

Comments
 (0)