44
44
*
45
45
* @author Christoph Strobl
46
46
*/
47
- public class VectorIndex implements IndexDefinition {
47
+ public class SearchIndex implements SearchIndexDefinition {
48
48
49
49
private final String name ;
50
50
private String path ;
51
51
private int dimensions ;
52
52
private String similarity ;
53
53
private List <Filter > filters ;
54
+ private String quantization = Quantization .NONE .name ();
54
55
55
56
/**
56
- * Create a new {@link VectorIndex } instance.
57
+ * Create a new {@link SearchIndex } instance.
57
58
*
58
59
* @param name The name of the index.
59
60
*/
60
- public VectorIndex (String name ) {
61
+ public SearchIndex (String name ) {
61
62
this .name = name ;
62
63
}
63
64
64
65
/**
65
- * Create a new {@link VectorIndex } instance using similarity based on the angle between vectors.
66
+ * Create a new {@link SearchIndex } instance using similarity based on the angle between vectors.
66
67
*
67
68
* @param name The name of the index.
68
- * @return new instance of {@link VectorIndex }.
69
+ * @return new instance of {@link SearchIndex }.
69
70
*/
70
- public static VectorIndex cosine (String name ) {
71
+ public static SearchIndex cosine (String name ) {
71
72
72
- VectorIndex idx = new VectorIndex (name );
73
+ SearchIndex idx = new SearchIndex (name );
73
74
return idx .similarity (SimilarityFunction .COSINE );
74
75
}
75
76
76
77
/**
77
- * Create a new {@link VectorIndex } instance using similarity based the distance between vector ends.
78
+ * Create a new {@link SearchIndex } instance using similarity based the distance between vector ends.
78
79
*
79
80
* @param name The name of the index.
80
- * @return new instance of {@link VectorIndex }.
81
+ * @return new instance of {@link SearchIndex }.
81
82
*/
82
- public static VectorIndex euclidean (String name ) {
83
+ public static SearchIndex euclidean (String name ) {
83
84
84
- VectorIndex idx = new VectorIndex (name );
85
+ SearchIndex idx = new SearchIndex (name );
85
86
return idx .similarity (SimilarityFunction .EUCLIDEAN );
86
87
}
87
88
88
89
/**
89
- * Create a new {@link VectorIndex } instance using similarity based on based on both angle and magnitude of the
90
+ * Create a new {@link SearchIndex } instance using similarity based on based on both angle and magnitude of the
90
91
* vectors.
91
92
*
92
93
* @param name The name of the index.
93
- * @return new instance of {@link VectorIndex }.
94
+ * @return new instance of {@link SearchIndex }.
94
95
*/
95
- public static VectorIndex dotProduct (String name ) {
96
+ public static SearchIndex dotProduct (String name ) {
96
97
97
- VectorIndex idx = new VectorIndex (name );
98
+ SearchIndex idx = new SearchIndex (name );
98
99
return idx .similarity (SimilarityFunction .DOT_PRODUCT );
99
100
}
100
101
@@ -104,7 +105,7 @@ public static VectorIndex dotProduct(String name) {
104
105
* @param path The path using dot notation.
105
106
* @return this.
106
107
*/
107
- public VectorIndex path (String path ) {
108
+ public SearchIndex path (String path ) {
108
109
109
110
this .path = path ;
110
111
return this ;
@@ -116,7 +117,7 @@ public VectorIndex path(String path) {
116
117
* @param dimensions value between {@code 0} and {@code 4096}.
117
118
* @return this.
118
119
*/
119
- public VectorIndex dimensions (int dimensions ) {
120
+ public SearchIndex dimensions (int dimensions ) {
120
121
this .dimensions = dimensions ;
121
122
return this ;
122
123
}
@@ -129,7 +130,7 @@ public VectorIndex dimensions(int dimensions) {
129
130
* @see SimilarityFunction
130
131
* @see #similarity(SimilarityFunction)
131
132
*/
132
- public VectorIndex similarity (String similarity ) {
133
+ public SearchIndex similarity (String similarity ) {
133
134
this .similarity = similarity ;
134
135
return this ;
135
136
}
@@ -140,17 +141,41 @@ public VectorIndex similarity(String similarity) {
140
141
* @param similarity must not be {@literal null}.
141
142
* @return this.
142
143
*/
143
- public VectorIndex similarity (SimilarityFunction similarity ) {
144
+ public SearchIndex similarity (SimilarityFunction similarity ) {
144
145
return similarity (similarity .getFunctionName ());
145
146
}
146
147
148
+
149
+ /**
150
+ * Quantization used.
151
+ *
152
+ * @param quantization should be one of {@literal none | scalar | binary}.
153
+ * @return this.
154
+ * @see Quantization
155
+ * @see #quantization(Quantization)
156
+ */
157
+ public SearchIndex quantization (String quantization ) {
158
+ this .quantization = quantization ;
159
+ return this ;
160
+ }
161
+
162
+ /**
163
+ * Quntization used.
164
+ *
165
+ * @param quantization must not be {@literal null}.
166
+ * @return this.
167
+ */
168
+ public SearchIndex quantization (Quantization quantization ) {
169
+ return similarity (quantization .getQuantizationName ());
170
+ }
171
+
147
172
/**
148
173
* Add a {@link Filter} that can be used to narrow search scope.
149
174
*
150
175
* @param filter must not be {@literal null}.
151
176
* @return this.
152
177
*/
153
- public VectorIndex filter (Filter filter ) {
178
+ public SearchIndex filter (Filter filter ) {
154
179
155
180
if (this .filters == null ) {
156
181
this .filters = new ArrayList <>(3 );
@@ -167,21 +192,10 @@ public VectorIndex filter(Filter filter) {
167
192
* @return this.
168
193
* @see #filter(Filter)
169
194
*/
170
- public VectorIndex filter (String path ) {
195
+ public SearchIndex filter (String path ) {
171
196
return filter (new Filter (path ));
172
197
}
173
198
174
- @ Override
175
- public Document getIndexKeys () {
176
-
177
- // List<Document> fields = new ArrayList<>(filters.size()+1);
178
- // fields.
179
-
180
- // needs to be wrapped in new Document("definition", before sending to server
181
- // return new Document("fields", fields);
182
- return new Document ();
183
- }
184
-
185
199
@ Override
186
200
public Document getIndexOptions () {
187
201
return new Document ("name" , name ).append ("type" , "vectorSearch" );
@@ -224,4 +238,18 @@ public String getFunctionName() {
224
238
return functionName ;
225
239
}
226
240
}
241
+
242
+ public enum Quantization {
243
+ NONE ("none" ), SCALAR ("scalar" ), BINARY ("binary" );
244
+
245
+ String quantizationName ;
246
+
247
+ Quantization (String quantizationName ) {
248
+ this .quantizationName = quantizationName ;
249
+ }
250
+
251
+ public String getQuantizationName () {
252
+ return quantizationName ;
253
+ }
254
+ }
227
255
}
0 commit comments