@@ -38,3 +38,165 @@ The following commands are available for vector sets:
38
38
- [ VREM] ({{< relref "/commands/vrem" >}}) - remove an element from a vector set.
39
39
- [ VSETATTR] ({{< relref "/commands/vsetattr" >}}) - set or replace attributes on a vector set element.
40
40
- [ VSIM] ({{< relref "/commands/vsim" >}}) - retrieve elements similar to a given vector or element with optional filtering.
41
+
42
+ ## Examples
43
+
44
+ The following examples give an overview of how to use vector sets. For clarity,
45
+ we will use a set of two-dimensional vectors that represent points in the
46
+ Cartesian coordinate plane. However, in real use cases, the vectors will typically
47
+ represent * text embeddings* and have hundreds of dimensions. See
48
+ [ Redis for AI] ({{< relref "/develop/ai" >}}) for more information about using text
49
+ embeddings.
50
+
51
+ The points we will use are A: (1.0, 1.0), B: (-1.0, -1.0), C: (-1.0, 1.0), D: (1.0. -1.0), and
52
+ E: (1.0, 0), shown in the diagram below.
53
+
54
+ {{<image filename =" images/vecsets/VecSetExamplePoints.drawio.svg " alt =" Example points on the coordinate plane. " width =" 400px " >}}
55
+
56
+ ### Basic operations
57
+
58
+ Start by adding the point vectors to a set called ` points ` using
59
+ [ ` VADD ` ] ({{< relref "/commands/vadd" >}}). This also creates the vector set object.
60
+ The [ ` TYPE ` ] ({{< relref "/commands/type" >}}) command returns a type of ` vectorset `
61
+ for this object.
62
+
63
+ ```
64
+ > VADD points VALUES 2 1.0 1.0 pt:A
65
+ (integer) 1
66
+ > VADD points VALUES 2 -1.0 -1.0 pt:B
67
+ (integer) 1
68
+ > VADD points VALUES 2 -1.0 1.0 pt:C
69
+ (integer) 1
70
+ > VADD points VALUES 2 1.0 -1.0 pt:D
71
+ (integer) 1
72
+ > VADD points VALUES 2 1.0 0 pt:E
73
+ (integer) 1
74
+ > TYPE points
75
+ vectorset
76
+ ```
77
+
78
+ Get the number of elements in the set (also known as the * cardinality* of the set)
79
+ using [ ` VCARD ` ] ({{< relref "/commands/vcard" >}}) and the number of dimensions of
80
+ the vectors using [ ` VDIM ` ] ({{< relref "/commands/vdim" >}}):
81
+
82
+ ```
83
+ > VCARD points
84
+ (integer) 5
85
+ > VDIM points
86
+ (integer) 2
87
+ ```
88
+
89
+ Get the coordinate values from the elements using [ ` VEMB ` ] ({{< relref "/commands/vemb" >}}).
90
+ Note that the values will not typically be the exact values you supplied when you added
91
+ the vector because
92
+ [ quantization] ({{< relref "/develop/data-types/vector-sets/performance#quantization-effects" >}})
93
+ is applied to improve performance.
94
+
95
+ ```
96
+ > VEMB points pt:A
97
+ 1) "0.9999999403953552"
98
+ 2) "0.9999999403953552"
99
+ 9> VEMB points pt:B
100
+ 1) "-0.9999999403953552"
101
+ 2) "-0.9999999403953552"
102
+ > VEMB points pt:C
103
+ 1) "-0.9999999403953552"
104
+ 2) "0.9999999403953552"
105
+ > VEMB points pt:D
106
+ 1) "0.9999999403953552"
107
+ 2) "-0.9999999403953552"
108
+ > VEMB points pt:E
109
+ 1) "1"
110
+ 2) "0"
111
+ ```
112
+
113
+ Set and retrieve an element's JSON attribute data using
114
+ [ ` VSETATTR ` ] ({{< relref "/commands/vsetattr" >}})
115
+ and [ ` VGETATTR ` ] ({{< relref "/commands/vgetattr" >}}). You can also pass an empty string
116
+ to ` VSETATTR ` to delete the attribute data:
117
+
118
+ ```
119
+ > VSETATTR points pt:A "{\"name\": \"Point A\", \"description\": \"First point added\"}"
120
+ (integer) 1
121
+ > VGETATTR points pt:A
122
+ "{\"name\": \"Point A\", \"description\": \"First point added\"}"
123
+ > VSETATTR points pt:A ""
124
+ (integer) 1
125
+ > VGETATTR points pt:A
126
+ (nil)
127
+ ```
128
+
129
+ Remove an unwanted element with [ ` VREM ` ] ({{< relref "/commands/vrem" >}})
130
+
131
+ ```
132
+ > VADD points VALUES 2 0 0 pt:F
133
+ (integer) 1
134
+ 127.0.0.1:6379> VCARD points
135
+ (integer) 6
136
+ 127.0.0.1:6379> VREM points pt:F
137
+ (integer) 1
138
+ 127.0.0.1:6379> VCARD points
139
+ (integer) 5
140
+ ```
141
+
142
+ ### Vector similarity search
143
+
144
+ Use [ ` VSIM ` ] ({{< relref "/commands/vsim" >}}) to rank the points in order of their vector distance from a sample point:
145
+
146
+ ```
147
+ > VSIM points values 2 0.9 0.1
148
+ 1) "pt:E"
149
+ 2) "pt:A"
150
+ 3) "pt:D"
151
+ 4) "pt:C"
152
+ 5) "pt:B"
153
+ ```
154
+
155
+ Find the four elements that are closest to point A and show their distance "scores":
156
+
157
+ ```
158
+ > VSIM points ELE pt:A WITHSCORES COUNT 4
159
+ 1) "pt:A"
160
+ 2) "1"
161
+ 3) "pt:E"
162
+ 4) "0.8535534143447876"
163
+ 5) "pt:C"
164
+ 6) "0.5"
165
+ 7) "pt:D"
166
+ 8) "0.5"
167
+ ```
168
+
169
+ Add some JSON attributes and use
170
+ [ filter expressions] ({{< relref "/develop/data-types/vector-sets/filtered-search" >}})
171
+ to include them in the search:
172
+
173
+ ```
174
+ > VSETATTR points pt:A "{\"size\":\"large\",\"price\": 18.99}"
175
+ (integer) 1
176
+ > VSETATTR points pt:B "{\"size\":\"large\",\"price\": 35.99}"
177
+ (integer) 1
178
+ > VSETATTR points pt:C "{\"size\":\"large\",\"price\": 25.99}"
179
+ (integer) 1
180
+ > VSETATTR points pt:D "{\"size\":\"small\",\"price\": 21.00}"
181
+ (integer) 1
182
+ > VSETATTR points pt:E "{\"size\":\"small\",\"price\": 17.75}"
183
+ (integer) 1
184
+
185
+ # Return elements in order of distance from point A whose
186
+ # `size` attribute is `large`.
187
+ > VSIM points ELE pt:A FILTER '.size == "large"'
188
+ 1) "pt:A"
189
+ 2) "pt:C"
190
+ 3) "pt:B"
191
+
192
+ # Return elements in order of distance from point A whose size is
193
+ # `large` and whose price is greater than 20.00.
194
+ > VSIM points ELE pt:A FILTER '.size == "large" && .price > 20.00'
195
+ 1) "pt:C"
196
+ 2) "pt:B"
197
+ ```
198
+
199
+ ## More information
200
+
201
+ See the other pages in this section to learn more about the features
202
+ and performance parameters of vector sets.
0 commit comments