Skip to content

Commit bef6b10

Browse files
andy-stark-redisvladvildanovofekshenawa
authored
DOC-4240 added Top-K examples (#3119)
Co-authored-by: Vladyslav Vildanov <[email protected]> Co-authored-by: ofekshenawa <[email protected]>
1 parent e3d41f2 commit bef6b10

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

doctests/topk_tutorial_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// EXAMPLE: topk_tutorial
2+
// HIDE_START
3+
package example_commands_test
4+
5+
import (
6+
"context"
7+
"fmt"
8+
9+
"github.com/redis/go-redis/v9"
10+
)
11+
12+
// HIDE_END
13+
14+
func ExampleClient_topk() {
15+
ctx := context.Background()
16+
17+
rdb := redis.NewClient(&redis.Options{
18+
Addr: "localhost:6379",
19+
Password: "", // no password docs
20+
DB: 0, // use default DB
21+
})
22+
23+
// REMOVE_START
24+
rdb.Del(ctx, "bikes:keywords")
25+
// REMOVE_END
26+
27+
// STEP_START topk
28+
res1, err := rdb.TopKReserve(ctx, "bikes:keywords", 5).Result()
29+
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
fmt.Println(res1) // >>> OK
35+
36+
res2, err := rdb.TopKAdd(ctx, "bikes:keywords",
37+
"store",
38+
"seat",
39+
"handlebars",
40+
"handles",
41+
"pedals",
42+
"tires",
43+
"store",
44+
"seat",
45+
).Result()
46+
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
fmt.Println(res2) // >>> [ handlebars ]
52+
53+
res3, err := rdb.TopKList(ctx, "bikes:keywords").Result()
54+
55+
if err != nil {
56+
panic(err)
57+
}
58+
59+
fmt.Println(res3) // [store seat pedals tires handles]
60+
61+
res4, err := rdb.TopKQuery(ctx, "bikes:keywords", "store", "handlebars").Result()
62+
63+
if err != nil {
64+
panic(err)
65+
}
66+
67+
fmt.Println(res4) // [true false]
68+
// STEP_END
69+
70+
// Output:
71+
// OK
72+
// [ handlebars ]
73+
// [store seat pedals tires handles]
74+
// [true false]
75+
}

0 commit comments

Comments
 (0)