Skip to content

Commit ec2a461

Browse files
authored
Merge branch 'redis:master' into count_keys_in_slot
2 parents 36d1cd6 + b26758a commit ec2a461

6 files changed

+109
-4
lines changed

Diff for: doctests/home_json_example_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,32 @@ func ExampleClient_search_json() {
152152
// >>> Tel Aviv
153153
// STEP_END
154154

155+
// STEP_START query2count_only
156+
citiesResult2, err := rdb.FTSearchWithArgs(
157+
ctx,
158+
"idx:users",
159+
"Paul",
160+
&redis.FTSearchOptions{
161+
Return: []redis.FTSearchReturn{
162+
{
163+
FieldName: "$.city",
164+
As: "city",
165+
},
166+
},
167+
CountOnly: true,
168+
},
169+
).Result()
170+
171+
if err != nil {
172+
panic(err)
173+
}
174+
175+
// The `Total` field has the correct number of docs found
176+
// by the query but the `Docs` slice is empty.
177+
fmt.Println(len(citiesResult2.Docs)) // >>> 0
178+
fmt.Println(citiesResult2.Total) // >>> 2
179+
// STEP_END
180+
155181
// STEP_START query3
156182
aggOptions := redis.FTAggregateOptions{
157183
GroupBy: []redis.FTAggregateGroupBy{
@@ -196,6 +222,8 @@ func ExampleClient_search_json() {
196222
// {1 [{user:3 <nil> <nil> <nil> map[$:{"age":35,"city":"Tel Aviv","email":"[email protected]","name":"Paul Zamir"}]}]}
197223
// London
198224
// Tel Aviv
225+
// 0
226+
// 2
199227
// London - 1
200228
// Tel Aviv - 2
201229
}

Diff for: hash_commands.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type HashCmdable interface {
1313
HGetDel(ctx context.Context, key string, fields ...string) *StringSliceCmd
1414
HGetEX(ctx context.Context, key string, fields ...string) *StringSliceCmd
1515
HGetEXWithArgs(ctx context.Context, key string, options *HGetEXOptions, fields ...string) *StringSliceCmd
16+
HIncrBy(ctx context.Context, key, field string, incr int64) *IntCmd
1617
HIncrByFloat(ctx context.Context, key, field string, incr float64) *FloatCmd
1718
HKeys(ctx context.Context, key string) *StringSliceCmd
1819
HLen(ctx context.Context, key string) *IntCmd

Diff for: search_commands.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,11 @@ type FTSearchOptions struct {
320320
SortByWithCount bool
321321
LimitOffset int
322322
Limit int
323-
Params map[string]interface{}
324-
DialectVersion int
323+
// CountOnly sets LIMIT 0 0 to get the count - number of documents in the result set without actually returning the result set.
324+
// When using this option, the Limit and LimitOffset options are ignored.
325+
CountOnly bool
326+
Params map[string]interface{}
327+
DialectVersion int
325328
}
326329

327330
type FTSynDumpResult struct {
@@ -1954,8 +1957,12 @@ func (c cmdable) FTSearchWithArgs(ctx context.Context, index string, query strin
19541957
args = append(args, "WITHCOUNT")
19551958
}
19561959
}
1957-
if options.LimitOffset >= 0 && options.Limit > 0 {
1958-
args = append(args, "LIMIT", options.LimitOffset, options.Limit)
1960+
if options.CountOnly {
1961+
args = append(args, "LIMIT", 0, 0)
1962+
} else {
1963+
if options.LimitOffset >= 0 && options.Limit > 0 || options.LimitOffset > 0 && options.Limit == 0 {
1964+
args = append(args, "LIMIT", options.LimitOffset, options.Limit)
1965+
}
19591966
}
19601967
if options.Params != nil {
19611968
args = append(args, "PARAMS", len(options.Params)*2)

Diff for: search_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,44 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
16831683
Expect(resUint8.Docs[0].ID).To(BeEquivalentTo("doc1"))
16841684
})
16851685

1686+
It("should test ft.search with CountOnly param", Label("search", "ftsearch"), func() {
1687+
val, err := client.FTCreate(ctx, "txtIndex", &redis.FTCreateOptions{},
1688+
&redis.FieldSchema{FieldName: "txt", FieldType: redis.SearchFieldTypeText},
1689+
).Result()
1690+
Expect(err).NotTo(HaveOccurred())
1691+
Expect(val).To(BeEquivalentTo("OK"))
1692+
WaitForIndexing(client, "txtIndex")
1693+
1694+
_, err = client.HSet(ctx, "doc1", "txt", "hello world").Result()
1695+
Expect(err).NotTo(HaveOccurred())
1696+
_, err = client.HSet(ctx, "doc2", "txt", "hello go").Result()
1697+
Expect(err).NotTo(HaveOccurred())
1698+
_, err = client.HSet(ctx, "doc3", "txt", "hello redis").Result()
1699+
Expect(err).NotTo(HaveOccurred())
1700+
1701+
optsCountOnly := &redis.FTSearchOptions{
1702+
CountOnly: true,
1703+
LimitOffset: 0,
1704+
Limit: 2, // even though we limit to 2, with count-only no docs are returned
1705+
DialectVersion: 2,
1706+
}
1707+
resCountOnly, err := client.FTSearchWithArgs(ctx, "txtIndex", "hello", optsCountOnly).Result()
1708+
Expect(err).NotTo(HaveOccurred())
1709+
Expect(resCountOnly.Total).To(BeEquivalentTo(3))
1710+
Expect(len(resCountOnly.Docs)).To(BeEquivalentTo(0))
1711+
1712+
optsLimit := &redis.FTSearchOptions{
1713+
CountOnly: false,
1714+
LimitOffset: 0,
1715+
Limit: 2, // we expect to get 2 documents even though total count is 3
1716+
DialectVersion: 2,
1717+
}
1718+
resLimit, err := client.FTSearchWithArgs(ctx, "txtIndex", "hello", optsLimit).Result()
1719+
Expect(err).NotTo(HaveOccurred())
1720+
Expect(resLimit.Total).To(BeEquivalentTo(3))
1721+
Expect(len(resLimit.Docs)).To(BeEquivalentTo(2))
1722+
})
1723+
16861724
})
16871725

16881726
func _assert_geosearch_result(result *redis.FTSearchResult, expectedDocIDs []string) {

Diff for: sentinel.go

+16
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,22 @@ func NewFailoverClusterClient(failoverOpt *FailoverOptions) *ClusterClient {
815815
}
816816

817817
opt := failoverOpt.clusterOptions()
818+
if failoverOpt.DB != 0 {
819+
onConnect := opt.OnConnect
820+
821+
opt.OnConnect = func(ctx context.Context, cn *Conn) error {
822+
if err := cn.Select(ctx, failoverOpt.DB).Err(); err != nil {
823+
return err
824+
}
825+
826+
if onConnect != nil {
827+
return onConnect(ctx, cn)
828+
}
829+
830+
return nil
831+
}
832+
}
833+
818834
opt.ClusterSlots = func(ctx context.Context) ([]ClusterSlot, error) {
819835
masterAddr, err := failover.MasterAddr(ctx)
820836
if err != nil {

Diff for: sentinel_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ var _ = Describe("NewFailoverClusterClient", func() {
200200
SentinelAddrs: sentinelAddrs,
201201

202202
RouteRandomly: true,
203+
DB: 1,
203204
})
204205
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
205206

@@ -289,6 +290,20 @@ var _ = Describe("NewFailoverClusterClient", func() {
289290
})
290291
})
291292

293+
It("should sentinel cluster client db", func() {
294+
err := client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
295+
return c.Ping(ctx).Err()
296+
})
297+
Expect(err).NotTo(HaveOccurred())
298+
299+
_ = client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
300+
clientInfo, err := c.ClientInfo(ctx).Result()
301+
Expect(err).NotTo(HaveOccurred())
302+
Expect(clientInfo.DB).To(Equal(1))
303+
return nil
304+
})
305+
})
306+
292307
It("should sentinel cluster PROTO 3", func() {
293308
_ = client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
294309
val, err := client.Do(ctx, "HELLO").Result()

0 commit comments

Comments
 (0)