Skip to content

Commit 6e07177

Browse files
ofekshenawandyakov
andauthored
Fix FT.Search Limit argument and add CountOnly argument for limit 0 0 (#3338)
* Fix Limit argument and add CountOnly argument * Add test and Documentation * Update search_commands.go --------- Co-authored-by: Nedyalko Dyakov <[email protected]>
1 parent 4648432 commit 6e07177

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

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)

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) {

0 commit comments

Comments
 (0)