24
24
import org .opensearch .knn .index .VectorDataType ;
25
25
import org .opensearch .knn .index .codec .nativeindex .model .BuildIndexParams ;
26
26
import org .opensearch .knn .index .engine .KNNEngine ;
27
- import org .opensearch .knn .index .engine .KNNIndexContext ;
28
- import org .opensearch .knn .index .quantizationService .QuantizationService ;
29
- import org .opensearch .knn .index .util .IndexUtil ;
30
27
import org .opensearch .knn .index .vectorvalues .KNNVectorValues ;
31
28
import org .opensearch .knn .indices .Model ;
32
- import org .opensearch .knn .indices .ModelCache ;
33
29
import org .opensearch .knn .indices .ModelUtil ;
34
30
import org .opensearch .knn .plugin .stats .KNNGraphValue ;
35
31
import org .opensearch .knn .quantization .models .quantizationState .QuantizationState ;
47
43
import static org .apache .lucene .codecs .CodecUtil .FOOTER_MAGIC ;
48
44
import static org .apache .lucene .search .DocIdSetIterator .NO_MORE_DOCS ;
49
45
import static org .opensearch .knn .common .FieldInfoExtractor .extractKNNEngine ;
50
- import static org .opensearch .knn .common .FieldInfoExtractor .extractVectorDataType ;
46
+ import static org .opensearch .knn .common .FieldInfoExtractor .extractVectorDataTypeForTransfer ;
51
47
import static org .opensearch .knn .common .KNNConstants .MODEL_ID ;
52
48
import static org .opensearch .knn .common .KNNConstants .PARAMETERS ;
53
49
import static org .opensearch .knn .common .KNNConstants .VECTOR_DATA_TYPE_FIELD ;
@@ -161,17 +157,14 @@ private void buildAndWriteIndex(final KNNVectorValues<?> knnVectorValues) throws
161
157
// TODO: Refactor this so its scalable. Possibly move it out of this class
162
158
private BuildIndexParams indexParams (FieldInfo fieldInfo , String indexPath , KNNEngine knnEngine ) throws IOException {
163
159
final Map <String , Object > parameters ;
164
- VectorDataType vectorDataType ;
165
- if (quantizationState != null ) {
166
- vectorDataType = QuantizationService .getInstance ().getVectorDataTypeForTransfer (fieldInfo );
167
- } else {
168
- vectorDataType = extractVectorDataType (fieldInfo );
169
- }
170
- if (fieldInfo .attributes ().containsKey (MODEL_ID )) {
171
- Model model = getModel (fieldInfo );
172
- parameters = getTemplateParameters (fieldInfo , model );
173
- } else {
160
+ VectorDataType vectorDataType = extractVectorDataTypeForTransfer (
161
+ fieldInfo ,
162
+ quantizationState == null ? null : quantizationState .getQuantizationParams ()
163
+ );
164
+ if (fieldInfo .attributes ().containsKey (MODEL_ID ) == false ) {
174
165
parameters = getParameters (fieldInfo , vectorDataType , knnEngine );
166
+ } else {
167
+ parameters = getTemplateParameters (fieldInfo , vectorDataType );
175
168
}
176
169
177
170
return BuildIndexParams .builder ()
@@ -215,7 +208,6 @@ private Map<String, Object> getParameters(FieldInfo fieldInfo, VectorDataType ve
215
208
);
216
209
}
217
210
218
- parameters .put (KNNConstants .VECTOR_DATA_TYPE_FIELD , vectorDataType .getValue ());
219
211
// In OpenSearch 2.16, we added the prefix for binary indices in the index description in the codec logic.
220
212
// After 2.16, we added the binary prefix in the faiss library code. However, to ensure backwards compatibility,
221
213
// we need to ensure that if the description does not contain the prefix but the type is binary, we add the
@@ -228,60 +220,20 @@ private Map<String, Object> getParameters(FieldInfo fieldInfo, VectorDataType ve
228
220
return parameters ;
229
221
}
230
222
231
- private void maybeAddBinaryPrefixForFaissBWC (KNNEngine knnEngine , Map <String , Object > parameters , Map <String , String > fieldAttributes ) {
232
- if (KNNEngine .FAISS != knnEngine ) {
233
- return ;
234
- }
235
-
236
- if (!VectorDataType .BINARY .getValue ()
237
- .equals (fieldAttributes .getOrDefault (KNNConstants .VECTOR_DATA_TYPE_FIELD , VectorDataType .DEFAULT .getValue ()))) {
238
- return ;
239
- }
240
-
241
- if (parameters .get (KNNConstants .INDEX_DESCRIPTION_PARAMETER ) == null ) {
242
- return ;
243
- }
244
-
245
- if (parameters .get (KNNConstants .INDEX_DESCRIPTION_PARAMETER ).toString ().startsWith (FAISS_BINARY_INDEX_DESCRIPTION_PREFIX )) {
246
- return ;
223
+ private Map <String , Object > getTemplateParameters (FieldInfo fieldInfo , VectorDataType vectorDataTypeForTransfer ) {
224
+ Model model = ModelUtil .getModel (fieldInfo .getAttribute (MODEL_ID ));
225
+ if (model == null ) {
226
+ throw new IllegalStateException ("Model not found for field " + fieldInfo .name );
247
227
}
248
228
249
- parameters .put (
250
- KNNConstants .INDEX_DESCRIPTION_PARAMETER ,
251
- FAISS_BINARY_INDEX_DESCRIPTION_PREFIX + parameters .get (KNNConstants .INDEX_DESCRIPTION_PARAMETER ).toString ()
252
- );
253
- IndexUtil .updateVectorDataTypeToParameters (parameters , VectorDataType .BINARY );
254
- }
255
-
256
- private Map <String , Object > getTemplateParameters (FieldInfo fieldInfo , Model model ) throws IOException {
257
229
Map <String , Object > parameters = new HashMap <>();
258
230
parameters .put (KNNConstants .INDEX_THREAD_QTY , KNNSettings .state ().getSettingValue (KNNSettings .KNN_ALGO_PARAM_INDEX_THREAD_QTY ));
259
- parameters .put (KNNConstants .MODEL_ID , fieldInfo . attributes (). get ( MODEL_ID ));
231
+ parameters .put (KNNConstants .MODEL_ID , model . getModelID ( ));
260
232
parameters .put (KNNConstants .MODEL_BLOB_PARAMETER , model .getModelBlob ());
261
-
262
- // TODO: Is there any way we could avoid resolving it like this?
263
- KNNIndexContext knnIndexContext = ModelUtil .getKnnMethodContextFromModelMetadata (model .getModelID (), model .getModelMetadata ());
264
- if (knnIndexContext != null && knnIndexContext .getLibraryParameters ().containsKey (VECTOR_DATA_TYPE_FIELD )) {
265
- IndexUtil .updateVectorDataTypeToParameters (
266
- parameters ,
267
- VectorDataType .get ((String ) knnIndexContext .getLibraryParameters ().get (VECTOR_DATA_TYPE_FIELD ))
268
- );
269
- } else {
270
- IndexUtil .updateVectorDataTypeToParameters (parameters , model .getModelMetadata ().getVectorDataType ());
271
- }
272
-
233
+ parameters .put (VECTOR_DATA_TYPE_FIELD , vectorDataTypeForTransfer .getValue ());
273
234
return parameters ;
274
235
}
275
236
276
- private Model getModel (FieldInfo fieldInfo ) {
277
- String modelId = fieldInfo .attributes ().get (MODEL_ID );
278
- Model model = ModelCache .getInstance ().get (modelId );
279
- if (model .getModelBlob () == null ) {
280
- throw new RuntimeException (String .format ("There is no trained model with id \" %s\" " , modelId ));
281
- }
282
- return model ;
283
- }
284
-
285
237
private void startMergeStats (int numDocs , long bytesPerVector ) {
286
238
KNNGraphValue .MERGE_CURRENT_OPERATIONS .increment ();
287
239
KNNGraphValue .MERGE_CURRENT_DOCS .incrementBy (numDocs );
@@ -358,4 +310,30 @@ private static NativeIndexWriter createWriter(
358
310
: DefaultIndexBuildStrategy .getInstance ();
359
311
return new NativeIndexWriter (state , fieldInfo , strategy , quantizationState );
360
312
}
313
+
314
+ private void maybeAddBinaryPrefixForFaissBWC (KNNEngine knnEngine , Map <String , Object > parameters , Map <String , String > fieldAttributes ) {
315
+ if (KNNEngine .FAISS != knnEngine ) {
316
+ return ;
317
+ }
318
+
319
+ if (!VectorDataType .BINARY .getValue ()
320
+ .equals (fieldAttributes .getOrDefault (KNNConstants .VECTOR_DATA_TYPE_FIELD , VectorDataType .DEFAULT .getValue ()))) {
321
+ return ;
322
+ }
323
+
324
+ if (parameters .get (KNNConstants .INDEX_DESCRIPTION_PARAMETER ) == null ) {
325
+ return ;
326
+ }
327
+
328
+ if (parameters .get (KNNConstants .INDEX_DESCRIPTION_PARAMETER ).toString ().startsWith (FAISS_BINARY_INDEX_DESCRIPTION_PREFIX )) {
329
+ return ;
330
+ }
331
+
332
+ parameters .put (
333
+ KNNConstants .INDEX_DESCRIPTION_PARAMETER ,
334
+ FAISS_BINARY_INDEX_DESCRIPTION_PREFIX + parameters .get (KNNConstants .INDEX_DESCRIPTION_PARAMETER ).toString ()
335
+ );
336
+
337
+ parameters .put (VECTOR_DATA_TYPE_FIELD , VectorDataType .BINARY .getValue ());
338
+ }
361
339
}
0 commit comments