Skip to content

Commit 5af10b9

Browse files
committed
Add test and Documentation
1 parent 639b45e commit 5af10b9

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

search_commands.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,11 @@ type FTSearchOptions struct {
320320
SortByWithCount bool
321321
LimitOffset int
322322
Limit int
323-
CountOnly bool
324-
Params map[string]interface{}
325-
DialectVersion int
323+
// You can use LIMIT 0 0 to count the number of documents in the result set without actually returning them.
324+
// When using this option, the Limit and LimitOffset options are ignored.
325+
CountOnly bool
326+
Params map[string]interface{}
327+
DialectVersion int
326328
}
327329

328330
type FTSynDumpResult struct {

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)