From b54b71a7ef1ed8ea1c45eeada846e2292ff7c71d Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Sun, 15 Dec 2024 17:56:40 +0200 Subject: [PATCH 1/5] Enable dialect 2 on deafult --- search_commands.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/search_commands.go b/search_commands.go index ede084e4e1..a4278e4689 100644 --- a/search_commands.go +++ b/search_commands.go @@ -576,6 +576,8 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery } if options.DialectVersion > 0 { queryArgs = append(queryArgs, "DIALECT", options.DialectVersion) + } else { + queryArgs = append(queryArgs, "DIALECT", 2) } } return queryArgs @@ -767,6 +769,8 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st } if options.DialectVersion > 0 { args = append(args, "DIALECT", options.DialectVersion) + } else { + args = append(args, "DIALECT", 2) } } @@ -1128,6 +1132,8 @@ func (c cmdable) FTExplainWithArgs(ctx context.Context, index string, query stri args := []interface{}{"FT.EXPLAIN", index, query} if options.Dialect != "" { args = append(args, "DIALECT", options.Dialect) + } else { + args = append(args, "DIALECT", 2) } cmd := NewStringCmd(ctx, args...) _ = c(ctx, cmd) @@ -1425,6 +1431,8 @@ func (c cmdable) FTSpellCheckWithArgs(ctx context.Context, index string, query s } if options.Dialect > 0 { args = append(args, "DIALECT", options.Dialect) + } else { + args = append(args, "DIALECT", 2) } } cmd := newFTSpellCheckCmd(ctx, args...) @@ -1789,6 +1797,8 @@ func FTSearchQuery(query string, options *FTSearchOptions) SearchQuery { } if options.DialectVersion > 0 { queryArgs = append(queryArgs, "DIALECT", options.DialectVersion) + } else { + queryArgs = append(queryArgs, "DIALECT", 2) } } return queryArgs @@ -1903,6 +1913,8 @@ func (c cmdable) FTSearchWithArgs(ctx context.Context, index string, query strin } if options.DialectVersion > 0 { args = append(args, "DIALECT", options.DialectVersion) + } else { + args = append(args, "DIALECT", 2) } } cmd := newFTSearchCmd(ctx, options, args...) From 30b9d4d8b4a61fa99b39af46f91dbe33d793689e Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Sun, 15 Dec 2024 18:01:17 +0200 Subject: [PATCH 2/5] add vector test for default dialect --- search_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/search_test.go b/search_test.go index e267c8ae86..bf2439f9dd 100644 --- a/search_test.go +++ b/search_test.go @@ -911,6 +911,30 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { Expect(res.Docs[0].Fields["__v_score"]).To(BeEquivalentTo("0")) }) + It("should FTCreate VECTOR with default dialect", Label("search", "ftcreate"), func() { + hnswOptions := &redis.FTHNSWOptions{Type: "FLOAT32", Dim: 2, DistanceMetric: "L2"} + val, err := client.FTCreate(ctx, "idx1", + &redis.FTCreateOptions{}, + &redis.FieldSchema{FieldName: "v", FieldType: redis.SearchFieldTypeVector, VectorArgs: &redis.FTVectorArgs{HNSWOptions: hnswOptions}}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(val).To(BeEquivalentTo("OK")) + WaitForIndexing(client, "idx1") + + client.HSet(ctx, "a", "v", "aaaaaaaa") + client.HSet(ctx, "b", "v", "aaaabaaa") + client.HSet(ctx, "c", "v", "aaaaabaa") + + searchOptions := &redis.FTSearchOptions{ + Return: []redis.FTSearchReturn{{FieldName: "__v_score"}}, + SortBy: []redis.FTSearchSortBy{{FieldName: "__v_score", Asc: true}}, + Params: map[string]interface{}{"vec": "aaaaaaaa"}, + } + res, err := client.FTSearchWithArgs(ctx, "idx1", "*=>[KNN 2 @v $vec]", searchOptions).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Docs[0].ID).To(BeEquivalentTo("a")) + Expect(res.Docs[0].Fields["__v_score"]).To(BeEquivalentTo("0")) + }) + It("should FTCreate and FTSearch text params", Label("search", "ftcreate", "ftsearch"), func() { val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{}, &redis.FieldSchema{FieldName: "name", FieldType: redis.SearchFieldTypeText}).Result() Expect(err).NotTo(HaveOccurred()) From 4fae9a191b924a39bd937b2debe2db4f5b5fff5c Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Thu, 16 Jan 2025 11:21:22 +0200 Subject: [PATCH 3/5] Add dialect 1 test --- search_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/search_test.go b/search_test.go index bf2439f9dd..e03af081f8 100644 --- a/search_test.go +++ b/search_test.go @@ -911,6 +911,31 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { Expect(res.Docs[0].Fields["__v_score"]).To(BeEquivalentTo("0")) }) + It("should FTCreate VECTOR with dialect 1 ", Label("search", "ftcreate"), func() { + hnswOptions := &redis.FTHNSWOptions{Type: "FLOAT32", Dim: 2, DistanceMetric: "L2"} + val, err := client.FTCreate(ctx, "idx1", + &redis.FTCreateOptions{}, + &redis.FieldSchema{FieldName: "v", FieldType: redis.SearchFieldTypeVector, VectorArgs: &redis.FTVectorArgs{HNSWOptions: hnswOptions}}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(val).To(BeEquivalentTo("OK")) + WaitForIndexing(client, "idx1") + + client.HSet(ctx, "a", "v", "aaaaaaaa") + client.HSet(ctx, "b", "v", "aaaabaaa") + client.HSet(ctx, "c", "v", "aaaaabaa") + + searchOptions := &redis.FTSearchOptions{ + Return: []redis.FTSearchReturn{{FieldName: "__v_score"}}, + SortBy: []redis.FTSearchSortBy{{FieldName: "__v_score", Asc: true}}, + DialectVersion: 1, + Params: map[string]interface{}{"vec": "aaaaaaaa"}, + } + res, err := client.FTSearchWithArgs(ctx, "idx1", "*=>[KNN 2 @v $vec]", searchOptions).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Docs[0].ID).To(BeEquivalentTo("a")) + Expect(res.Docs[0].Fields["__v_score"]).To(BeEquivalentTo("0")) + }) + It("should FTCreate VECTOR with default dialect", Label("search", "ftcreate"), func() { hnswOptions := &redis.FTHNSWOptions{Type: "FLOAT32", Dim: 2, DistanceMetric: "L2"} val, err := client.FTCreate(ctx, "idx1", From 85b83010687423b4553591a2c54932c3f317597e Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Wed, 19 Feb 2025 18:21:53 +0200 Subject: [PATCH 4/5] Add dialect 1 test & fix ft.search --- search_commands.go | 5 +++-- search_test.go | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/search_commands.go b/search_commands.go index a4278e4689..216f5bec8a 100644 --- a/search_commands.go +++ b/search_commands.go @@ -1671,11 +1671,12 @@ 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 } @@ -1899,7 +1900,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 e03af081f8..ff48b57ea0 100644 --- a/search_test.go +++ b/search_test.go @@ -127,7 +127,7 @@ 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))) }) @@ -925,15 +925,15 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { client.HSet(ctx, "c", "v", "aaaaabaa") searchOptions := &redis.FTSearchOptions{ - Return: []redis.FTSearchReturn{{FieldName: "__v_score"}}, - SortBy: []redis.FTSearchSortBy{{FieldName: "__v_score", Asc: true}}, + Return: []redis.FTSearchReturn{{FieldName: "v"}}, + SortBy: []redis.FTSearchSortBy{{FieldName: "v", Asc: true}}, + Limit: 10, DialectVersion: 1, - Params: map[string]interface{}{"vec": "aaaaaaaa"}, } - res, err := client.FTSearchWithArgs(ctx, "idx1", "*=>[KNN 2 @v $vec]", searchOptions).Result() + res, err := client.FTSearchWithArgs(ctx, "idx1", "*", searchOptions).Result() Expect(err).NotTo(HaveOccurred()) Expect(res.Docs[0].ID).To(BeEquivalentTo("a")) - Expect(res.Docs[0].Fields["__v_score"]).To(BeEquivalentTo("0")) + Expect(res.Docs[0].Fields["v"]).To(BeEquivalentTo("aaaaaaaa")) }) It("should FTCreate VECTOR with default dialect", Label("search", "ftcreate"), func() { From 63cf646b0b1ce9f20ec1f1950c56ae69313ebcc9 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Thu, 13 Mar 2025 10:47:27 +0200 Subject: [PATCH 5/5] Add default dialect to Readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index e8342734da..cfe97d3772 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,10 @@ res1, err := client.FTSearchWithArgs(ctx, "txt", "foo bar", &redis.FTSearchOptio val1 := client.FTSearchWithArgs(ctx, "txt", "foo bar", &redis.FTSearchOptions{}).RawVal() ``` +#### Redis-Search Default Dialect + +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. + ## Contributing Please see [out contributing guidelines](CONTRIBUTING.md) to help us improve this library!