Skip to content

Commit 555a41e

Browse files
authored
Enable dialect 2 on default (#3213)
* Enable dialect 2 on deafult * add vector test for default dialect * Add dialect 1 test * Add dialect 1 test & fix ft.search * Add default dialect to Readme
1 parent 310ce55 commit 555a41e

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ res1, err := client.FTSearchWithArgs(ctx, "txt", "foo bar", &redis.FTSearchOptio
209209
val1 := client.FTSearchWithArgs(ctx, "txt", "foo bar", &redis.FTSearchOptions{}).RawVal()
210210
```
211211

212+
#### Redis-Search Default Dialect
213+
214+
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.
215+
212216
## Contributing
213217

214218
Please see [out contributing guidelines](CONTRIBUTING.md) to help us improve this library!

search_commands.go

+12
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

+49
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)