Skip to content

Commit 75929ef

Browse files
authored
Merge branch 'master' into ndyakov/fix-handle-error-on-initConn
2 parents b817963 + 1c9309f commit 75929ef

File tree

5 files changed

+101
-8
lines changed

5 files changed

+101
-8
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@
66
[![codecov](https://codecov.io/github/redis/go-redis/graph/badge.svg?token=tsrCZKuSSw)](https://codecov.io/github/redis/go-redis)
77
[![Chat](https://discordapp.com/api/guilds/752070105847955518/widget.png)](https://discord.gg/rWtp5Aj)
88

9-
> go-redis is brought to you by :star: [**uptrace/uptrace**](https://github.com/uptrace/uptrace).
10-
> Uptrace is an open-source APM tool that supports distributed tracing, metrics, and logs. You can
11-
> use it to monitor applications and set up automatic alerts to receive notifications via email,
12-
> Slack, Telegram, and others.
13-
>
14-
> See [OpenTelemetry](https://github.com/redis/go-redis/tree/master/example/otel) example which
15-
> demonstrates how you can use Uptrace to monitor go-redis.
9+
> go-redis is the official Redis client library for the Go programming language. It offers a straightforward interface for interacting with Redis servers.
1610
1711
## Supported versions
1812

@@ -217,6 +211,10 @@ res1, err := client.FTSearchWithArgs(ctx, "txt", "foo bar", &redis.FTSearchOptio
217211
val1 := client.FTSearchWithArgs(ctx, "txt", "foo bar", &redis.FTSearchOptions{}).RawVal()
218212
```
219213

214+
#### Redis-Search Default Dialect
215+
216+
In the Redis-Search module, **the default dialect is 2**. If needed, you can explicitly specify a different dialect using the appropriate configuration in your queries.
217+
220218
## Contributing
221219

222220
Please see [out contributing guidelines](CONTRIBUTING.md) to help us improve this library!
@@ -299,6 +297,14 @@ REDIS_PORT=9999 go test <your options>
299297

300298
## Contributors
301299

300+
> The go-redis project was originally initiated by :star: [**uptrace/uptrace**](https://github.com/uptrace/uptrace).
301+
> Uptrace is an open-source APM tool that supports distributed tracing, metrics, and logs. You can
302+
> use it to monitor applications and set up automatic alerts to receive notifications via email,
303+
> Slack, Telegram, and others.
304+
>
305+
> See [OpenTelemetry](https://github.com/redis/go-redis/tree/master/example/otel) example which
306+
> demonstrates how you can use Uptrace to monitor go-redis.
307+
302308
Thanks to all the people who already contributed!
303309

304310
<a href="https://github.com/redis/go-redis/graphs/contributors">

redis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
310310

311311
// for redis-server versions that do not support the HELLO command,
312312
// RESP2 will continue to be used.
313-
if err = conn.Hello(ctx, protocol, username, password, "").Err(); err == nil {
313+
if err = conn.Hello(ctx, protocol, username, password, c.opt.ClientName).Err(); err == nil {
314314
auth = true
315315
} else if !isRedisError(err) {
316316
// When the server responds with the RESP protocol and the result is not a normal

redis_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,32 @@ var _ = Describe("Client", func() {
186186
Expect(val).Should(ContainSubstring("name=hi"))
187187
})
188188

189+
It("should attempt to set client name in HELLO", func() {
190+
opt := redisOptions()
191+
opt.ClientName = "hi"
192+
db := redis.NewClient(opt)
193+
194+
defer func() {
195+
Expect(db.Close()).NotTo(HaveOccurred())
196+
}()
197+
198+
// Client name should be already set on any successfully initialized connection
199+
name, err := db.ClientGetName(ctx).Result()
200+
Expect(err).NotTo(HaveOccurred())
201+
Expect(name).Should(Equal("hi"))
202+
203+
// HELLO should be able to explicitly overwrite the client name
204+
conn := db.Conn()
205+
hello, err := conn.Hello(ctx, 3, "", "", "hi2").Result()
206+
Expect(err).NotTo(HaveOccurred())
207+
Expect(hello["proto"]).Should(Equal(int64(3)))
208+
name, err = conn.ClientGetName(ctx).Result()
209+
Expect(err).NotTo(HaveOccurred())
210+
Expect(name).Should(Equal("hi2"))
211+
err = conn.Close()
212+
Expect(err).NotTo(HaveOccurred())
213+
})
214+
189215
It("should client PROTO 2", func() {
190216
opt := redisOptions()
191217
opt.Protocol = 2

search_commands.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,8 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery
604604

605605
if options.DialectVersion > 0 {
606606
queryArgs = append(queryArgs, "DIALECT", options.DialectVersion)
607+
} else {
608+
queryArgs = append(queryArgs, "DIALECT", 2)
607609
}
608610
}
609611
return queryArgs
@@ -801,6 +803,8 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st
801803
}
802804
if options.DialectVersion > 0 {
803805
args = append(args, "DIALECT", options.DialectVersion)
806+
} else {
807+
args = append(args, "DIALECT", 2)
804808
}
805809
}
806810

@@ -1174,6 +1178,8 @@ func (c cmdable) FTExplainWithArgs(ctx context.Context, index string, query stri
11741178
args := []interface{}{"FT.EXPLAIN", index, query}
11751179
if options.Dialect != "" {
11761180
args = append(args, "DIALECT", options.Dialect)
1181+
} else {
1182+
args = append(args, "DIALECT", 2)
11771183
}
11781184
cmd := NewStringCmd(ctx, args...)
11791185
_ = c(ctx, cmd)
@@ -1471,6 +1477,8 @@ func (c cmdable) FTSpellCheckWithArgs(ctx context.Context, index string, query s
14711477
}
14721478
if options.Dialect > 0 {
14731479
args = append(args, "DIALECT", options.Dialect)
1480+
} else {
1481+
args = append(args, "DIALECT", 2)
14741482
}
14751483
}
14761484
cmd := newFTSpellCheckCmd(ctx, args...)
@@ -1840,6 +1848,8 @@ func FTSearchQuery(query string, options *FTSearchOptions) SearchQuery {
18401848
}
18411849
if options.DialectVersion > 0 {
18421850
queryArgs = append(queryArgs, "DIALECT", options.DialectVersion)
1851+
} else {
1852+
queryArgs = append(queryArgs, "DIALECT", 2)
18431853
}
18441854
}
18451855
return queryArgs
@@ -1955,6 +1965,8 @@ func (c cmdable) FTSearchWithArgs(ctx context.Context, index string, query strin
19551965
}
19561966
if options.DialectVersion > 0 {
19571967
args = append(args, "DIALECT", options.DialectVersion)
1968+
} else {
1969+
args = append(args, "DIALECT", 2)
19581970
}
19591971
}
19601972
cmd := newFTSearchCmd(ctx, options, args...)

search_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,55 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
11431143
Expect(res.Docs[0].Fields["__v_score"]).To(BeEquivalentTo("0"))
11441144
})
11451145

1146+
It("should FTCreate VECTOR with dialect 1 ", Label("search", "ftcreate"), func() {
1147+
hnswOptions := &redis.FTHNSWOptions{Type: "FLOAT32", Dim: 2, DistanceMetric: "L2"}
1148+
val, err := client.FTCreate(ctx, "idx1",
1149+
&redis.FTCreateOptions{},
1150+
&redis.FieldSchema{FieldName: "v", FieldType: redis.SearchFieldTypeVector, VectorArgs: &redis.FTVectorArgs{HNSWOptions: hnswOptions}}).Result()
1151+
Expect(err).NotTo(HaveOccurred())
1152+
Expect(val).To(BeEquivalentTo("OK"))
1153+
WaitForIndexing(client, "idx1")
1154+
1155+
client.HSet(ctx, "a", "v", "aaaaaaaa")
1156+
client.HSet(ctx, "b", "v", "aaaabaaa")
1157+
client.HSet(ctx, "c", "v", "aaaaabaa")
1158+
1159+
searchOptions := &redis.FTSearchOptions{
1160+
Return: []redis.FTSearchReturn{{FieldName: "v"}},
1161+
SortBy: []redis.FTSearchSortBy{{FieldName: "v", Asc: true}},
1162+
Limit: 10,
1163+
DialectVersion: 1,
1164+
}
1165+
res, err := client.FTSearchWithArgs(ctx, "idx1", "*", searchOptions).Result()
1166+
Expect(err).NotTo(HaveOccurred())
1167+
Expect(res.Docs[0].ID).To(BeEquivalentTo("a"))
1168+
Expect(res.Docs[0].Fields["v"]).To(BeEquivalentTo("aaaaaaaa"))
1169+
})
1170+
1171+
It("should FTCreate VECTOR with default dialect", Label("search", "ftcreate"), func() {
1172+
hnswOptions := &redis.FTHNSWOptions{Type: "FLOAT32", Dim: 2, DistanceMetric: "L2"}
1173+
val, err := client.FTCreate(ctx, "idx1",
1174+
&redis.FTCreateOptions{},
1175+
&redis.FieldSchema{FieldName: "v", FieldType: redis.SearchFieldTypeVector, VectorArgs: &redis.FTVectorArgs{HNSWOptions: hnswOptions}}).Result()
1176+
Expect(err).NotTo(HaveOccurred())
1177+
Expect(val).To(BeEquivalentTo("OK"))
1178+
WaitForIndexing(client, "idx1")
1179+
1180+
client.HSet(ctx, "a", "v", "aaaaaaaa")
1181+
client.HSet(ctx, "b", "v", "aaaabaaa")
1182+
client.HSet(ctx, "c", "v", "aaaaabaa")
1183+
1184+
searchOptions := &redis.FTSearchOptions{
1185+
Return: []redis.FTSearchReturn{{FieldName: "__v_score"}},
1186+
SortBy: []redis.FTSearchSortBy{{FieldName: "__v_score", Asc: true}},
1187+
Params: map[string]interface{}{"vec": "aaaaaaaa"},
1188+
}
1189+
res, err := client.FTSearchWithArgs(ctx, "idx1", "*=>[KNN 2 @v $vec]", searchOptions).Result()
1190+
Expect(err).NotTo(HaveOccurred())
1191+
Expect(res.Docs[0].ID).To(BeEquivalentTo("a"))
1192+
Expect(res.Docs[0].Fields["__v_score"]).To(BeEquivalentTo("0"))
1193+
})
1194+
11461195
It("should FTCreate and FTSearch text params", Label("search", "ftcreate", "ftsearch"), func() {
11471196
val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{}, &redis.FieldSchema{FieldName: "name", FieldType: redis.SearchFieldTypeText}).Result()
11481197
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)