@@ -97,6 +97,8 @@ public class MilvusIT extends TestSuiteBase implements TestResource {
9797 private static final String COLLECTION_NAME = "simple_example" ;
9898 private static final String COLLECTION_NAME_1 = "simple_example_1" ;
9999 private static final String COLLECTION_NAME_2 = "simple_example_2" ;
100+ private static final String COLLECTION_NAME_WITH_PARTITIONKEY =
101+ "simple_example_with_partitionkey" ;
100102 private static final String ID_FIELD = "book_id" ;
101103 private static final String VECTOR_FIELD = "book_intro" ;
102104 private static final String VECTOR_FIELD2 = "book_kind" ;
@@ -243,6 +245,112 @@ private void initSourceData() {
243245
244246 log .info ("Collection created" );
245247
248+ // Define fields With Partition Key
249+ List <FieldType > fieldsSchemaWithPartitionKey =
250+ Arrays .asList (
251+ FieldType .newBuilder ()
252+ .withName (ID_FIELD )
253+ .withDataType (DataType .Int64 )
254+ .withPrimaryKey (true )
255+ .withAutoID (false )
256+ .build (),
257+ FieldType .newBuilder ()
258+ .withName (VECTOR_FIELD )
259+ .withDataType (DataType .FloatVector )
260+ .withDimension (VECTOR_DIM )
261+ .build (),
262+ FieldType .newBuilder ()
263+ .withName (VECTOR_FIELD2 )
264+ .withDataType (DataType .Float16Vector )
265+ .withDimension (VECTOR_DIM )
266+ .build (),
267+ FieldType .newBuilder ()
268+ .withName (VECTOR_FIELD3 )
269+ .withDataType (DataType .BinaryVector )
270+ .withDimension (VECTOR_DIM * 2 )
271+ .build (),
272+ FieldType .newBuilder ()
273+ .withName (VECTOR_FIELD4 )
274+ .withDataType (DataType .SparseFloatVector )
275+ .build (),
276+ FieldType .newBuilder ()
277+ .withName (TITLE_FIELD )
278+ .withDataType (DataType .VarChar )
279+ .withPartitionKey (true )
280+ .withMaxLength (64 )
281+ .build ());
282+
283+ // Create the collection with 3 fields
284+ R <RpcStatus > ret2 =
285+ milvusClient .createCollection (
286+ CreateCollectionParam .newBuilder ()
287+ .withCollectionName (COLLECTION_NAME_WITH_PARTITIONKEY )
288+ .withFieldTypes (fieldsSchemaWithPartitionKey )
289+ .build ());
290+ if (ret2 .getStatus () != R .Status .Success .getCode ()) {
291+ throw new RuntimeException ("Failed to create collection! Error: " + ret .getMessage ());
292+ }
293+
294+ // Specify an index type on the vector field.
295+ ret2 =
296+ milvusClient .createIndex (
297+ CreateIndexParam .newBuilder ()
298+ .withCollectionName (COLLECTION_NAME_WITH_PARTITIONKEY )
299+ .withFieldName (VECTOR_FIELD )
300+ .withIndexType (IndexType .FLAT )
301+ .withMetricType (MetricType .L2 )
302+ .build ());
303+ if (ret2 .getStatus () != R .Status .Success .getCode ()) {
304+ throw new RuntimeException (
305+ "Failed to create index on vector field! Error: " + ret .getMessage ());
306+ }
307+
308+ ret2 =
309+ milvusClient .createIndex (
310+ CreateIndexParam .newBuilder ()
311+ .withCollectionName (COLLECTION_NAME_WITH_PARTITIONKEY )
312+ .withFieldName (VECTOR_FIELD2 )
313+ .withIndexType (IndexType .FLAT )
314+ .withMetricType (MetricType .L2 )
315+ .build ());
316+ if (ret2 .getStatus () != R .Status .Success .getCode ()) {
317+ throw new RuntimeException (
318+ "Failed to create index on vector field! Error: " + ret .getMessage ());
319+ }
320+ ret2 =
321+ milvusClient .createIndex (
322+ CreateIndexParam .newBuilder ()
323+ .withCollectionName (COLLECTION_NAME_WITH_PARTITIONKEY )
324+ .withFieldName (VECTOR_FIELD3 )
325+ .withIndexType (IndexType .BIN_FLAT )
326+ .withMetricType (MetricType .HAMMING )
327+ .build ());
328+ if (ret2 .getStatus () != R .Status .Success .getCode ()) {
329+ throw new RuntimeException (
330+ "Failed to create index on vector field! Error: " + ret .getMessage ());
331+ }
332+
333+ ret2 =
334+ milvusClient .createIndex (
335+ CreateIndexParam .newBuilder ()
336+ .withCollectionName (COLLECTION_NAME_WITH_PARTITIONKEY )
337+ .withFieldName (VECTOR_FIELD4 )
338+ .withIndexType (IndexType .SPARSE_INVERTED_INDEX )
339+ .withMetricType (MetricType .IP )
340+ .build ());
341+ if (ret2 .getStatus () != R .Status .Success .getCode ()) {
342+ throw new RuntimeException (
343+ "Failed to create index on vector field! Error: " + ret .getMessage ());
344+ }
345+
346+ // Call loadCollection() to enable automatically loading data into memory for searching
347+ milvusClient .loadCollection (
348+ LoadCollectionParam .newBuilder ()
349+ .withCollectionName (COLLECTION_NAME_WITH_PARTITIONKEY )
350+ .build ());
351+
352+ log .info ("Collection created" );
353+
246354 // Insert 10 records into the collection
247355 List <JsonObject > rows = new ArrayList <>();
248356 for (long i = 1L ; i <= 10 ; ++i ) {
@@ -272,7 +380,16 @@ private void initSourceData() {
272380 .withCollectionName (COLLECTION_NAME )
273381 .withRows (rows )
274382 .build ());
275- if (insertRet .getStatus () != R .Status .Success .getCode ()) {
383+
384+ R <MutationResult > insertRet2 =
385+ milvusClient .insert (
386+ InsertParam .newBuilder ()
387+ .withCollectionName (COLLECTION_NAME_WITH_PARTITIONKEY )
388+ .withRows (rows )
389+ .build ());
390+
391+ if (insertRet .getStatus () != R .Status .Success .getCode ()
392+ || insertRet2 .getStatus () != R .Status .Success .getCode ()) {
276393 throw new RuntimeException ("Failed to insert! Error: " + insertRet .getMessage ());
277394 }
278395 }
@@ -322,6 +439,43 @@ public void testMilvus(TestContainer container) throws IOException, InterruptedE
322439 Assertions .assertTrue (fileds .contains (TITLE_FIELD ));
323440 }
324441
442+ @ TestTemplate
443+ public void testMilvusWithPartitionKey (TestContainer container )
444+ throws IOException , InterruptedException {
445+ Container .ExecResult execResult =
446+ container .executeJob ("/milvus-to-milvus-with-partitionkey.conf" );
447+ Assertions .assertEquals (0 , execResult .getExitCode ());
448+
449+ // assert table exist
450+ R <Boolean > hasCollectionResponse =
451+ this .milvusClient .hasCollection (
452+ HasCollectionParam .newBuilder ()
453+ .withDatabaseName ("test" )
454+ .withCollectionName (COLLECTION_NAME_WITH_PARTITIONKEY )
455+ .build ());
456+ Assertions .assertTrue (hasCollectionResponse .getData ());
457+
458+ // check table fields
459+ R <DescribeCollectionResponse > describeCollectionResponseR =
460+ this .milvusClient .describeCollection (
461+ DescribeCollectionParam .newBuilder ()
462+ .withDatabaseName ("test" )
463+ .withCollectionName (COLLECTION_NAME_WITH_PARTITIONKEY )
464+ .build ());
465+
466+ DescribeCollectionResponse data = describeCollectionResponseR .getData ();
467+ List <String > fileds =
468+ data .getSchema ().getFieldsList ().stream ()
469+ .map (FieldSchema ::getName )
470+ .collect (Collectors .toList ());
471+ Assertions .assertTrue (fileds .contains (ID_FIELD ));
472+ Assertions .assertTrue (fileds .contains (VECTOR_FIELD ));
473+ Assertions .assertTrue (fileds .contains (VECTOR_FIELD2 ));
474+ Assertions .assertTrue (fileds .contains (VECTOR_FIELD3 ));
475+ Assertions .assertTrue (fileds .contains (VECTOR_FIELD4 ));
476+ Assertions .assertTrue (fileds .contains (TITLE_FIELD ));
477+ }
478+
325479 @ TestTemplate
326480 public void testFakeToMilvus (TestContainer container ) throws IOException , InterruptedException {
327481 Container .ExecResult execResult = container .executeJob ("/fake-to-milvus.conf" );
0 commit comments