Browse Source

Support clustering key (#1086)

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

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

@@ -1287,6 +1287,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())
@@ -1320,6 +1321,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;
     private final boolean nullable;
     private final boolean nullable;
@@ -58,6 +59,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;
         this.nullable = builder.nullable;
         this.nullable = builder.nullable;
@@ -103,6 +105,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
         private boolean nullable = false; // only for scalar fields(not include Array fields)
         private boolean nullable = false; // only for scalar fields(not include Array fields)
@@ -307,6 +310,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

@@ -105,6 +105,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())
                     .isNullable(addFieldReq.getIsNullable())
                     .isNullable(addFieldReq.getIsNullable())
                     .defaultValue(addFieldReq.getDefaultValue())
                     .defaultValue(addFieldReq.getDefaultValue())
@@ -153,6 +154,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;

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

@@ -37,6 +37,7 @@ public class SchemaUtils {
                 .setDataType(dType)
                 .setDataType(dType)
                 .setIsPrimaryKey(fieldSchema.getIsPrimaryKey())
                 .setIsPrimaryKey(fieldSchema.getIsPrimaryKey())
                 .setIsPartitionKey(fieldSchema.getIsPartitionKey())
                 .setIsPartitionKey(fieldSchema.getIsPartitionKey())
+                .setIsClusteringKey(fieldSchema.getIsClusteringKey())
                 .setAutoID(fieldSchema.getAutoID())
                 .setAutoID(fieldSchema.getAutoID())
                 .setNullable(fieldSchema.getIsNullable());
                 .setNullable(fieldSchema.getIsNullable());
         if (!ParamUtils.isVectorDataType(dType) && !fieldSchema.getIsPrimaryKey()) {
         if (!ParamUtils.isVectorDataType(dType) && !fieldSchema.getIsPrimaryKey()) {
@@ -89,6 +90,7 @@ public class SchemaUtils {
                 .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())
                 .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()))
                 .isNullable(fieldSchema.getNullable())
                 .isNullable(fieldSchema.getNullable())