1
2
3 package example_commands_test
4
5 import (
6 "context"
7 "fmt"
8 "sort"
9
10 "github.com/redis/go-redis/v9"
11 )
12
13
14
15 func ExampleClient_vectorset() {
16 ctx := context.Background()
17
18 rdb := redis.NewClient(&redis.Options{
19 Addr: "localhost:6379",
20 Password: "",
21 DB: 0,
22 })
23
24 defer rdb.Close()
25
26 rdb.Del(ctx, "points", "quantSetQ8", "quantSetNoQ", "quantSetBin", "setNotReduced", "setReduced")
27
28
29
30 res1, err := rdb.VAdd(ctx, "points", "pt:A",
31 &redis.VectorValues{Val: []float64{1.0, 1.0}},
32 ).Result()
33
34 if err != nil {
35 panic(err)
36 }
37
38 fmt.Println(res1)
39
40 res2, err := rdb.VAdd(ctx, "points", "pt:B",
41 &redis.VectorValues{Val: []float64{-1.0, -1.0}},
42 ).Result()
43
44 if err != nil {
45 panic(err)
46 }
47
48 fmt.Println(res2)
49
50 res3, err := rdb.VAdd(ctx, "points", "pt:C",
51 &redis.VectorValues{Val: []float64{-1.0, 1.0}},
52 ).Result()
53
54 if err != nil {
55 panic(err)
56 }
57
58 fmt.Println(res3)
59
60 res4, err := rdb.VAdd(ctx, "points", "pt:D",
61 &redis.VectorValues{Val: []float64{1.0, -1.0}},
62 ).Result()
63
64 if err != nil {
65 panic(err)
66 }
67
68 fmt.Println(res4)
69
70 res5, err := rdb.VAdd(ctx, "points", "pt:E",
71 &redis.VectorValues{Val: []float64{1.0, 0.0}},
72 ).Result()
73
74 if err != nil {
75 panic(err)
76 }
77
78 fmt.Println(res5)
79
80 res6, err := rdb.Type(ctx, "points").Result()
81
82 if err != nil {
83 panic(err)
84 }
85
86 fmt.Println(res6)
87
88
89
90 res7, err := rdb.VCard(ctx, "points").Result()
91
92 if err != nil {
93 panic(err)
94 }
95
96 fmt.Println(res7)
97
98 res8, err := rdb.VDim(ctx, "points").Result()
99
100 if err != nil {
101 panic(err)
102 }
103
104 fmt.Println(res8)
105
106
107
108 res9, err := rdb.VEmb(ctx, "points", "pt:A", false).Result()
109
110 if err != nil {
111 panic(err)
112 }
113
114 fmt.Println(res9)
115
116 res10, err := rdb.VEmb(ctx, "points", "pt:B", false).Result()
117
118 if err != nil {
119 panic(err)
120 }
121
122 fmt.Println(res10)
123
124 res11, err := rdb.VEmb(ctx, "points", "pt:C", false).Result()
125
126 if err != nil {
127 panic(err)
128 }
129
130 fmt.Println(res11)
131
132 res12, err := rdb.VEmb(ctx, "points", "pt:D", false).Result()
133
134 if err != nil {
135 panic(err)
136 }
137
138 fmt.Println(res12)
139
140 res13, err := rdb.VEmb(ctx, "points", "pt:E", false).Result()
141
142 if err != nil {
143 panic(err)
144 }
145
146 fmt.Println(res13)
147
148
149
150 attrs := map[string]interface{}{
151 "name": "Point A",
152 "description": "First point added",
153 }
154
155 res14, err := rdb.VSetAttr(ctx, "points", "pt:A", attrs).Result()
156
157 if err != nil {
158 panic(err)
159 }
160
161 fmt.Println(res14)
162
163 res15, err := rdb.VGetAttr(ctx, "points", "pt:A").Result()
164
165 if err != nil {
166 panic(err)
167 }
168
169 fmt.Println(res15)
170
171
172 res16, err := rdb.VClearAttributes(ctx, "points", "pt:A").Result()
173
174 if err != nil {
175 panic(err)
176 }
177
178 fmt.Println(res16)
179
180
181 _, err = rdb.VGetAttr(ctx, "points", "pt:A").Result()
182
183 if err != nil {
184 fmt.Println(err)
185 }
186
187
188
189 res18, err := rdb.VAdd(ctx, "points", "pt:F",
190 &redis.VectorValues{Val: []float64{0.0, 0.0}},
191 ).Result()
192
193 if err != nil {
194 panic(err)
195 }
196
197 fmt.Println(res18)
198
199 res19, err := rdb.VCard(ctx, "points").Result()
200
201 if err != nil {
202 panic(err)
203 }
204
205 fmt.Println(res19)
206
207 res20, err := rdb.VRem(ctx, "points", "pt:F").Result()
208
209 if err != nil {
210 panic(err)
211 }
212
213 fmt.Println(res20)
214
215 res21, err := rdb.VCard(ctx, "points").Result()
216
217 if err != nil {
218 panic(err)
219 }
220
221 fmt.Println(res21)
222
223
224
225 res22, err := rdb.VSim(ctx, "points",
226 &redis.VectorValues{Val: []float64{0.9, 0.1}},
227 ).Result()
228
229 if err != nil {
230 panic(err)
231 }
232
233 fmt.Println(res22)
234
235
236
237 res23, err := rdb.VSimWithArgsWithScores(
238 ctx,
239 "points",
240 &redis.VectorRef{Name: "pt:A"},
241 &redis.VSimArgs{Count: 4},
242 ).Result()
243
244 if err != nil {
245 panic(err)
246 }
247
248 sort.Slice(res23, func(i, j int) bool {
249 return res23[i].Name < res23[j].Name
250 })
251
252 fmt.Println(res23)
253
254
255
256
257
258 res24, err := rdb.VSetAttr(ctx, "points", "pt:A",
259 map[string]interface{}{
260 "size": "large",
261 "price": 18.99,
262 },
263 ).Result()
264
265 if err != nil {
266 panic(err)
267 }
268
269 fmt.Println(res24)
270
271 res25, err := rdb.VSetAttr(ctx, "points", "pt:B",
272 map[string]interface{}{
273 "size": "large",
274 "price": 35.99,
275 },
276 ).Result()
277
278 if err != nil {
279 panic(err)
280 }
281
282 fmt.Println(res25)
283
284 res26, err := rdb.VSetAttr(ctx, "points", "pt:C",
285 map[string]interface{}{
286 "size": "large",
287 "price": 25.99,
288 },
289 ).Result()
290
291 if err != nil {
292 panic(err)
293 }
294
295 fmt.Println(res26)
296
297 res27, err := rdb.VSetAttr(ctx, "points", "pt:D",
298 map[string]interface{}{
299 "size": "small",
300 "price": 21.00,
301 },
302 ).Result()
303
304 if err != nil {
305 panic(err)
306 }
307
308 fmt.Println(res27)
309
310 res28, err := rdb.VSetAttr(ctx, "points", "pt:E",
311 map[string]interface{}{
312 "size": "small",
313 "price": 17.75,
314 },
315 ).Result()
316
317 if err != nil {
318 panic(err)
319 }
320
321 fmt.Println(res28)
322
323
324
325 res29, err := rdb.VSimWithArgs(ctx, "points",
326 &redis.VectorRef{Name: "pt:A"},
327 &redis.VSimArgs{Filter: `.size == "large"`},
328 ).Result()
329
330 if err != nil {
331 panic(err)
332 }
333
334 fmt.Println(res29)
335
336
337
338 res30, err := rdb.VSimWithArgs(ctx, "points",
339 &redis.VectorRef{Name: "pt:A"},
340 &redis.VSimArgs{Filter: `.size == "large" && .price > 20.00`},
341 ).Result()
342
343 if err != nil {
344 panic(err)
345 }
346
347 fmt.Println(res30)
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381 }
382
383 func ExampleClient_vectorset_quantization() {
384 ctx := context.Background()
385
386 rdb := redis.NewClient(&redis.Options{
387 Addr: "localhost:6379",
388 Password: "",
389 DB: 0,
390 })
391
392 defer rdb.Close()
393
394 rdb.Del(ctx, "quantSetQ8", "quantSetNoQ", "quantSetBin")
395
396
397
398
399 vecQ := &redis.VectorValues{Val: []float64{1.262185, 1.958231}}
400
401 res1, err := rdb.VAddWithArgs(ctx, "quantSetQ8", "quantElement", vecQ,
402 &redis.VAddArgs{
403 Q8: true,
404 },
405 ).Result()
406
407 if err != nil {
408 panic(err)
409 }
410
411 fmt.Println(res1)
412
413 embQ8, err := rdb.VEmb(ctx, "quantSetQ8", "quantElement", false).Result()
414
415 if err != nil {
416 panic(err)
417 }
418
419 fmt.Printf("Q8 embedding: %v\n", embQ8)
420
421
422
423 res2, err := rdb.VAddWithArgs(ctx, "quantSetNoQ", "quantElement", vecQ,
424 &redis.VAddArgs{
425 NoQuant: true,
426 },
427 ).Result()
428
429 if err != nil {
430 panic(err)
431 }
432
433 fmt.Println(res2)
434
435 embNoQ, err := rdb.VEmb(ctx, "quantSetNoQ", "quantElement", false).Result()
436
437 if err != nil {
438 panic(err)
439 }
440
441 fmt.Printf("NOQUANT embedding: %v\n", embNoQ)
442
443
444
445 res3, err := rdb.VAddWithArgs(ctx, "quantSetBin", "quantElement", vecQ,
446 &redis.VAddArgs{
447 Bin: true,
448 },
449 ).Result()
450
451 if err != nil {
452 panic(err)
453 }
454
455 fmt.Println(res3)
456
457 embBin, err := rdb.VEmb(ctx, "quantSetBin", "quantElement", false).Result()
458
459 if err != nil {
460 panic(err)
461 }
462
463 fmt.Printf("BIN embedding: %v\n", embBin)
464
465
466
467
468
469
470
471
472
473
474 }
475
476 func ExampleClient_vectorset_dimension_reduction() {
477 ctx := context.Background()
478
479 rdb := redis.NewClient(&redis.Options{
480 Addr: "localhost:6379",
481 Password: "",
482 DB: 0,
483 })
484
485 defer rdb.Close()
486
487 rdb.Del(ctx, "setNotReduced", "setReduced")
488
489
490
491
492 values := make([]float64, 300)
493
494 for i := 0; i < 300; i++ {
495 values[i] = float64(i) / 299
496 }
497
498 vecLarge := &redis.VectorValues{Val: values}
499
500
501 res1, err := rdb.VAdd(ctx, "setNotReduced", "element", vecLarge).Result()
502
503 if err != nil {
504 panic(err)
505 }
506
507 fmt.Println(res1)
508
509 dim1, err := rdb.VDim(ctx, "setNotReduced").Result()
510
511 if err != nil {
512 panic(err)
513 }
514
515 fmt.Printf("Dimension without reduction: %d\n", dim1)
516
517
518
519 res2, err := rdb.VAddWithArgs(ctx, "setReduced", "element", vecLarge,
520 &redis.VAddArgs{
521 Reduce: 100,
522 },
523 ).Result()
524
525 if err != nil {
526 panic(err)
527 }
528
529 fmt.Println(res2)
530
531 dim2, err := rdb.VDim(ctx, "setReduced").Result()
532
533 if err != nil {
534 panic(err)
535 }
536
537 fmt.Printf("Dimension after reduction: %d\n", dim2)
538
539
540
541
542
543
544
545
546 }
547
View as plain text