Skip to content

Commit 28d719d

Browse files
docs: DOC-5074 added vector set doc examples (#3031)
1 parent c2dc73c commit 28d719d

File tree

1 file changed

+281
-0
lines changed

1 file changed

+281
-0
lines changed

doctests/dt-vec-set.js

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
// EXAMPLE: vecset_tutorial
2+
// REMOVE_START
3+
/**
4+
* Code samples for Vector set doc pages:
5+
* https://redis.io/docs/latest/develop/data-types/vector-sets/
6+
*/
7+
8+
import assert from 'assert';
9+
// REMOVE_END
10+
// HIDE_START
11+
import { createClient } from 'redis';
12+
13+
const client = createClient({
14+
RESP: 3 // Required for vector set commands
15+
});
16+
17+
await client.connect();
18+
// HIDE_END
19+
20+
// REMOVE_START
21+
await client.del([
22+
"points", "quantSetQ8", "quantSetNoQ",
23+
"quantSetBin", "setNotReduced", "setReduced"
24+
]);
25+
// REMOVE_END
26+
27+
// STEP_START vadd
28+
const res1 = await client.vAdd("points", [1.0, 1.0], "pt:A");
29+
console.log(res1); // >>> true
30+
31+
const res2 = await client.vAdd("points", [-1.0, -1.0], "pt:B");
32+
console.log(res2); // >>> true
33+
34+
const res3 = await client.vAdd("points", [-1.0, 1.0], "pt:C");
35+
console.log(res3); // >>> true
36+
37+
const res4 = await client.vAdd("points", [1.0, -1.0], "pt:D");
38+
console.log(res4); // >>> true
39+
40+
const res5 = await client.vAdd("points", [1.0, 0], "pt:E");
41+
console.log(res5); // >>> true
42+
43+
const res6 = await client.type("points");
44+
console.log(res6); // >>> vectorset
45+
// STEP_END
46+
// REMOVE_START
47+
assert.equal(res1, true);
48+
assert.equal(res2, true);
49+
assert.equal(res3, true);
50+
assert.equal(res4, true);
51+
assert.equal(res5, true);
52+
assert.equal(res6, "vectorset");
53+
// REMOVE_END
54+
55+
// STEP_START vcardvdim
56+
const res7 = await client.vCard("points");
57+
console.log(res7); // >>> 5
58+
59+
const res8 = await client.vDim("points");
60+
console.log(res8); // >>> 2
61+
// STEP_END
62+
// REMOVE_START
63+
assert.equal(res7, 5);
64+
assert.equal(res8, 2);
65+
// REMOVE_END
66+
67+
// STEP_START vemb
68+
const res9 = await client.vEmb("points", "pt:A");
69+
console.log(res9); // >>> [0.9999999403953552, 0.9999999403953552]
70+
71+
const res10 = await client.vEmb("points", "pt:B");
72+
console.log(res10); // >>> [-0.9999999403953552, -0.9999999403953552]
73+
74+
const res11 = await client.vEmb("points", "pt:C");
75+
console.log(res11); // >>> [-0.9999999403953552, 0.9999999403953552]
76+
77+
const res12 = await client.vEmb("points", "pt:D");
78+
console.log(res12); // >>> [0.9999999403953552, -0.9999999403953552]
79+
80+
const res13 = await client.vEmb("points", "pt:E");
81+
console.log(res13); // >>> [1, 0]
82+
// STEP_END
83+
// REMOVE_START
84+
assert(Math.abs(1 - res9[0]) < 0.001);
85+
assert(Math.abs(1 - res9[1]) < 0.001);
86+
assert(Math.abs(1 + res10[0]) < 0.001);
87+
assert(Math.abs(1 + res10[1]) < 0.001);
88+
assert(Math.abs(1 + res11[0]) < 0.001);
89+
assert(Math.abs(1 - res11[1]) < 0.001);
90+
assert(Math.abs(1 - res12[0]) < 0.001);
91+
assert(Math.abs(1 + res12[1]) < 0.001);
92+
assert.deepEqual(res13, [1, 0]);
93+
// REMOVE_END
94+
95+
// STEP_START attr
96+
const res14 = await client.vSetAttr("points", "pt:A", {
97+
name: "Point A",
98+
description: "First point added"
99+
});
100+
console.log(res14); // >>> true
101+
102+
const res15 = await client.vGetAttr("points", "pt:A");
103+
console.log(res15);
104+
// >>> {name: 'Point A', description: 'First point added'}
105+
106+
const res16 = await client.vSetAttr("points", "pt:A", "");
107+
console.log(res16); // >>> true
108+
109+
const res17 = await client.vGetAttr("points", "pt:A");
110+
console.log(res17); // >>> null
111+
// STEP_END
112+
// REMOVE_START
113+
assert.equal(res14, true);
114+
assert.deepEqual(res15, {name: "Point A", description: "First point added"});
115+
assert.equal(res16, true);
116+
assert.equal(res17, null);
117+
// REMOVE_END
118+
119+
// STEP_START vrem
120+
const res18 = await client.vAdd("points", [0, 0], "pt:F");
121+
console.log(res18); // >>> true
122+
123+
const res19 = await client.vCard("points");
124+
console.log(res19); // >>> 6
125+
126+
const res20 = await client.vRem("points", "pt:F");
127+
console.log(res20); // >>> true
128+
129+
const res21 = await client.vCard("points");
130+
console.log(res21); // >>> 5
131+
// STEP_END
132+
// REMOVE_START
133+
assert.equal(res18, true);
134+
assert.equal(res19, 6);
135+
assert.equal(res20, true);
136+
assert.equal(res21, 5);
137+
// REMOVE_END
138+
139+
// STEP_START vsim_basic
140+
const res22 = await client.vSim("points", [0.9, 0.1]);
141+
console.log(res22);
142+
// >>> ['pt:E', 'pt:A', 'pt:D', 'pt:C', 'pt:B']
143+
// STEP_END
144+
// REMOVE_START
145+
assert.deepEqual(res22, ["pt:E", "pt:A", "pt:D", "pt:C", "pt:B"]);
146+
// REMOVE_END
147+
148+
// STEP_START vsim_options
149+
const res23 = await client.vSimWithScores("points", "pt:A", { COUNT: 4 });
150+
console.log(res23);
151+
// >>> {pt:A: 1.0, pt:E: 0.8535534143447876, pt:D: 0.5, pt:C: 0.5}
152+
// STEP_END
153+
// REMOVE_START
154+
assert.equal(res23["pt:A"], 1.0);
155+
assert.equal(res23["pt:C"], 0.5);
156+
assert.equal(res23["pt:D"], 0.5);
157+
assert(Math.abs(res23["pt:E"] - 0.85) < 0.005);
158+
// REMOVE_END
159+
160+
// STEP_START vsim_filter
161+
const res24 = await client.vSetAttr("points", "pt:A", {
162+
size: "large",
163+
price: 18.99
164+
});
165+
console.log(res24); // >>> true
166+
167+
const res25 = await client.vSetAttr("points", "pt:B", {
168+
size: "large",
169+
price: 35.99
170+
});
171+
console.log(res25); // >>> true
172+
173+
const res26 = await client.vSetAttr("points", "pt:C", {
174+
size: "large",
175+
price: 25.99
176+
});
177+
console.log(res26); // >>> true
178+
179+
const res27 = await client.vSetAttr("points", "pt:D", {
180+
size: "small",
181+
price: 21.00
182+
});
183+
console.log(res27); // >>> true
184+
185+
const res28 = await client.vSetAttr("points", "pt:E", {
186+
size: "small",
187+
price: 17.75
188+
});
189+
console.log(res28); // >>> true
190+
191+
// Return elements in order of distance from point A whose
192+
// `size` attribute is `large`.
193+
const res29 = await client.vSim("points", "pt:A", {
194+
FILTER: '.size == "large"'
195+
});
196+
console.log(res29); // >>> ['pt:A', 'pt:C', 'pt:B']
197+
198+
// Return elements in order of distance from point A whose size is
199+
// `large` and whose price is greater than 20.00.
200+
const res30 = await client.vSim("points", "pt:A", {
201+
FILTER: '.size == "large" && .price > 20.00'
202+
});
203+
console.log(res30); // >>> ['pt:C', 'pt:B']
204+
// STEP_END
205+
// REMOVE_START
206+
assert.equal(res24, true);
207+
assert.equal(res25, true);
208+
assert.equal(res26, true);
209+
assert.equal(res27, true);
210+
assert.equal(res28, true);
211+
assert.deepEqual(res29, ['pt:A', 'pt:C', 'pt:B']);
212+
assert.deepEqual(res30, ['pt:C', 'pt:B']);
213+
// REMOVE_END
214+
215+
// STEP_START add_quant
216+
const res31 = await client.vAdd("quantSetQ8", [1.262185, 1.958231], "quantElement", {
217+
QUANT: 'Q8'
218+
});
219+
console.log(res31); // >>> true
220+
221+
const res32 = await client.vEmb("quantSetQ8", "quantElement");
222+
console.log(`Q8: ${res32}`);
223+
// >>> Q8: [1.2643694877624512, 1.958230972290039]
224+
225+
const res33 = await client.vAdd("quantSetNoQ", [1.262185, 1.958231], "quantElement", {
226+
QUANT: 'NOQUANT'
227+
});
228+
console.log(res33); // >>> true
229+
230+
const res34 = await client.vEmb("quantSetNoQ", "quantElement");
231+
console.log(`NOQUANT: ${res34}`);
232+
// >>> NOQUANT: [1.262184977531433, 1.958230972290039]
233+
234+
const res35 = await client.vAdd("quantSetBin", [1.262185, 1.958231], "quantElement", {
235+
QUANT: 'BIN'
236+
});
237+
console.log(res35); // >>> true
238+
239+
const res36 = await client.vEmb("quantSetBin", "quantElement");
240+
console.log(`BIN: ${res36}`);
241+
// >>> BIN: [1, 1]
242+
// STEP_END
243+
// REMOVE_START
244+
assert.equal(res31, true);
245+
assert(Math.abs(res32[0] - 1.2643694877624512) < 0.001);
246+
assert(Math.abs(res32[1] - 1.958230972290039) < 0.001);
247+
assert.equal(res33, true);
248+
assert(Math.abs(res34[0] - 1.262184977531433) < 0.001);
249+
assert(Math.abs(res34[1] - 1.958230972290039) < 0.001);
250+
assert.equal(res35, true);
251+
assert.deepEqual(res36, [1, 1]);
252+
// REMOVE_END
253+
254+
// STEP_START add_reduce
255+
// Create a list of 300 arbitrary values.
256+
const values = Array.from({length: 300}, (_, x) => x / 299);
257+
258+
const res37 = await client.vAdd("setNotReduced", values, "element");
259+
console.log(res37); // >>> true
260+
261+
const res38 = await client.vDim("setNotReduced");
262+
console.log(res38); // >>> 300
263+
264+
const res39 = await client.vAdd("setReduced", values, "element", {
265+
REDUCE: 100
266+
});
267+
console.log(res39); // >>> true
268+
269+
const res40 = await client.vDim("setReduced");
270+
console.log(res40); // >>> 100
271+
// STEP_END
272+
// REMOVE_START
273+
assert.equal(res37, true);
274+
assert.equal(res38, 300);
275+
assert.equal(res39, true);
276+
assert.equal(res40, 100);
277+
// REMOVE_END
278+
279+
// HIDE_START
280+
await client.quit();
281+
// HIDE_END

0 commit comments

Comments
 (0)