6
6
import java .util .LinkedHashMap ;
7
7
import java .util .Map ;
8
8
import redis .clients .jedis .CommandArguments ;
9
+ import redis .clients .jedis .args .Rawable ;
9
10
import redis .clients .jedis .search .FieldName ;
11
+ import redis .clients .jedis .util .SafeEncoder ;
10
12
13
+ /**
14
+ * Represents a vector field in a Redis search index schema for performing semantic vector searches.
15
+ * Vector fields enable high-performance similarity searches over vector embeddings using various
16
+ * algorithms and distance metrics.
17
+ *
18
+ * @see <a href="https://redis.io/docs/latest/develop/ai/search-and-query/vectors/">Redis Vector Search Documentation</a>
19
+ */
11
20
public class VectorField extends SchemaField {
12
21
13
- public enum VectorAlgorithm {
14
- FLAT ,
15
- HNSW
22
+ /**
23
+ * Enumeration of supported vector indexing algorithms in Redis.
24
+ * Each algorithm has different performance characteristics and use cases.
25
+ */
26
+ public enum VectorAlgorithm implements Rawable {
27
+
28
+ /**
29
+ * FLAT algorithm provides exact vector search with perfect accuracy.
30
+ * Best suited for smaller datasets (< 1M vectors) where search accuracy
31
+ * is more important than search latency.
32
+ */
33
+ FLAT ("FLAT" ),
34
+
35
+ /**
36
+ * HNSW (Hierarchical Navigable Small World) algorithm provides approximate
37
+ * vector search with configurable accuracy-performance trade-offs.
38
+ * Best suited for larger datasets (> 1M vectors) where search performance
39
+ * and scalability are more important than perfect accuracy.
40
+ */
41
+ HNSW ("HNSW" ),
42
+
43
+ /**
44
+ * SVS_VAMANA algorithm provides high-performance approximate vector search
45
+ * optimized for specific use cases with advanced compression and optimization features.
46
+ *
47
+ * <p>Characteristics:
48
+ * <ul>
49
+ * <li>High-performance approximate search</li>
50
+ * <li>Support for vector compression (LVQ, LeanVec)</li>
51
+ * <li>Configurable graph construction and search parameters</li>
52
+ * <li>Optimized for Intel platforms with fallback support</li>
53
+ * </ul>
54
+ *
55
+ * <p>Note: This algorithm may have specific requirements and limitations.
56
+ * Consult the Redis documentation for detailed usage guidelines.
57
+ */
58
+ SVS_VAMANA ("SVS-VAMANA" );
59
+
60
+ private final byte [] raw ;
61
+
62
+ /**
63
+ * Creates a VectorAlgorithm enum value.
64
+ *
65
+ * @param redisParamName the Redis parameter name for this algorithm
66
+ */
67
+ VectorAlgorithm (String redisParamName ) {
68
+ raw = SafeEncoder .encode (redisParamName );
69
+ }
70
+
71
+ /**
72
+ * Returns the raw byte representation of the algorithm name for Redis commands.
73
+ *
74
+ * @return the raw bytes of the algorithm name
75
+ */
76
+ @ Override
77
+ public byte [] getRaw () {
78
+ return raw ;
79
+ }
16
80
}
17
81
18
82
private final VectorAlgorithm algorithm ;
@@ -21,29 +85,70 @@ public enum VectorAlgorithm {
21
85
private boolean indexMissing ;
22
86
// private boolean noIndex; // throws Field `NOINDEX` does not have a type
23
87
88
+ /**
89
+ * Creates a new VectorField with the specified field name, algorithm, and attributes.
90
+ *
91
+ * @param fieldName the name of the vector field in the index
92
+ * @param algorithm the vector indexing algorithm to use
93
+ * @param attributes the algorithm-specific configuration attributes
94
+ * @throws IllegalArgumentException if required attributes are missing or invalid
95
+ */
24
96
public VectorField (String fieldName , VectorAlgorithm algorithm , Map <String , Object > attributes ) {
25
97
super (fieldName );
26
98
this .algorithm = algorithm ;
27
99
this .attributes = attributes ;
28
100
}
29
101
102
+ /**
103
+ * Creates a new VectorField with the specified field name, algorithm, and attributes.
104
+ *
105
+ * @param fieldName the field name object containing the field name and optional alias
106
+ * @param algorithm the vector indexing algorithm to use
107
+ * @param attributes the algorithm-specific configuration attributes
108
+ * @throws IllegalArgumentException if required attributes are missing or invalid
109
+ * @see #VectorField(String, VectorAlgorithm, Map) for detailed attribute documentation
110
+ */
30
111
public VectorField (FieldName fieldName , VectorAlgorithm algorithm , Map <String , Object > attributes ) {
31
112
super (fieldName );
32
113
this .algorithm = algorithm ;
33
114
this .attributes = attributes ;
34
115
}
35
116
117
+ /**
118
+ * Sets an alias for this field that can be used in queries instead of the field name.
119
+ * This is useful when the field name contains special characters or when you want
120
+ * to use a shorter name in queries.
121
+ *
122
+ * @param attribute the alias name to use for this field in queries
123
+ * @return this VectorField instance for method chaining
124
+ */
36
125
@ Override
37
126
public VectorField as (String attribute ) {
38
127
super .as (attribute );
39
128
return this ;
40
129
}
41
130
131
+ /**
132
+ * Configures the field to handle missing values during indexing.
133
+ * When enabled, documents that don't contain this vector field will still be indexed,
134
+ * but won't participate in vector searches.
135
+ *
136
+ * <p>This is useful when not all documents in your dataset contain vector embeddings,
137
+ * but you still want to index them for other types of searches.
138
+ *
139
+ * @return this VectorField instance for method chaining
140
+ */
42
141
public VectorField indexMissing () {
43
142
this .indexMissing = true ;
44
143
return this ;
45
144
}
46
145
146
+ /**
147
+ * Adds the vector field parameters to the Redis command arguments.
148
+ * This method is used internally when creating the search index.
149
+ *
150
+ * @param args the command arguments to add parameters to
151
+ */
47
152
@ Override
48
153
public void addParams (CommandArguments args ) {
49
154
args .addParams (fieldName );
@@ -58,51 +163,122 @@ public void addParams(CommandArguments args) {
58
163
}
59
164
}
60
165
166
+ /**
167
+ * Creates a new Builder instance for constructing VectorField objects using the builder pattern.
168
+ * The builder pattern provides a fluent interface for setting field properties and is especially
169
+ * useful when dealing with complex vector field configurations.
170
+ *
171
+ * @return a new Builder instance
172
+ */
61
173
public static Builder builder () {
62
174
return new Builder ();
63
175
}
64
176
177
+ /**
178
+ * Builder class for constructing VectorField instances using the builder pattern.
179
+ * Provides a fluent interface for setting vector field properties and attributes.
180
+ *
181
+ * <p>Example usage:
182
+ * <pre>{@code
183
+ * VectorField field = VectorField.builder()
184
+ * .fieldName("product_embedding")
185
+ * .algorithm(VectorAlgorithm.HNSW)
186
+ * .addAttribute("TYPE", "FLOAT32")
187
+ * .addAttribute("DIM", 768)
188
+ * .addAttribute("DISTANCE_METRIC", "COSINE")
189
+ * .addAttribute("M", 32)
190
+ * .addAttribute("EF_CONSTRUCTION", 200)
191
+ * .build();
192
+ * }</pre>
193
+ */
65
194
public static class Builder {
66
195
67
196
private FieldName fieldName ;
68
197
private VectorAlgorithm algorithm ;
69
198
private Map <String , Object > attributes ;
70
199
200
+ /**
201
+ * Private constructor to enforce use of the static builder() method.
202
+ */
71
203
private Builder () {
72
204
}
73
205
206
+ /**
207
+ * Builds and returns a new VectorField instance with the configured properties.
208
+ *
209
+ * @return a new VectorField instance
210
+ * @throws IllegalArgumentException if required parameters (fieldName, algorithm, or attributes) are not set
211
+ */
74
212
public VectorField build () {
75
213
if (fieldName == null || algorithm == null || attributes == null || attributes .isEmpty ()) {
76
214
throw new IllegalArgumentException ("All required VectorField parameters are not set." );
77
215
}
78
216
return new VectorField (fieldName , algorithm , attributes );
79
217
}
80
218
219
+ /**
220
+ * Sets the field name for the vector field.
221
+ *
222
+ * @param fieldName the name of the vector field in the index
223
+ * @return this Builder instance for method chaining
224
+ */
81
225
public Builder fieldName (String fieldName ) {
82
226
this .fieldName = FieldName .of (fieldName );
83
227
return this ;
84
228
}
85
229
230
+ /**
231
+ * Sets the field name using a FieldName object.
232
+ *
233
+ * @param fieldName the FieldName object containing the field name and optional alias
234
+ * @return this Builder instance for method chaining
235
+ */
86
236
public Builder fieldName (FieldName fieldName ) {
87
237
this .fieldName = fieldName ;
88
238
return this ;
89
239
}
90
240
241
+ /**
242
+ * Sets an alias for the field that can be used in queries.
243
+ *
244
+ * @param attribute the alias name to use for this field in queries
245
+ * @return this Builder instance for method chaining
246
+ */
91
247
public Builder as (String attribute ) {
92
248
this .fieldName .as (attribute );
93
249
return this ;
94
250
}
95
251
252
+ /**
253
+ * Sets the vector indexing algorithm to use.
254
+ *
255
+ * @param algorithm the vector algorithm (FLAT, HNSW, or SVS_VAMANA)
256
+ * @return this Builder instance for method chaining
257
+ */
96
258
public Builder algorithm (VectorAlgorithm algorithm ) {
97
259
this .algorithm = algorithm ;
98
260
return this ;
99
261
}
100
262
263
+ /**
264
+ * Sets all vector field attributes at once, replacing any previously set attributes.
265
+ *
266
+ * @param attributes a map of attribute names to values
267
+ * @return this Builder instance for method chaining
268
+ */
101
269
public Builder attributes (Map <String , Object > attributes ) {
102
270
this .attributes = attributes ;
103
271
return this ;
104
272
}
105
273
274
+ /**
275
+ * Adds a single attribute to the vector field configuration.
276
+ * If this is the first attribute added, initializes the attributes map.
277
+ *
278
+ * @param name the attribute name
279
+ * @param value the attribute value
280
+ * @return this Builder instance for method chaining
281
+ */
106
282
public Builder addAttribute (String name , Object value ) {
107
283
if (this .attributes == null ) {
108
284
this .attributes = new LinkedHashMap <>();
0 commit comments