38
38
import io .milvus .param .R ;
39
39
import io .milvus .param .R .Status ;
40
40
import io .milvus .param .RpcStatus ;
41
+ import io .milvus .param .collection .CollectionSchemaParam ;
41
42
import io .milvus .param .collection .CreateCollectionParam ;
42
43
import io .milvus .param .collection .DropCollectionParam ;
43
44
import io .milvus .param .collection .FieldType ;
@@ -443,6 +444,8 @@ void createCollection() {
443
444
if (!isDatabaseCollectionExists ()) {
444
445
createCollection (this .databaseName , this .collectionName , this .idFieldName , this .isAutoId ,
445
446
this .contentFieldName , this .metadataFieldName , this .embeddingFieldName );
447
+ createIndex (this .databaseName , this .collectionName , this .embeddingFieldName , this .indexType ,
448
+ this .metricType , this .indexParameters );
446
449
}
447
450
448
451
R <DescribeIndexResponse > indexDescriptionResponse = this .milvusClient
@@ -452,19 +455,8 @@ void createCollection() {
452
455
.build ());
453
456
454
457
if (indexDescriptionResponse .getData () == null ) {
455
- R <RpcStatus > indexStatus = this .milvusClient .createIndex (CreateIndexParam .newBuilder ()
456
- .withDatabaseName (this .databaseName )
457
- .withCollectionName (this .collectionName )
458
- .withFieldName (this .embeddingFieldName )
459
- .withIndexType (this .indexType )
460
- .withMetricType (this .metricType )
461
- .withExtraParam (this .indexParameters )
462
- .withSyncMode (Boolean .FALSE )
463
- .build ());
464
-
465
- if (indexStatus .getException () != null ) {
466
- throw new RuntimeException ("Failed to create Index" , indexStatus .getException ());
467
- }
458
+ createIndex (this .databaseName , this .collectionName , this .embeddingFieldName , this .indexType ,
459
+ this .metricType , this .indexParameters );
468
460
}
469
461
470
462
R <RpcStatus > loadCollectionStatus = this .milvusClient .loadCollection (LoadCollectionParam .newBuilder ()
@@ -507,10 +499,12 @@ void createCollection(String databaseName, String collectionName, String idField
507
499
.withDescription ("Spring AI Vector Store" )
508
500
.withConsistencyLevel (ConsistencyLevelEnum .STRONG )
509
501
.withShardsNum (2 )
510
- .addFieldType (docIdFieldType )
511
- .addFieldType (contentFieldType )
512
- .addFieldType (metadataFieldType )
513
- .addFieldType (embeddingFieldType )
502
+ .withSchema (CollectionSchemaParam .newBuilder ()
503
+ .addFieldType (docIdFieldType )
504
+ .addFieldType (contentFieldType )
505
+ .addFieldType (metadataFieldType )
506
+ .addFieldType (embeddingFieldType )
507
+ .build ())
514
508
.build ();
515
509
516
510
R <RpcStatus > collectionStatus = this .milvusClient .createCollection (createCollectionReq );
@@ -520,6 +514,23 @@ void createCollection(String databaseName, String collectionName, String idField
520
514
521
515
}
522
516
517
+ void createIndex (String databaseName , String collectionName , String embeddingFieldName , IndexType indexType ,
518
+ MetricType metricType , String indexParameters ) {
519
+ R <RpcStatus > indexStatus = this .milvusClient .createIndex (CreateIndexParam .newBuilder ()
520
+ .withDatabaseName (databaseName )
521
+ .withCollectionName (collectionName )
522
+ .withFieldName (embeddingFieldName )
523
+ .withIndexType (indexType )
524
+ .withMetricType (metricType )
525
+ .withExtraParam (indexParameters )
526
+ .withSyncMode (Boolean .FALSE )
527
+ .build ());
528
+
529
+ if (indexStatus .getException () != null ) {
530
+ throw new RuntimeException ("Failed to create Index" , indexStatus .getException ());
531
+ }
532
+ }
533
+
523
534
int embeddingDimensions () {
524
535
if (this .embeddingDimension != INVALID_EMBEDDING_DIMENSION ) {
525
536
return this .embeddingDimension ;
0 commit comments