Browse Source

Support clustering key (#1085)

Signed-off-by: yhmo <yihua.mo@zilliz.com>
groot 9 months ago
parent
commit
332045540d

+ 2 - 0
src/main/java/io/milvus/param/ParamUtils.java

@@ -1188,6 +1188,7 @@ public class ParamUtils {
                 .withDescription(field.getDescription())
                 .withDescription(field.getDescription())
                 .withPrimaryKey(field.getIsPrimaryKey())
                 .withPrimaryKey(field.getIsPrimaryKey())
                 .withPartitionKey(field.getIsPartitionKey())
                 .withPartitionKey(field.getIsPartitionKey())
+                .withClusteringKey(field.getIsClusteringKey())
                 .withAutoID(field.getAutoID())
                 .withAutoID(field.getAutoID())
                 .withDataType(field.getDataType())
                 .withDataType(field.getDataType())
                 .withElementType(field.getElementType())
                 .withElementType(field.getElementType())
@@ -1215,6 +1216,7 @@ public class ParamUtils {
                 .setDescription(field.getDescription())
                 .setDescription(field.getDescription())
                 .setIsPrimaryKey(field.isPrimaryKey())
                 .setIsPrimaryKey(field.isPrimaryKey())
                 .setIsPartitionKey(field.isPartitionKey())
                 .setIsPartitionKey(field.isPartitionKey())
+                .setIsClusteringKey(field.isClusteringKey())
                 .setAutoID(field.isAutoID())
                 .setAutoID(field.isAutoID())
                 .setDataType(field.getDataType())
                 .setDataType(field.getDataType())
                 .setElementType(field.getElementType())
                 .setElementType(field.getElementType())

+ 21 - 0
src/main/java/io/milvus/param/collection/FieldType.java

@@ -45,6 +45,7 @@ public class FieldType {
     private final Map<String,String> typeParams;
     private final Map<String,String> typeParams;
     private final boolean autoID;
     private final boolean autoID;
     private final boolean partitionKey;
     private final boolean partitionKey;
+    private final boolean clusteringKey;
     private final boolean isDynamic;
     private final boolean isDynamic;
     private final DataType elementType;
     private final DataType elementType;
 
 
@@ -56,6 +57,7 @@ public class FieldType {
         this.typeParams = builder.typeParams;
         this.typeParams = builder.typeParams;
         this.autoID = builder.autoID;
         this.autoID = builder.autoID;
         this.partitionKey = builder.partitionKey;
         this.partitionKey = builder.partitionKey;
+        this.clusteringKey = builder.clusteringKey;
         this.isDynamic = builder.isDynamic;
         this.isDynamic = builder.isDynamic;
         this.elementType = builder.elementType;
         this.elementType = builder.elementType;
     }
     }
@@ -99,6 +101,7 @@ public class FieldType {
         private final Map<String,String> typeParams = new HashMap<>();
         private final Map<String,String> typeParams = new HashMap<>();
         private boolean autoID = false;
         private boolean autoID = false;
         private boolean partitionKey = false;
         private boolean partitionKey = false;
+        private boolean clusteringKey = false;
         private boolean isDynamic = false;
         private boolean isDynamic = false;
         private DataType elementType = DataType.None; // only for Array type field
         private DataType elementType = DataType.None; // only for Array type field
 
 
@@ -255,6 +258,24 @@ public class FieldType {
             return this;
             return this;
         }
         }
 
 
+        /**
+         * Set the field to be a clustering key.
+         * A clustering key can notify milvus to trigger a clustering compaction to redistribute entities among segments
+         * in a collection based on the values in the clustering key field. And one global index called PartitionStats
+         * is generated to store the mapping relationship between segments and clustering key values. Once receiving
+         * a search/query request that carries a clustering key value, it quickly finds out a search scope from
+         * the PartitionStats which significantly improving search performance.
+         * Only scalar fields can be clustering key.
+         * Only one culstering key is allowed in one collection.
+         *
+         * @param clusteringKey true is clustering key, false is not
+         * @return <code>Builder</code>
+         */
+        public Builder withClusteringKey(boolean clusteringKey) {
+            this.clusteringKey = clusteringKey;
+            return this;
+        }
+
         /**
         /**
          * Verifies parameters and creates a new {@link FieldType} instance.
          * Verifies parameters and creates a new {@link FieldType} instance.
          *
          *

+ 2 - 0
src/main/java/io/milvus/v2/service/collection/request/AddFieldReq.java

@@ -38,6 +38,8 @@ public class AddFieldReq {
     @Builder.Default
     @Builder.Default
     private Boolean isPartitionKey = Boolean.FALSE;
     private Boolean isPartitionKey = Boolean.FALSE;
     @Builder.Default
     @Builder.Default
+    private Boolean isClusteringKey = Boolean.FALSE;
+    @Builder.Default
     private Boolean autoID = Boolean.FALSE;
     private Boolean autoID = Boolean.FALSE;
     private Integer dimension;
     private Integer dimension;
     private io.milvus.v2.common.DataType elementType;
     private io.milvus.v2.common.DataType elementType;

+ 3 - 0
src/main/java/io/milvus/v2/service/collection/request/CreateCollectionReq.java

@@ -98,6 +98,7 @@ public class CreateCollectionReq {
                     .description(addFieldReq.getDescription())
                     .description(addFieldReq.getDescription())
                     .isPrimaryKey(addFieldReq.getIsPrimaryKey())
                     .isPrimaryKey(addFieldReq.getIsPrimaryKey())
                     .isPartitionKey(addFieldReq.getIsPartitionKey())
                     .isPartitionKey(addFieldReq.getIsPartitionKey())
+                    .isClusteringKey(addFieldReq.getIsClusteringKey())
                     .autoID(addFieldReq.getAutoID())
                     .autoID(addFieldReq.getAutoID())
                     .build();
                     .build();
             if (addFieldReq.getDataType().equals(DataType.Array)) {
             if (addFieldReq.getDataType().equals(DataType.Array)) {
@@ -144,6 +145,8 @@ public class CreateCollectionReq {
         @Builder.Default
         @Builder.Default
         private Boolean isPartitionKey = Boolean.FALSE;
         private Boolean isPartitionKey = Boolean.FALSE;
         @Builder.Default
         @Builder.Default
+        private Boolean isClusteringKey = Boolean.FALSE;
+        @Builder.Default
         private Boolean autoID = Boolean.FALSE;
         private Boolean autoID = Boolean.FALSE;
         private DataType elementType;
         private DataType elementType;
         private Integer maxCapacity;
         private Integer maxCapacity;

+ 3 - 0
src/main/java/io/milvus/v2/utils/SchemaUtils.java

@@ -36,6 +36,7 @@ public class SchemaUtils {
                 .setDataType(DataType.valueOf(fieldSchema.getDataType().name()))
                 .setDataType(DataType.valueOf(fieldSchema.getDataType().name()))
                 .setIsPrimaryKey(fieldSchema.getIsPrimaryKey())
                 .setIsPrimaryKey(fieldSchema.getIsPrimaryKey())
                 .setIsPartitionKey(fieldSchema.getIsPartitionKey())
                 .setIsPartitionKey(fieldSchema.getIsPartitionKey())
+                .setIsClusteringKey(fieldSchema.getIsClusteringKey())
                 .setAutoID(fieldSchema.getAutoID())
                 .setAutoID(fieldSchema.getAutoID())
                 .build();
                 .build();
         if(fieldSchema.getDimension() != null){
         if(fieldSchema.getDimension() != null){
@@ -73,6 +74,8 @@ public class SchemaUtils {
                 .name(fieldSchema.getName())
                 .name(fieldSchema.getName())
                 .dataType(io.milvus.v2.common.DataType.valueOf(fieldSchema.getDataType().name()))
                 .dataType(io.milvus.v2.common.DataType.valueOf(fieldSchema.getDataType().name()))
                 .isPrimaryKey(fieldSchema.getIsPrimaryKey())
                 .isPrimaryKey(fieldSchema.getIsPrimaryKey())
+                .isPartitionKey(fieldSchema.getIsPartitionKey())
+                .isClusteringKey(fieldSchema.getIsClusteringKey())
                 .autoID(fieldSchema.getAutoID())
                 .autoID(fieldSchema.getAutoID())
                 .elementType(io.milvus.v2.common.DataType.valueOf(fieldSchema.getElementType().name()))
                 .elementType(io.milvus.v2.common.DataType.valueOf(fieldSchema.getElementType().name()))
                 .build();
                 .build();