Skip to content

Commit 81e383d

Browse files
bitsarkndyakovofekshenawa
committed
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 dfd84ac commit 81e383d

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
@@ -502,12 +502,16 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery
502502
}
503503
if options.Load != nil {
504504
queryArgs = append(queryArgs, "LOAD", len(options.Load))
505+
index, count := len(queryArgs)-1, 0
505506
for _, load := range options.Load {
506507
queryArgs = append(queryArgs, load.Field)
508+
count++
507509
if load.As != "" {
508510
queryArgs = append(queryArgs, "AS", load.As)
511+
count += 2
509512
}
510513
}
514+
queryArgs[index] = count
511515
}
512516

513517
if options.Timeout > 0 {
@@ -665,12 +669,10 @@ func (cmd *AggregateCmd) String() string {
665669
func (cmd *AggregateCmd) readReply(rd *proto.Reader) (err error) {
666670
data, err := rd.ReadSlice()
667671
if err != nil {
668-
cmd.err = err
669672
return err
670673
}
671674
cmd.val, err = ProcessAggregateResult(data)
672675
if err != nil {
673-
cmd.err = err
674676
return err
675677
}
676678
return nil
@@ -701,12 +703,16 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st
701703
}
702704
if options.Load != nil {
703705
args = append(args, "LOAD", len(options.Load))
706+
index, count := len(args)-1, 0
704707
for _, load := range options.Load {
705708
args = append(args, load.Field)
709+
count++
706710
if load.As != "" {
707711
args = append(args, "AS", load.As)
712+
count += 2
708713
}
709714
}
715+
args[index] = count
710716
}
711717
if options.Timeout > 0 {
712718
args = append(args, "TIMEOUT", options.Timeout)
@@ -1396,7 +1402,7 @@ func (cmd *FTInfoCmd) readReply(rd *proto.Reader) (err error) {
13961402
}
13971403
cmd.val, err = parseFTInfo(data)
13981404
if err != nil {
1399-
cmd.err = err
1405+
return err
14001406
}
14011407

14021408
return nil
@@ -1489,12 +1495,11 @@ func (cmd *FTSpellCheckCmd) RawResult() (interface{}, error) {
14891495
func (cmd *FTSpellCheckCmd) readReply(rd *proto.Reader) (err error) {
14901496
data, err := rd.ReadSlice()
14911497
if err != nil {
1492-
cmd.err = err
1493-
return nil
1498+
return err
14941499
}
14951500
cmd.val, err = parseFTSpellCheck(data)
14961501
if err != nil {
1497-
cmd.err = err
1502+
return err
14981503
}
14991504
return nil
15001505
}
@@ -1678,12 +1683,11 @@ func (cmd *FTSearchCmd) RawResult() (interface{}, error) {
16781683
func (cmd *FTSearchCmd) readReply(rd *proto.Reader) (err error) {
16791684
data, err := rd.ReadSlice()
16801685
if err != nil {
1681-
cmd.err = err
1682-
return nil
1686+
return err
16831687
}
16841688
cmd.val, err = parseFTSearch(data, cmd.options.NoContent, cmd.options.WithScores, cmd.options.WithPayloads, cmd.options.WithSortKeys)
16851689
if err != nil {
1686-
cmd.err = err
1690+
return err
16871691
}
16881692
return nil
16891693
}

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() {
@@ -597,11 +599,25 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
597599
Expect(err).NotTo(HaveOccurred())
598600
Expect(res.Rows[0].Fields["t2"]).To(BeEquivalentTo("world"))
599601

602+
options = &redis.FTAggregateOptions{Load: []redis.FTAggregateLoad{{Field: "t2", As: "t2alias"}}}
603+
res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result()
604+
Expect(err).NotTo(HaveOccurred())
605+
Expect(res.Rows[0].Fields["t2alias"]).To(BeEquivalentTo("world"))
606+
607+
options = &redis.FTAggregateOptions{Load: []redis.FTAggregateLoad{{Field: "t1"}, {Field: "t2", As: "t2alias"}}}
608+
res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result()
609+
Expect(err).NotTo(HaveOccurred())
610+
Expect(res.Rows[0].Fields["t1"]).To(BeEquivalentTo("hello"))
611+
Expect(res.Rows[0].Fields["t2alias"]).To(BeEquivalentTo("world"))
612+
600613
options = &redis.FTAggregateOptions{LoadAll: true}
601614
res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result()
602615
Expect(err).NotTo(HaveOccurred())
603616
Expect(res.Rows[0].Fields["t1"]).To(BeEquivalentTo("hello"))
604617
Expect(res.Rows[0].Fields["t2"]).To(BeEquivalentTo("world"))
618+
619+
_, err = client.FTAggregateWithArgs(ctx, "idx_not_exist", "*", &redis.FTAggregateOptions{}).Result()
620+
Expect(err).To(HaveOccurred())
605621
})
606622

607623
It("should FTAggregate with scorer and addscores", Label("search", "ftaggregate", "NonRedisEnterprise"), func() {
@@ -1203,6 +1219,7 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
12031219
val, err = client.FTCreate(ctx, "idx_hash", ftCreateOptions, schema...).Result()
12041220
Expect(err).NotTo(HaveOccurred())
12051221
Expect(val).To(Equal("OK"))
1222+
WaitForIndexing(client, "idx_hash")
12061223

12071224
ftSearchOptions := &redis.FTSearchOptions{
12081225
DialectVersion: 4,

0 commit comments

Comments
 (0)