Skip to content

Commit a0c22f8

Browse files
DOC-6084 enabled go-redis index/query notebook
1 parent d1dc8f8 commit a0c22f8

File tree

1 file changed

+334
-0
lines changed

1 file changed

+334
-0
lines changed
Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
// EXAMPLE: go_home_json
2+
// BINDER_ID go-go_home_json
3+
// HIDE_START
4+
package example_commands_test
5+
6+
// HIDE_END
7+
// STEP_START import
8+
import (
9+
"context"
10+
"fmt"
11+
"sort"
12+
13+
"github.com/redis/go-redis/v9"
14+
)
15+
16+
// STEP_END
17+
18+
func ExampleClient_search_json() {
19+
// STEP_START connect
20+
ctx := context.Background()
21+
22+
rdb := redis.NewClient(&redis.Options{
23+
Addr: "localhost:6379",
24+
Password: "", // no password docs
25+
DB: 0, // use default DB
26+
Protocol: 2,
27+
})
28+
// STEP_END
29+
// REMOVE_START
30+
// make sure we are working with fresh database
31+
rdb.FlushDB(ctx)
32+
rdb.Del(ctx, "user:1", "user:2", "user:3")
33+
rdb.FTDropIndex(ctx, "idx:users")
34+
// REMOVE_END
35+
36+
// STEP_START create_data
37+
user1 := map[string]interface{}{
38+
"name": "Paul John",
39+
"email": "[email protected]",
40+
"age": 42,
41+
"city": "London",
42+
}
43+
44+
user2 := map[string]interface{}{
45+
"name": "Eden Zamir",
46+
"email": "[email protected]",
47+
"age": 29,
48+
"city": "Tel Aviv",
49+
}
50+
51+
user3 := map[string]interface{}{
52+
"name": "Paul Zamir",
53+
"email": "[email protected]",
54+
"age": 35,
55+
"city": "Tel Aviv",
56+
}
57+
// STEP_END
58+
59+
// STEP_START make_index
60+
_, err := rdb.FTCreate(
61+
ctx,
62+
"idx:users",
63+
// Options:
64+
&redis.FTCreateOptions{
65+
OnJSON: true,
66+
Prefix: []interface{}{"user:"},
67+
},
68+
// Index schema fields:
69+
&redis.FieldSchema{
70+
FieldName: "$.name",
71+
As: "name",
72+
FieldType: redis.SearchFieldTypeText,
73+
},
74+
&redis.FieldSchema{
75+
FieldName: "$.city",
76+
As: "city",
77+
FieldType: redis.SearchFieldTypeTag,
78+
},
79+
&redis.FieldSchema{
80+
FieldName: "$.age",
81+
As: "age",
82+
FieldType: redis.SearchFieldTypeNumeric,
83+
},
84+
).Result()
85+
86+
if err != nil {
87+
panic(err)
88+
}
89+
// STEP_END
90+
91+
// STEP_START add_data
92+
_, err = rdb.JSONSet(ctx, "user:1", "$", user1).Result()
93+
94+
if err != nil {
95+
panic(err)
96+
}
97+
98+
_, err = rdb.JSONSet(ctx, "user:2", "$", user2).Result()
99+
100+
if err != nil {
101+
panic(err)
102+
}
103+
104+
_, err = rdb.JSONSet(ctx, "user:3", "$", user3).Result()
105+
106+
if err != nil {
107+
panic(err)
108+
}
109+
// STEP_END
110+
111+
// STEP_START query1
112+
findPaulResult, err := rdb.FTSearch(
113+
ctx,
114+
"idx:users",
115+
"Paul @age:[30 40]",
116+
).Result()
117+
118+
if err != nil {
119+
panic(err)
120+
}
121+
122+
fmt.Println(findPaulResult)
123+
// >>> {1 [{user:3 <nil> <nil> <nil> map[$:{"age":35,"city":"Tel Aviv"...
124+
// STEP_END
125+
126+
// STEP_START query2
127+
citiesResult, err := rdb.FTSearchWithArgs(
128+
ctx,
129+
"idx:users",
130+
"Paul",
131+
&redis.FTSearchOptions{
132+
Return: []redis.FTSearchReturn{
133+
{
134+
FieldName: "$.city",
135+
As: "city",
136+
},
137+
},
138+
},
139+
).Result()
140+
141+
if err != nil {
142+
panic(err)
143+
}
144+
145+
sort.Slice(citiesResult.Docs, func(i, j int) bool {
146+
return citiesResult.Docs[i].Fields["city"] < citiesResult.Docs[j].Fields["city"]
147+
})
148+
149+
for _, result := range citiesResult.Docs {
150+
fmt.Println(result.Fields["city"])
151+
}
152+
// >>> London
153+
// >>> Tel Aviv
154+
// STEP_END
155+
156+
// STEP_START query2count_only
157+
citiesResult2, err := rdb.FTSearchWithArgs(
158+
ctx,
159+
"idx:users",
160+
"Paul",
161+
&redis.FTSearchOptions{
162+
Return: []redis.FTSearchReturn{
163+
{
164+
FieldName: "$.city",
165+
As: "city",
166+
},
167+
},
168+
CountOnly: true,
169+
},
170+
).Result()
171+
172+
if err != nil {
173+
panic(err)
174+
}
175+
176+
// The `Total` field has the correct number of docs found
177+
// by the query but the `Docs` slice is empty.
178+
fmt.Println(len(citiesResult2.Docs)) // >>> 0
179+
fmt.Println(citiesResult2.Total) // >>> 2
180+
// STEP_END
181+
182+
// STEP_START query3
183+
aggOptions := redis.FTAggregateOptions{
184+
GroupBy: []redis.FTAggregateGroupBy{
185+
{
186+
Fields: []interface{}{"@city"},
187+
Reduce: []redis.FTAggregateReducer{
188+
{
189+
Reducer: redis.SearchCount,
190+
As: "count",
191+
},
192+
},
193+
},
194+
},
195+
}
196+
197+
aggResult, err := rdb.FTAggregateWithArgs(
198+
ctx,
199+
"idx:users",
200+
"*",
201+
&aggOptions,
202+
).Result()
203+
204+
if err != nil {
205+
panic(err)
206+
}
207+
208+
sort.Slice(aggResult.Rows, func(i, j int) bool {
209+
return aggResult.Rows[i].Fields["city"].(string) <
210+
aggResult.Rows[j].Fields["city"].(string)
211+
})
212+
213+
for _, row := range aggResult.Rows {
214+
fmt.Printf("%v - %v\n",
215+
row.Fields["city"], row.Fields["count"],
216+
)
217+
}
218+
// >>> City: London - 1
219+
// >>> City: Tel Aviv - 2
220+
// STEP_END
221+
222+
// Output:
223+
// {1 [{user:3 <nil> <nil> <nil> map[$:{"age":35,"city":"Tel Aviv","email":"[email protected]","name":"Paul Zamir"}] <nil>}]}
224+
// London
225+
// Tel Aviv
226+
// 0
227+
// 2
228+
// London - 1
229+
// Tel Aviv - 2
230+
}
231+
232+
func ExampleClient_search_hash() {
233+
ctx := context.Background()
234+
235+
rdb := redis.NewClient(&redis.Options{
236+
Addr: "localhost:6379",
237+
Password: "", // no password docs
238+
DB: 0, // use default DB
239+
Protocol: 2,
240+
})
241+
242+
// REMOVE_START
243+
rdb.Del(ctx, "huser:1", "huser:2", "huser:3")
244+
rdb.FTDropIndex(ctx, "hash-idx:users")
245+
// REMOVE_END
246+
247+
// STEP_START make_hash_index
248+
_, err := rdb.FTCreate(
249+
ctx,
250+
"hash-idx:users",
251+
// Options:
252+
&redis.FTCreateOptions{
253+
OnHash: true,
254+
Prefix: []interface{}{"huser:"},
255+
},
256+
// Index schema fields:
257+
&redis.FieldSchema{
258+
FieldName: "name",
259+
FieldType: redis.SearchFieldTypeText,
260+
},
261+
&redis.FieldSchema{
262+
FieldName: "city",
263+
FieldType: redis.SearchFieldTypeTag,
264+
},
265+
&redis.FieldSchema{
266+
FieldName: "age",
267+
FieldType: redis.SearchFieldTypeNumeric,
268+
},
269+
).Result()
270+
271+
if err != nil {
272+
panic(err)
273+
}
274+
// STEP_END
275+
276+
user1 := map[string]interface{}{
277+
"name": "Paul John",
278+
"email": "[email protected]",
279+
"age": 42,
280+
"city": "London",
281+
}
282+
283+
user2 := map[string]interface{}{
284+
"name": "Eden Zamir",
285+
"email": "[email protected]",
286+
"age": 29,
287+
"city": "Tel Aviv",
288+
}
289+
290+
user3 := map[string]interface{}{
291+
"name": "Paul Zamir",
292+
"email": "[email protected]",
293+
"age": 35,
294+
"city": "Tel Aviv",
295+
}
296+
297+
// STEP_START add_hash_data
298+
_, err = rdb.HSet(ctx, "huser:1", user1).Result()
299+
300+
if err != nil {
301+
panic(err)
302+
}
303+
304+
_, err = rdb.HSet(ctx, "huser:2", user2).Result()
305+
306+
if err != nil {
307+
panic(err)
308+
}
309+
310+
_, err = rdb.HSet(ctx, "huser:3", user3).Result()
311+
312+
if err != nil {
313+
panic(err)
314+
}
315+
// STEP_END
316+
317+
// STEP_START query1_hash
318+
findPaulHashResult, err := rdb.FTSearch(
319+
ctx,
320+
"hash-idx:users",
321+
"Paul @age:[30 40]",
322+
).Result()
323+
324+
if err != nil {
325+
panic(err)
326+
}
327+
328+
fmt.Println(findPaulHashResult)
329+
// >>> {1 [{huser:3 <nil> <nil> <nil> map[age:35 city:Tel Aviv...
330+
// STEP_END
331+
332+
// Output:
333+
// {1 [{huser:3 <nil> <nil> <nil> map[age:35 city:Tel Aviv email:[email protected] name:Paul Zamir] <nil>}]}
334+
}

0 commit comments

Comments
 (0)