From 639b45ebf52a54c82e2c7c2502cdcb75dac8238e Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Mon, 7 Apr 2025 18:22:28 +0300 Subject: [PATCH 1/3] Fix Limit argument and add CountOnly argument --- search_commands.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/search_commands.go b/search_commands.go index 85e125614..8cdf05704 100644 --- a/search_commands.go +++ b/search_commands.go @@ -320,6 +320,7 @@ type FTSearchOptions struct { SortByWithCount bool LimitOffset int Limit int + CountOnly bool Params map[string]interface{} DialectVersion int } @@ -1954,8 +1955,12 @@ func (c cmdable) FTSearchWithArgs(ctx context.Context, index string, query strin args = append(args, "WITHCOUNT") } } - if options.LimitOffset >= 0 && options.Limit > 0 { - args = append(args, "LIMIT", options.LimitOffset, options.Limit) + if options.CountOnly { + args = append(args, "LIMIT", 0, 0) + } else { + if options.LimitOffset >= 0 && options.Limit > 0 || options.LimitOffset > 0 && options.Limit == 0 { + args = append(args, "LIMIT", options.LimitOffset, options.Limit) + } } if options.Params != nil { args = append(args, "PARAMS", len(options.Params)*2) From 5af10b96e99fb166830e262fddcf82186b892cc0 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Mon, 14 Apr 2025 12:39:35 +0300 Subject: [PATCH 2/3] Add test and Documentation --- search_commands.go | 8 +++++--- search_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/search_commands.go b/search_commands.go index 8cdf05704..0a97be907 100644 --- a/search_commands.go +++ b/search_commands.go @@ -320,9 +320,11 @@ type FTSearchOptions struct { SortByWithCount bool LimitOffset int Limit int - CountOnly bool - Params map[string]interface{} - DialectVersion int + // You can use LIMIT 0 0 to count the number of documents in the result set without actually returning them. + // When using this option, the Limit and LimitOffset options are ignored. + CountOnly bool + Params map[string]interface{} + DialectVersion int } type FTSynDumpResult struct { diff --git a/search_test.go b/search_test.go index 4d8417d78..3c4457a45 100644 --- a/search_test.go +++ b/search_test.go @@ -1683,6 +1683,44 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { Expect(resUint8.Docs[0].ID).To(BeEquivalentTo("doc1")) }) + It("should test ft.search with CountOnly param", Label("search", "ftsearch"), func() { + val, err := client.FTCreate(ctx, "txtIndex", &redis.FTCreateOptions{}, + &redis.FieldSchema{FieldName: "txt", FieldType: redis.SearchFieldTypeText}, + ).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(val).To(BeEquivalentTo("OK")) + WaitForIndexing(client, "txtIndex") + + _, err = client.HSet(ctx, "doc1", "txt", "hello world").Result() + Expect(err).NotTo(HaveOccurred()) + _, err = client.HSet(ctx, "doc2", "txt", "hello go").Result() + Expect(err).NotTo(HaveOccurred()) + _, err = client.HSet(ctx, "doc3", "txt", "hello redis").Result() + Expect(err).NotTo(HaveOccurred()) + + optsCountOnly := &redis.FTSearchOptions{ + CountOnly: true, + LimitOffset: 0, + Limit: 2, // even though we limit to 2, with count-only no docs are returned + DialectVersion: 2, + } + resCountOnly, err := client.FTSearchWithArgs(ctx, "txtIndex", "hello", optsCountOnly).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resCountOnly.Total).To(BeEquivalentTo(3)) + Expect(len(resCountOnly.Docs)).To(BeEquivalentTo(0)) + + optsLimit := &redis.FTSearchOptions{ + CountOnly: false, + LimitOffset: 0, + Limit: 2, // we expect to get 2 documents even though total count is 3 + DialectVersion: 2, + } + resLimit, err := client.FTSearchWithArgs(ctx, "txtIndex", "hello", optsLimit).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resLimit.Total).To(BeEquivalentTo(3)) + Expect(len(resLimit.Docs)).To(BeEquivalentTo(2)) + }) + }) func _assert_geosearch_result(result *redis.FTSearchResult, expectedDocIDs []string) { From 974623f4d4918eb678e1db0d193a4b4b779e9c31 Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com> Date: Tue, 15 Apr 2025 13:57:47 +0300 Subject: [PATCH 3/3] Update search_commands.go --- search_commands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/search_commands.go b/search_commands.go index 0a97be907..409426209 100644 --- a/search_commands.go +++ b/search_commands.go @@ -320,7 +320,7 @@ type FTSearchOptions struct { SortByWithCount bool LimitOffset int Limit int - // You can use LIMIT 0 0 to count the number of documents in the result set without actually returning them. + // CountOnly sets LIMIT 0 0 to get the count - number of documents in the result set without actually returning the result set. // When using this option, the Limit and LimitOffset options are ignored. CountOnly bool Params map[string]interface{}