From 77a3aed8a0e95c948e355d997b250aab933666ab Mon Sep 17 00:00:00 2001 From: herodot Date: Thu, 9 Jan 2025 23:20:08 +0800 Subject: [PATCH 1/5] fix (#3220) * LOAD has NO AS param(https://redis.io/docs/latest/commands/ft.aggregate/) * fix typo: WITHCOUT -> WITHCOUNT --- search_commands.go | 18 ++++-------------- search_test.go | 12 ++++++++++-- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/search_commands.go b/search_commands.go index ede084e4e..c0546651c 100644 --- a/search_commands.go +++ b/search_commands.go @@ -231,7 +231,6 @@ type FTAggregateApply struct { type FTAggregateLoad struct { Field string - As string } type FTAggregateWithCursor struct { @@ -493,9 +492,6 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery queryArgs = append(queryArgs, "LOAD", len(options.Load)) for _, load := range options.Load { queryArgs = append(queryArgs, load.Field) - if load.As != "" { - queryArgs = append(queryArgs, "AS", load.As) - } } } if options.Timeout > 0 { @@ -653,8 +649,7 @@ func (cmd *AggregateCmd) String() string { func (cmd *AggregateCmd) readReply(rd *proto.Reader) (err error) { data, err := rd.ReadSlice() if err != nil { - cmd.err = err - return nil + return err } cmd.val, err = ProcessAggregateResult(data) if err != nil { @@ -684,9 +679,6 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st args = append(args, "LOAD", len(options.Load)) for _, load := range options.Load { args = append(args, load.Field) - if load.As != "" { - args = append(args, "AS", load.As) - } } } if options.Timeout > 0 { @@ -1473,8 +1465,7 @@ func (cmd *FTSpellCheckCmd) RawResult() (interface{}, error) { func (cmd *FTSpellCheckCmd) readReply(rd *proto.Reader) (err error) { data, err := rd.ReadSlice() if err != nil { - cmd.err = err - return nil + return err } cmd.val, err = parseFTSpellCheck(data) if err != nil { @@ -1662,8 +1653,7 @@ func (cmd *FTSearchCmd) RawResult() (interface{}, error) { func (cmd *FTSearchCmd) readReply(rd *proto.Reader) (err error) { data, err := rd.ReadSlice() if err != nil { - cmd.err = err - return nil + return err } cmd.val, err = parseFTSearch(data, cmd.options.NoContent, cmd.options.WithScores, cmd.options.WithPayloads, cmd.options.WithSortKeys) if err != nil { @@ -1889,7 +1879,7 @@ func (c cmdable) FTSearchWithArgs(ctx context.Context, index string, query strin } } if options.SortByWithCount { - args = append(args, "WITHCOUT") + args = append(args, "WITHCOUNT") } } if options.LimitOffset >= 0 && options.Limit > 0 { diff --git a/search_test.go b/search_test.go index e267c8ae8..3a27a8697 100644 --- a/search_test.go +++ b/search_test.go @@ -127,8 +127,10 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { res3, err := client.FTSearchWithArgs(ctx, "num", "foo", &redis.FTSearchOptions{NoContent: true, SortBy: []redis.FTSearchSortBy{sortBy2}, SortByWithCount: true}).Result() Expect(err).NotTo(HaveOccurred()) - Expect(res3.Total).To(BeEquivalentTo(int64(0))) - + Expect(res3.Total).To(BeEquivalentTo(int64(3))) + Expect(res2.Docs[2].ID).To(BeEquivalentTo("doc1")) + Expect(res2.Docs[1].ID).To(BeEquivalentTo("doc2")) + Expect(res2.Docs[0].ID).To(BeEquivalentTo("doc3")) }) It("should FTCreate and FTSearch example", Label("search", "ftcreate", "ftsearch"), func() { @@ -264,6 +266,8 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { Expect(err).NotTo(HaveOccurred()) Expect(res1.Total).To(BeEquivalentTo(int64(1))) + _, err = client.FTSearch(ctx, "idx_not_exist", "only in the body").Result() + Expect(err).To(HaveOccurred()) }) It("should FTSpellCheck", Label("search", "ftcreate", "ftsearch", "ftspellcheck"), func() { @@ -592,6 +596,9 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { Expect(err).NotTo(HaveOccurred()) Expect(res.Rows[0].Fields["t1"]).To(BeEquivalentTo("hello")) Expect(res.Rows[0].Fields["t2"]).To(BeEquivalentTo("world")) + + _, err = client.FTAggregateWithArgs(ctx, "idx_not_exist", "*", &redis.FTAggregateOptions{}).Result() + Expect(err).To(HaveOccurred()) }) It("should FTAggregate apply", Label("search", "ftaggregate"), func() { @@ -1101,6 +1108,7 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { val, err = client.FTCreate(ctx, "idx_hash", ftCreateOptions, schema...).Result() Expect(err).NotTo(HaveOccurred()) Expect(val).To(Equal("OK")) + WaitForIndexing(client, "idx_hash") ftSearchOptions := &redis.FTSearchOptions{ DialectVersion: 4, From 3dcbf9ed8aa80149e3f5afdd402b559565564061 Mon Sep 17 00:00:00 2001 From: herodot Date: Thu, 6 Feb 2025 23:51:04 +0800 Subject: [PATCH 2/5] fix (#3220): * Compatible with known RediSearch issue in test --- search_test.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/search_test.go b/search_test.go index 3c7cc41dc..66cff5de7 100644 --- a/search_test.go +++ b/search_test.go @@ -644,7 +644,14 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { Expect(res.Rows[0].Fields["t2"]).To(BeEquivalentTo("world")) _, err = client.FTAggregateWithArgs(ctx, "idx_not_exist", "*", &redis.FTAggregateOptions{}).Result() - Expect(err).To(HaveOccurred()) + if !RECluster { + Expect(err).To(HaveOccurred()) + } else { + //FIXME:This is a known issue, which is fixed in RedisSearch(2.8.10), but the Redis Enterprise(7.4.2) packaged RediSearch 2.8.9 + //https://github.com/RediSearch/RediSearch/issues/4272 + //https://redis.io/docs/latest/operate/rs/release-notes/rs-7-4-2-releases/rs-7-4-2-54/ + Expect(err).NotTo(HaveOccurred()) + } }) It("should FTAggregate apply", Label("search", "ftaggregate"), func() { From fd47606def3cbf9ca7614e9653ad425be35a9952 Mon Sep 17 00:00:00 2001 From: herodot Date: Fri, 7 Feb 2025 22:21:04 +0800 Subject: [PATCH 3/5] fix (#3220) * fixed the calculation bug of the count of load params --- search_commands.go | 15 +++++++++++++++ search_test.go | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/search_commands.go b/search_commands.go index 7ba53ef67..955e62ae9 100644 --- a/search_commands.go +++ b/search_commands.go @@ -231,6 +231,7 @@ type FTAggregateApply struct { type FTAggregateLoad struct { Field string + As string } type FTAggregateWithCursor struct { @@ -497,9 +498,16 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery } if options.Load != nil { queryArgs = append(queryArgs, "LOAD", len(options.Load)) + index, count := len(queryArgs)-1, 0 for _, load := range options.Load { queryArgs = append(queryArgs, load.Field) + count++ + if load.As != "" { + queryArgs = append(queryArgs, "AS", load.As) + count += 2 + } } + queryArgs[index] = count } if options.Timeout > 0 { queryArgs = append(queryArgs, "TIMEOUT", options.Timeout) @@ -684,9 +692,16 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st } if options.Load != nil { args = append(args, "LOAD", len(options.Load)) + index, count := len(args)-1, 0 for _, load := range options.Load { args = append(args, load.Field) + count++ + if load.As != "" { + args = append(args, "AS", load.As) + count += 2 + } } + args[index] = count } if options.Timeout > 0 { args = append(args, "TIMEOUT", options.Timeout) diff --git a/search_test.go b/search_test.go index 66cff5de7..ed8b134dc 100644 --- a/search_test.go +++ b/search_test.go @@ -637,6 +637,17 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { Expect(err).NotTo(HaveOccurred()) Expect(res.Rows[0].Fields["t2"]).To(BeEquivalentTo("world")) + options = &redis.FTAggregateOptions{Load: []redis.FTAggregateLoad{{Field: "t2", As: "t2alias"}}} + res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Rows[0].Fields["t2alias"]).To(BeEquivalentTo("world")) + + options = &redis.FTAggregateOptions{Load: []redis.FTAggregateLoad{{Field: "t1"}, {Field: "t2", As: "t2alias"}}} + res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Rows[0].Fields["t1"]).To(BeEquivalentTo("hello")) + Expect(res.Rows[0].Fields["t2alias"]).To(BeEquivalentTo("world")) + options = &redis.FTAggregateOptions{LoadAll: true} res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result() Expect(err).NotTo(HaveOccurred()) From d3b325c515a22849b77d7c969431ac04eae16bfd Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Thu, 20 Feb 2025 14:06:27 +0200 Subject: [PATCH 4/5] test should not include special condition --- search_test.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/search_test.go b/search_test.go index 4a138e0c3..ea3460d3d 100644 --- a/search_test.go +++ b/search_test.go @@ -663,14 +663,7 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { Expect(res.Rows[0].Fields["t2"]).To(BeEquivalentTo("world")) _, err = client.FTAggregateWithArgs(ctx, "idx_not_exist", "*", &redis.FTAggregateOptions{}).Result() - if !RECluster { - Expect(err).To(HaveOccurred()) - } else { - //FIXME:This is a known issue, which is fixed in RedisSearch(2.8.10), but the Redis Enterprise(7.4.2) packaged RediSearch 2.8.9 - //https://github.com/RediSearch/RediSearch/issues/4272 - //https://redis.io/docs/latest/operate/rs/release-notes/rs-7-4-2-releases/rs-7-4-2-54/ - Expect(err).NotTo(HaveOccurred()) - } + Expect(err).To(HaveOccurred()) }) It("should FTAggregate with scorer and addscores", Label("search", "ftaggregate", "NonRedisEnterprise"), func() { From 3f6e5a8e3ab6831d3596de44958397179408752a Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Thu, 20 Feb 2025 14:11:46 +0200 Subject: [PATCH 5/5] return errors when they occur --- search_commands.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/search_commands.go b/search_commands.go index b77267102..71ee6ab32 100644 --- a/search_commands.go +++ b/search_commands.go @@ -681,12 +681,10 @@ func (cmd *AggregateCmd) String() string { func (cmd *AggregateCmd) readReply(rd *proto.Reader) (err error) { data, err := rd.ReadSlice() if err != nil { - cmd.err = err return err } cmd.val, err = ProcessAggregateResult(data) if err != nil { - cmd.err = err return err } return nil @@ -1428,7 +1426,7 @@ func (cmd *FTInfoCmd) readReply(rd *proto.Reader) (err error) { } cmd.val, err = parseFTInfo(data) if err != nil { - cmd.err = err + return err } return nil @@ -1525,7 +1523,7 @@ func (cmd *FTSpellCheckCmd) readReply(rd *proto.Reader) (err error) { } cmd.val, err = parseFTSpellCheck(data) if err != nil { - cmd.err = err + return err } return nil } @@ -1713,7 +1711,7 @@ func (cmd *FTSearchCmd) readReply(rd *proto.Reader) (err error) { } cmd.val, err = parseFTSearch(data, cmd.options.NoContent, cmd.options.WithScores, cmd.options.WithPayloads, cmd.options.WithSortKeys) if err != nil { - cmd.err = err + return err } return nil }