From 3d041a1dd6974412c95803384965d73a50bf07ab Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Fri, 21 Feb 2025 15:40:07 +0200 Subject: [PATCH] release: 9.7.1 patch (#3278) * Add guidance on unstable RESP3 support for RediSearch commands to README (#3177) * Add UnstableResp3 to docs * Add RawVal and RawResult to wordlist * Explain more about SetVal * Add UnstableResp to wordlist * Eliminate redundant dial mutex causing unbounded connection queue contention (#3088) * Eliminate redundant dial mutex causing unbounded connection queue contention * Dialer connection timeouts unit test --------- Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com> * SortByWithCount FTSearchOptions fix (#3201) * SortByWithCount FTSearchOptions fix * FTSearch test fix * Another FTSearch test fix * Another FTSearch test fix --------- Co-authored-by: Christopher Golling * Fix race condition in clusterNodes.Addrs() (#3219) Resolve a race condition in the clusterNodes.Addrs() method. Previously, the method returned a reference to a string slice, creating the potential for concurrent reads by the caller while the slice was being modified by the garbage collection process. Co-authored-by: Nedyalko Dyakov * chore: fix some comments (#3226) Signed-off-by: zhuhaicity Co-authored-by: Nedyalko Dyakov * fix(aggregate, search): ft.aggregate bugfixes (#3263) * fix: rearange args for ft.aggregate apply should be before any groupby or sortby * improve test * wip: add scorer and addscores * enable all tests * fix ftsearch with count test * make linter happy * Addscores is available in later redisearch releases. For safety state it is available in redis ce 8 * load an apply seem to break scorer and addscores * fix: add unstableresp3 to cluster client (#3266) * fix: add unstableresp3 to cluster client * propagate unstableresp3 * proper test that will ignore error, but fail if client panics * add separate test for clusterclient constructor * fix: flaky ClientKillByFilter test (#3268) * Reinstate read-only lock on hooks access in dialHook (#3225) * use limit when limitoffset is zero (#3275) * remove redis 8 comments * update package versions * use latest golangci-lint * 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 Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com> * Recognize byte slice for key argument in cluster client hash slot computation (#3049) Co-authored-by: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com> --------- Signed-off-by: zhuhaicity Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com> Co-authored-by: LINKIWI Co-authored-by: Cgol9 Co-authored-by: Christopher Golling Co-authored-by: Shawn Wang <62313353+shawnwgit@users.noreply.github.com> Co-authored-by: ZhuHaiCheng Co-authored-by: herodot <54836727+bitsark@users.noreply.github.com> Co-authored-by: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> --- .github/wordlist.txt | 3 + .github/workflows/golangci-lint.yml | 6 +- .golangci.yml | 3 +- README.md | 15 ++++ command.go | 2 + commands_test.go | 2 +- example/del-keys-without-ttl/go.mod | 2 +- example/hll/go.mod | 2 +- example/lua-scripting/go.mod | 2 +- example/otel/go.mod | 6 +- example/redis-bloom/go.mod | 2 +- example/scan-struct/go.mod | 2 +- extra/rediscensus/go.mod | 4 +- extra/rediscmd/go.mod | 2 +- extra/redisotel/go.mod | 4 +- extra/redisprometheus/go.mod | 2 +- hash_commands.go | 2 +- options.go | 2 +- osscluster.go | 12 ++- osscluster_test.go | 26 ++++++ redis.go | 17 ++-- redis_test.go | 65 ++++++++++++++ search_commands.go | 100 +++++++++++++-------- search_test.go | 129 +++++++++++++++++++++++++++- universal.go | 1 + universal_test.go | 22 +++++ version.go | 2 +- 27 files changed, 365 insertions(+), 72 deletions(-) diff --git a/.github/wordlist.txt b/.github/wordlist.txt index c200c60b..1fc34f73 100644 --- a/.github/wordlist.txt +++ b/.github/wordlist.txt @@ -54,6 +54,7 @@ stunnel SynDump TCP TLS +UnstableResp uri URI url @@ -62,3 +63,5 @@ RedisStack RedisGears RedisTimeseries RediSearch +RawResult +RawVal \ No newline at end of file diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 5210ccfa..d9e53f70 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -12,15 +12,13 @@ on: permissions: contents: read + pull-requests: read # for golangci/golangci-lint-action to fetch pull requests jobs: golangci: - permissions: - contents: read # for actions/checkout to fetch code - pull-requests: read # for golangci/golangci-lint-action to fetch pull requests name: lint runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: golangci-lint - uses: golangci/golangci-lint-action@v6 + uses: golangci/golangci-lint-action@v6.5.0 diff --git a/.golangci.yml b/.golangci.yml index de514554..285aca6b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,4 +1,3 @@ run: - concurrency: 8 - deadline: 5m + timeout: 5m tests: false diff --git a/README.md b/README.md index 37714a97..e7136765 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,21 @@ rdb := redis.NewClient(&redis.Options{ #### Unstable RESP3 Structures for RediSearch Commands When integrating Redis with application functionalities using RESP3, it's important to note that some response structures aren't final yet. This is especially true for more complex structures like search and query results. We recommend using RESP2 when using the search and query capabilities, but we plan to stabilize the RESP3-based API-s in the coming versions. You can find more guidance in the upcoming release notes. +To enable unstable RESP3, set the option in your client configuration: + +```go +redis.NewClient(&redis.Options{ + UnstableResp3: true, + }) +``` +**Note:** When UnstableResp3 mode is enabled, it's necessary to use RawResult() and RawVal() to retrieve a raw data. + Since, raw response is the only option for unstable search commands Val() and Result() calls wouldn't have any affect on them: + +```go +res1, err := client.FTSearchWithArgs(ctx, "txt", "foo bar", &redis.FTSearchOptions{}).RawResult() +val1 := client.FTSearchWithArgs(ctx, "txt", "foo bar", &redis.FTSearchOptions{}).RawVal() +``` + ## Contributing Please see [out contributing guidelines](CONTRIBUTING.md) to help us improve this library! diff --git a/command.go b/command.go index 4ced2979..f3d0e49b 100644 --- a/command.go +++ b/command.go @@ -167,6 +167,8 @@ func (cmd *baseCmd) stringArg(pos int) string { switch v := arg.(type) { case string: return v + case []byte: + return string(v) default: // TODO: consider using appendArg return fmt.Sprint(v) diff --git a/commands_test.go b/commands_test.go index 9554bf9a..64800705 100644 --- a/commands_test.go +++ b/commands_test.go @@ -217,7 +217,7 @@ var _ = Describe("Commands", func() { killed := client.ClientKillByFilter(ctx, "MAXAGE", "1") Expect(killed.Err()).NotTo(HaveOccurred()) - Expect(killed.Val()).To(SatisfyAny(Equal(int64(2)), Equal(int64(3)))) + Expect(killed.Val()).To(BeNumerically(">=", 2)) select { case <-done: diff --git a/example/del-keys-without-ttl/go.mod b/example/del-keys-without-ttl/go.mod index 6b59a87a..eee5e9c2 100644 --- a/example/del-keys-without-ttl/go.mod +++ b/example/del-keys-without-ttl/go.mod @@ -5,7 +5,7 @@ go 1.18 replace github.com/redis/go-redis/v9 => ../.. require ( - github.com/redis/go-redis/v9 v9.7.0 + github.com/redis/go-redis/v9 v9.7.1 go.uber.org/zap v1.24.0 ) diff --git a/example/hll/go.mod b/example/hll/go.mod index 5bff57e6..9346c153 100644 --- a/example/hll/go.mod +++ b/example/hll/go.mod @@ -4,7 +4,7 @@ go 1.18 replace github.com/redis/go-redis/v9 => ../.. -require github.com/redis/go-redis/v9 v9.7.0 +require github.com/redis/go-redis/v9 v9.7.1 require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect diff --git a/example/lua-scripting/go.mod b/example/lua-scripting/go.mod index 8ca0910e..382ffb57 100644 --- a/example/lua-scripting/go.mod +++ b/example/lua-scripting/go.mod @@ -4,7 +4,7 @@ go 1.18 replace github.com/redis/go-redis/v9 => ../.. -require github.com/redis/go-redis/v9 v9.7.0 +require github.com/redis/go-redis/v9 v9.7.1 require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect diff --git a/example/otel/go.mod b/example/otel/go.mod index 28b791e1..a92878be 100644 --- a/example/otel/go.mod +++ b/example/otel/go.mod @@ -9,8 +9,8 @@ replace github.com/redis/go-redis/extra/redisotel/v9 => ../../extra/redisotel replace github.com/redis/go-redis/extra/rediscmd/v9 => ../../extra/rediscmd require ( - github.com/redis/go-redis/extra/redisotel/v9 v9.7.0 - github.com/redis/go-redis/v9 v9.7.0 + github.com/redis/go-redis/extra/redisotel/v9 v9.7.1 + github.com/redis/go-redis/v9 v9.7.1 github.com/uptrace/uptrace-go v1.21.0 go.opentelemetry.io/otel v1.22.0 ) @@ -23,7 +23,7 @@ require ( github.com/go-logr/stdr v1.2.2 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect - github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0 // indirect + github.com/redis/go-redis/extra/rediscmd/v9 v9.7.1 // indirect go.opentelemetry.io/contrib/instrumentation/runtime v0.46.1 // indirect go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.44.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 // indirect diff --git a/example/redis-bloom/go.mod b/example/redis-bloom/go.mod index e968e60f..c5b8f71f 100644 --- a/example/redis-bloom/go.mod +++ b/example/redis-bloom/go.mod @@ -4,7 +4,7 @@ go 1.18 replace github.com/redis/go-redis/v9 => ../.. -require github.com/redis/go-redis/v9 v9.7.0 +require github.com/redis/go-redis/v9 v9.7.1 require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect diff --git a/example/scan-struct/go.mod b/example/scan-struct/go.mod index b850ef6f..7b594604 100644 --- a/example/scan-struct/go.mod +++ b/example/scan-struct/go.mod @@ -6,7 +6,7 @@ replace github.com/redis/go-redis/v9 => ../.. require ( github.com/davecgh/go-spew v1.1.1 - github.com/redis/go-redis/v9 v9.7.0 + github.com/redis/go-redis/v9 v9.7.1 ) require ( diff --git a/extra/rediscensus/go.mod b/extra/rediscensus/go.mod index b9d65195..4da17c55 100644 --- a/extra/rediscensus/go.mod +++ b/extra/rediscensus/go.mod @@ -7,8 +7,8 @@ replace github.com/redis/go-redis/v9 => ../.. replace github.com/redis/go-redis/extra/rediscmd/v9 => ../rediscmd require ( - github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0 - github.com/redis/go-redis/v9 v9.7.0 + github.com/redis/go-redis/extra/rediscmd/v9 v9.7.1 + github.com/redis/go-redis/v9 v9.7.1 go.opencensus.io v0.24.0 ) diff --git a/extra/rediscmd/go.mod b/extra/rediscmd/go.mod index 794985bb..3b18846e 100644 --- a/extra/rediscmd/go.mod +++ b/extra/rediscmd/go.mod @@ -7,7 +7,7 @@ replace github.com/redis/go-redis/v9 => ../.. require ( github.com/bsm/ginkgo/v2 v2.12.0 github.com/bsm/gomega v1.27.10 - github.com/redis/go-redis/v9 v9.7.0 + github.com/redis/go-redis/v9 v9.7.1 ) require ( diff --git a/extra/redisotel/go.mod b/extra/redisotel/go.mod index e40bd207..c0d853e7 100644 --- a/extra/redisotel/go.mod +++ b/extra/redisotel/go.mod @@ -7,8 +7,8 @@ replace github.com/redis/go-redis/v9 => ../.. replace github.com/redis/go-redis/extra/rediscmd/v9 => ../rediscmd require ( - github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0 - github.com/redis/go-redis/v9 v9.7.0 + github.com/redis/go-redis/extra/rediscmd/v9 v9.7.1 + github.com/redis/go-redis/v9 v9.7.1 go.opentelemetry.io/otel v1.22.0 go.opentelemetry.io/otel/metric v1.22.0 go.opentelemetry.io/otel/sdk v1.22.0 diff --git a/extra/redisprometheus/go.mod b/extra/redisprometheus/go.mod index 33d8a6a4..69f70184 100644 --- a/extra/redisprometheus/go.mod +++ b/extra/redisprometheus/go.mod @@ -6,7 +6,7 @@ replace github.com/redis/go-redis/v9 => ../.. require ( github.com/prometheus/client_golang v1.14.0 - github.com/redis/go-redis/v9 v9.7.0 + github.com/redis/go-redis/v9 v9.7.1 ) require ( diff --git a/hash_commands.go b/hash_commands.go index dcffdcdd..6596c6f5 100644 --- a/hash_commands.go +++ b/hash_commands.go @@ -225,7 +225,7 @@ func (c cmdable) HExpire(ctx context.Context, key string, expiration time.Durati return cmd } -// HExpire - Sets the expiration time for specified fields in a hash in seconds. +// HExpireWithArgs - Sets the expiration time for specified fields in a hash in seconds. // It requires a key, an expiration duration, a struct with boolean flags for conditional expiration settings (NX, XX, GT, LT), and a list of fields. // The command constructs an argument list starting with "HEXPIRE", followed by the key, duration, any conditional flags, and the specified fields. // For more information - https://redis.io/commands/hexpire/ diff --git a/options.go b/options.go index 8ba74ccd..60ff1ee4 100644 --- a/options.go +++ b/options.go @@ -154,7 +154,7 @@ type Options struct { // Add suffix to client name. Default is empty. IdentitySuffix string - // Enable Unstable mode for Redis Search module with RESP3. + // UnstableResp3 enables Unstable mode for Redis Search module with RESP3. UnstableResp3 bool } diff --git a/osscluster.go b/osscluster.go index ce258ff3..9268120b 100644 --- a/osscluster.go +++ b/osscluster.go @@ -90,6 +90,9 @@ type ClusterOptions struct { DisableIndentity bool // Disable set-lib on connect. Default is false. IdentitySuffix string // Add suffix to client name. Default is empty. + + // UnstableResp3 enables Unstable mode for Redis Search module with RESP3. + UnstableResp3 bool } func (opt *ClusterOptions) init() { @@ -304,7 +307,8 @@ func (opt *ClusterOptions) clientOptions() *Options { // much use for ClusterSlots config). This means we cannot execute the // READONLY command against that node -- setting readOnly to false in such // situations in the options below will prevent that from happening. - readOnly: opt.ReadOnly && opt.ClusterSlots == nil, + readOnly: opt.ReadOnly && opt.ClusterSlots == nil, + UnstableResp3: opt.UnstableResp3, } } @@ -465,9 +469,11 @@ func (c *clusterNodes) Addrs() ([]string, error) { closed := c.closed //nolint:ifshort if !closed { if len(c.activeAddrs) > 0 { - addrs = c.activeAddrs + addrs = make([]string, len(c.activeAddrs)) + copy(addrs, c.activeAddrs) } else { - addrs = c.addrs + addrs = make([]string, len(c.addrs)) + copy(addrs, c.addrs) } } c.mu.RUnlock() diff --git a/osscluster_test.go b/osscluster_test.go index f7bd1683..9c3eaba3 100644 --- a/osscluster_test.go +++ b/osscluster_test.go @@ -653,6 +653,32 @@ var _ = Describe("ClusterClient", func() { Expect(client.Close()).NotTo(HaveOccurred()) }) + It("determines hash slots correctly for generic commands", func() { + opt := redisClusterOptions() + opt.MaxRedirects = -1 + client := cluster.newClusterClient(ctx, opt) + + err := client.Do(ctx, "GET", "A").Err() + Expect(err).To(Equal(redis.Nil)) + + err = client.Do(ctx, []byte("GET"), []byte("A")).Err() + Expect(err).To(Equal(redis.Nil)) + + Eventually(func() error { + return client.SwapNodes(ctx, "A") + }, 30*time.Second).ShouldNot(HaveOccurred()) + + err = client.Do(ctx, "GET", "A").Err() + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("MOVED")) + + err = client.Do(ctx, []byte("GET"), []byte("A")).Err() + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("MOVED")) + + Expect(client.Close()).NotTo(HaveOccurred()) + }) + It("follows node redirection immediately", func() { // Configure retry backoffs far in excess of the expected duration of redirection opt := redisClusterOptions() diff --git a/redis.go b/redis.go index c8b50080..ec3ff616 100644 --- a/redis.go +++ b/redis.go @@ -41,7 +41,7 @@ type ( ) type hooksMixin struct { - hooksMu *sync.Mutex + hooksMu *sync.RWMutex slice []Hook initial hooks @@ -49,7 +49,7 @@ type hooksMixin struct { } func (hs *hooksMixin) initHooks(hooks hooks) { - hs.hooksMu = new(sync.Mutex) + hs.hooksMu = new(sync.RWMutex) hs.initial = hooks hs.chain() } @@ -151,7 +151,7 @@ func (hs *hooksMixin) clone() hooksMixin { clone := *hs l := len(clone.slice) clone.slice = clone.slice[:l:l] - clone.hooksMu = new(sync.Mutex) + clone.hooksMu = new(sync.RWMutex) return clone } @@ -176,9 +176,14 @@ func (hs *hooksMixin) withProcessPipelineHook( } func (hs *hooksMixin) dialHook(ctx context.Context, network, addr string) (net.Conn, error) { - hs.hooksMu.Lock() - defer hs.hooksMu.Unlock() - return hs.current.dial(ctx, network, addr) + // Access to hs.current is guarded by a read-only lock since it may be mutated by AddHook(...) + // while this dialer is concurrently accessed by the background connection pool population + // routine when MinIdleConns > 0. + hs.hooksMu.RLock() + current := hs.current + hs.hooksMu.RUnlock() + + return current.dial(ctx, network, addr) } func (hs *hooksMixin) processHook(ctx context.Context, cmd Cmder) error { diff --git a/redis_test.go b/redis_test.go index ef212545..b5cf2570 100644 --- a/redis_test.go +++ b/redis_test.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "net" + "sync" "testing" "time" @@ -633,3 +634,67 @@ var _ = Describe("Hook with MinIdleConns", func() { })) }) }) + +var _ = Describe("Dialer connection timeouts", func() { + var client *redis.Client + + const dialSimulatedDelay = 1 * time.Second + + BeforeEach(func() { + options := redisOptions() + options.Dialer = func(ctx context.Context, network, addr string) (net.Conn, error) { + // Simulated slow dialer. + // Note that the following sleep is deliberately not context-aware. + time.Sleep(dialSimulatedDelay) + return net.Dial("tcp", options.Addr) + } + options.MinIdleConns = 1 + client = redis.NewClient(options) + }) + + AfterEach(func() { + err := client.Close() + Expect(err).NotTo(HaveOccurred()) + }) + + It("does not contend on connection dial for concurrent commands", func() { + var wg sync.WaitGroup + + const concurrency = 10 + + durations := make(chan time.Duration, concurrency) + errs := make(chan error, concurrency) + + start := time.Now() + wg.Add(concurrency) + + for i := 0; i < concurrency; i++ { + go func() { + defer wg.Done() + + start := time.Now() + err := client.Ping(ctx).Err() + durations <- time.Since(start) + errs <- err + }() + } + + wg.Wait() + close(durations) + close(errs) + + // All commands should eventually succeed, after acquiring a connection. + for err := range errs { + Expect(err).NotTo(HaveOccurred()) + } + + // Each individual command should complete within the simulated dial duration bound. + for duration := range durations { + Expect(duration).To(BeNumerically("<", 2*dialSimulatedDelay)) + } + + // Due to concurrent execution, the entire test suite should also complete within + // the same dial duration bound applied for individual commands. + Expect(time.Since(start)).To(BeNumerically("<", 2*dialSimulatedDelay)) + }) +}) diff --git a/search_commands.go b/search_commands.go index e4df0b6f..9359a723 100644 --- a/search_commands.go +++ b/search_commands.go @@ -247,6 +247,8 @@ type FTAggregateOptions struct { GroupBy []FTAggregateGroupBy SortBy []FTAggregateSortBy SortByMax int + Scorer string + AddScores bool Apply []FTAggregateApply LimitOffset int Limit int @@ -483,6 +485,15 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery if options.Verbatim { queryArgs = append(queryArgs, "VERBATIM") } + + if options.Scorer != "" { + queryArgs = append(queryArgs, "SCORER", options.Scorer) + } + + if options.AddScores { + queryArgs = append(queryArgs, "ADDSCORES") + } + if options.LoadAll && options.Load != nil { panic("FT.AGGREGATE: LOADALL and LOAD are mutually exclusive") } @@ -491,16 +502,29 @@ 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) } + + for _, apply := range options.Apply { + queryArgs = append(queryArgs, "APPLY", apply.Field) + if apply.As != "" { + queryArgs = append(queryArgs, "AS", apply.As) + } + } + if options.GroupBy != nil { for _, groupBy := range options.GroupBy { queryArgs = append(queryArgs, "GROUPBY", len(groupBy.Fields)) @@ -542,17 +566,8 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery if options.SortByMax > 0 { queryArgs = append(queryArgs, "MAX", options.SortByMax) } - for _, apply := range options.Apply { - queryArgs = append(queryArgs, "APPLY", apply.Field) - if apply.As != "" { - queryArgs = append(queryArgs, "AS", apply.As) - } - } - if options.LimitOffset > 0 { - queryArgs = append(queryArgs, "LIMIT", options.LimitOffset) - } - if options.Limit > 0 { - queryArgs = append(queryArgs, options.Limit) + if options.LimitOffset >= 0 && options.Limit > 0 { + queryArgs = append(queryArgs, "LIMIT", options.LimitOffset, options.Limit) } if options.Filter != "" { queryArgs = append(queryArgs, "FILTER", options.Filter) @@ -574,6 +589,7 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery queryArgs = append(queryArgs, key, value) } } + if options.DialectVersion > 0 { queryArgs = append(queryArgs, "DIALECT", options.DialectVersion) } @@ -653,12 +669,11 @@ 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 { - cmd.err = err + return err } return nil } @@ -674,6 +689,12 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st if options.Verbatim { args = append(args, "VERBATIM") } + if options.Scorer != "" { + args = append(args, "SCORER", options.Scorer) + } + if options.AddScores { + args = append(args, "ADDSCORES") + } if options.LoadAll && options.Load != nil { panic("FT.AGGREGATE: LOADALL and LOAD are mutually exclusive") } @@ -682,16 +703,26 @@ 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) } + for _, apply := range options.Apply { + args = append(args, "APPLY", apply.Field) + if apply.As != "" { + args = append(args, "AS", apply.As) + } + } if options.GroupBy != nil { for _, groupBy := range options.GroupBy { args = append(args, "GROUPBY", len(groupBy.Fields)) @@ -733,17 +764,8 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st if options.SortByMax > 0 { args = append(args, "MAX", options.SortByMax) } - for _, apply := range options.Apply { - args = append(args, "APPLY", apply.Field) - if apply.As != "" { - args = append(args, "AS", apply.As) - } - } - if options.LimitOffset > 0 { - args = append(args, "LIMIT", options.LimitOffset) - } - if options.Limit > 0 { - args = append(args, options.Limit) + if options.LimitOffset >= 0 && options.Limit > 0 { + args = append(args, "LIMIT", options.LimitOffset, options.Limit) } if options.Filter != "" { args = append(args, "FILTER", options.Filter) @@ -1380,7 +1402,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 @@ -1473,12 +1495,11 @@ 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 { - cmd.err = err + return err } return nil } @@ -1662,19 +1683,19 @@ 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 { - cmd.err = err + return err } return nil } // FTSearch - Executes a search query on an index. // The 'index' parameter specifies the index to search, and the 'query' parameter specifies the search query. -// For more information, please refer to the Redis documentation: +// For more information, please refer to the Redis documentation about [FT.SEARCH]. +// // [FT.SEARCH]: (https://redis.io/commands/ft.search/) func (c cmdable) FTSearch(ctx context.Context, index string, query string) *FTSearchCmd { args := []interface{}{"FT.SEARCH", index, query} @@ -1685,6 +1706,12 @@ func (c cmdable) FTSearch(ctx context.Context, index string, query string) *FTSe type SearchQuery []interface{} +// FTSearchQuery - Executes a search query on an index with additional options. +// The 'index' parameter specifies the index to search, the 'query' parameter specifies the search query, +// and the 'options' parameter specifies additional options for the search. +// For more information, please refer to the Redis documentation about [FT.SEARCH]. +// +// [FT.SEARCH]: (https://redis.io/commands/ft.search/) func FTSearchQuery(query string, options *FTSearchOptions) SearchQuery { queryArgs := []interface{}{query} if options != nil { @@ -1775,7 +1802,7 @@ func FTSearchQuery(query string, options *FTSearchOptions) SearchQuery { } } if options.SortByWithCount { - queryArgs = append(queryArgs, "WITHCOUT") + queryArgs = append(queryArgs, "WITHCOUNT") } } if options.LimitOffset >= 0 && options.Limit > 0 { @@ -1797,7 +1824,8 @@ func FTSearchQuery(query string, options *FTSearchOptions) SearchQuery { // FTSearchWithArgs - Executes a search query on an index with additional options. // The 'index' parameter specifies the index to search, the 'query' parameter specifies the search query, // and the 'options' parameter specifies additional options for the search. -// For more information, please refer to the Redis documentation: +// For more information, please refer to the Redis documentation about [FT.SEARCH]. +// // [FT.SEARCH]: (https://redis.io/commands/ft.search/) func (c cmdable) FTSearchWithArgs(ctx context.Context, index string, query string, options *FTSearchOptions) *FTSearchCmd { args := []interface{}{"FT.SEARCH", index, query} @@ -1889,7 +1917,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 48b9aa39..af98e5f7 100644 --- a/search_test.go +++ b/search_test.go @@ -2,6 +2,8 @@ package redis_test import ( "context" + "fmt" + "strconv" "time" . "github.com/bsm/ginkgo/v2" @@ -125,6 +127,13 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { Expect(res2.Docs[1].ID).To(BeEquivalentTo("doc2")) Expect(res2.Docs[0].ID).To(BeEquivalentTo("doc3")) + 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(3))) + + res4, err := client.FTSearchWithArgs(ctx, "num", "notpresentf00", &redis.FTSearchOptions{NoContent: true, SortBy: []redis.FTSearchSortBy{sortBy2}, SortByWithCount: true}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res4.Total).To(BeEquivalentTo(int64(0))) }) It("should FTCreate and FTSearch example", Label("search", "ftcreate", "ftsearch"), func() { @@ -132,7 +141,7 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { Expect(err).NotTo(HaveOccurred()) Expect(val).To(BeEquivalentTo("OK")) WaitForIndexing(client, "txt") - client.HSet(ctx, "doc1", "title", "RediSearch", "body", "Redisearch impements a search engine on top of redis") + client.HSet(ctx, "doc1", "title", "RediSearch", "body", "Redisearch implements a search engine on top of redis") res1, err := client.FTSearchWithArgs(ctx, "txt", "search engine", &redis.FTSearchOptions{NoContent: true, Verbatim: true, LimitOffset: 0, Limit: 5}).Result() Expect(err).NotTo(HaveOccurred()) Expect(res1.Total).To(BeEquivalentTo(int64(1))) @@ -260,6 +269,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() { @@ -432,7 +443,7 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { WaitForIndexing(client, "idx1") client.HSet(ctx, "search", "title", "RediSearch", - "body", "Redisearch impements a search engine on top of redis", + "body", "Redisearch implements a search engine on top of redis", "parent", "redis", "random_num", 10) client.HSet(ctx, "ai", "title", "RedisAI", @@ -561,6 +572,11 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result() Expect(err).NotTo(HaveOccurred()) Expect(res.Rows[0].Fields["t1"]).To(BeEquivalentTo("b")) + + options = &redis.FTAggregateOptions{SortBy: []redis.FTAggregateSortBy{{FieldName: "@t1"}}, Limit: 1, LimitOffset: 0} + res, err = client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Rows[0].Fields["t1"]).To(BeEquivalentTo("a")) }) It("should FTAggregate load ", Label("search", "ftaggregate"), func() { @@ -583,11 +599,118 @@ 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()) 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 with scorer and addscores", Label("search", "ftaggregate", "NonRedisEnterprise"), func() { + title := &redis.FieldSchema{FieldName: "title", FieldType: redis.SearchFieldTypeText, Sortable: false} + description := &redis.FieldSchema{FieldName: "description", FieldType: redis.SearchFieldTypeText, Sortable: false} + val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{OnHash: true, Prefix: []interface{}{"product:"}}, title, description).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(val).To(BeEquivalentTo("OK")) + WaitForIndexing(client, "idx1") + + client.HSet(ctx, "product:1", "title", "New Gaming Laptop", "description", "this is not a desktop") + client.HSet(ctx, "product:2", "title", "Super Old Not Gaming Laptop", "description", "this laptop is not a new laptop but it is a laptop") + client.HSet(ctx, "product:3", "title", "Office PC", "description", "office desktop pc") + + options := &redis.FTAggregateOptions{ + AddScores: true, + Scorer: "BM25", + SortBy: []redis.FTAggregateSortBy{{ + FieldName: "@__score", + Desc: true, + }}, + } + + res, err := client.FTAggregateWithArgs(ctx, "idx1", "laptop", options).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).ToNot(BeNil()) + Expect(len(res.Rows)).To(BeEquivalentTo(2)) + score1, err := strconv.ParseFloat(fmt.Sprintf("%s", res.Rows[0].Fields["__score"]), 64) + Expect(err).NotTo(HaveOccurred()) + score2, err := strconv.ParseFloat(fmt.Sprintf("%s", res.Rows[1].Fields["__score"]), 64) + Expect(err).NotTo(HaveOccurred()) + Expect(score1).To(BeNumerically(">", score2)) + + optionsDM := &redis.FTAggregateOptions{ + AddScores: true, + Scorer: "DISMAX", + SortBy: []redis.FTAggregateSortBy{{ + FieldName: "@__score", + Desc: true, + }}, + } + + resDM, err := client.FTAggregateWithArgs(ctx, "idx1", "laptop", optionsDM).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resDM).ToNot(BeNil()) + Expect(len(resDM.Rows)).To(BeEquivalentTo(2)) + score1DM, err := strconv.ParseFloat(fmt.Sprintf("%s", resDM.Rows[0].Fields["__score"]), 64) + Expect(err).NotTo(HaveOccurred()) + score2DM, err := strconv.ParseFloat(fmt.Sprintf("%s", resDM.Rows[1].Fields["__score"]), 64) + Expect(err).NotTo(HaveOccurred()) + Expect(score1DM).To(BeNumerically(">", score2DM)) + + Expect(score1DM).To(BeEquivalentTo(float64(4))) + Expect(score2DM).To(BeEquivalentTo(float64(1))) + Expect(score1).NotTo(BeEquivalentTo(score1DM)) + Expect(score2).NotTo(BeEquivalentTo(score2DM)) + }) + + It("should FTAggregate apply and groupby", Label("search", "ftaggregate"), func() { + text1 := &redis.FieldSchema{FieldName: "PrimaryKey", FieldType: redis.SearchFieldTypeText, Sortable: true} + num1 := &redis.FieldSchema{FieldName: "CreatedDateTimeUTC", FieldType: redis.SearchFieldTypeNumeric, Sortable: true} + val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{}, text1, num1).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(val).To(BeEquivalentTo("OK")) + WaitForIndexing(client, "idx1") + + // 6 feb + client.HSet(ctx, "doc1", "PrimaryKey", "9::362330", "CreatedDateTimeUTC", "1738823999") + + // 12 feb + client.HSet(ctx, "doc2", "PrimaryKey", "9::362329", "CreatedDateTimeUTC", "1739342399") + client.HSet(ctx, "doc3", "PrimaryKey", "9::362329", "CreatedDateTimeUTC", "1739353199") + + reducer := redis.FTAggregateReducer{Reducer: redis.SearchCount, As: "perDay"} + + options := &redis.FTAggregateOptions{ + Apply: []redis.FTAggregateApply{{Field: "floor(@CreatedDateTimeUTC /(60*60*24))", As: "TimestampAsDay"}}, + GroupBy: []redis.FTAggregateGroupBy{{ + Fields: []interface{}{"@TimestampAsDay"}, + Reduce: []redis.FTAggregateReducer{reducer}, + }}, + SortBy: []redis.FTAggregateSortBy{{ + FieldName: "@perDay", + Desc: true, + }}, + } + + res, err := client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).ToNot(BeNil()) + Expect(len(res.Rows)).To(BeEquivalentTo(2)) + Expect(res.Rows[0].Fields["perDay"]).To(BeEquivalentTo("2")) + Expect(res.Rows[1].Fields["perDay"]).To(BeEquivalentTo("1")) }) It("should FTAggregate apply", Label("search", "ftaggregate"), func() { @@ -634,7 +757,6 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { Expect(res.Rows[0].Fields["age"]).To(BeEquivalentTo("19")) Expect(res.Rows[1].Fields["age"]).To(BeEquivalentTo("25")) } - }) It("should FTSearch SkipInitialScan", Label("search", "ftsearch"), func() { @@ -1097,6 +1219,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, diff --git a/universal.go b/universal.go index f4d2d759..47fda276 100644 --- a/universal.go +++ b/universal.go @@ -115,6 +115,7 @@ func (o *UniversalOptions) Cluster() *ClusterOptions { DisableIndentity: o.DisableIndentity, IdentitySuffix: o.IdentitySuffix, + UnstableResp3: o.UnstableResp3, } } diff --git a/universal_test.go b/universal_test.go index 747c68ac..9328b477 100644 --- a/universal_test.go +++ b/universal_test.go @@ -38,4 +38,26 @@ var _ = Describe("UniversalClient", func() { }) Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred()) }) + + It("connect to clusters with UniversalClient and UnstableResp3", Label("NonRedisEnterprise"), func() { + client = redis.NewUniversalClient(&redis.UniversalOptions{ + Addrs: cluster.addrs(), + Protocol: 3, + UnstableResp3: true, + }) + Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred()) + a := func() { client.FTInfo(ctx, "all").Result() } + Expect(a).ToNot(Panic()) + }) + + It("connect to clusters with ClusterClient and UnstableResp3", Label("NonRedisEnterprise"), func() { + client = redis.NewClusterClient(&redis.ClusterOptions{ + Addrs: cluster.addrs(), + Protocol: 3, + UnstableResp3: true, + }) + Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred()) + a := func() { client.FTInfo(ctx, "all").Result() } + Expect(a).ToNot(Panic()) + }) }) diff --git a/version.go b/version.go index 2b9926ea..a447a546 100644 --- a/version.go +++ b/version.go @@ -2,5 +2,5 @@ package redis // Version is the current release version. func Version() string { - return "9.7.0" + return "9.7.1" }