Repository: milvus-io/milvus-sdk-go
Pull request #794 (2024-07-25 09:21:56)

enhance: Support specifying field schema to be cluster key
See also milvus-io/milvus#32939

sre-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/milvus-io/milvus-sdk-go/pull/794#" title="Author self-approved">congqixia</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=milvus-io%2Fmilvus-sdk-go).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/milvus-io/milvus-sdk-go/blob/master/OWNERS)~~ [congqixia]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
congqixia: /kind enhancement

Patch:
diff --git a/entity/schema.go b/entity/schema.go
index 03086bf..9bb4754 100644
--- a/entity/schema.go
+++ b/entity/schema.go
@@ -141,33 +141,35 @@ func (s *Schema) PKField() *Field {
 
 // Field represent field schema in milvus
 type Field struct {
-	ID             int64  // field id, generated when collection is created, input value is ignored
-	Name           string // field name
-	PrimaryKey     bool   // is primary key
-	AutoID         bool   // is auto id
-	Description    string
-	DataType       FieldType
-	TypeParams     map[string]string
-	IndexParams    map[string]string
-	IsDynamic      bool
-	IsPartitionKey bool
-	ElementType    FieldType
+	ID              int64  // field id, generated when collection is created, input value is ignored
+	Name            string // field name
+	PrimaryKey      bool   // is primary key
+	AutoID          bool   // is auto id
+	Description     string
+	DataType        FieldType
+	TypeParams      map[string]string
+	IndexParams     map[string]string
+	IsDynamic       bool
+	IsPartitionKey  bool
+	IsClusteringKey bool
+	ElementType     FieldType
 }
 
 // ProtoMessage generates corresponding FieldSchema
 func (f *Field) ProtoMessage() *schema.FieldSchema {
 	return &schema.FieldSchema{
-		FieldID:        f.ID,
-		Name:           f.Name,
-		Description:    f.Description,
-		IsPrimaryKey:   f.PrimaryKey,
-		AutoID:         f.AutoID,
-		DataType:       schema.DataType(f.DataType),
-		TypeParams:     MapKvPairs(f.TypeParams),
-		IndexParams:    MapKvPairs(f.IndexParams),
-		IsDynamic:      f.IsDynamic,
-		IsPartitionKey: f.IsPartitionKey,
-		ElementType:    schema.DataType(f.ElementType),
+		FieldID:         f.ID,
+		Name:            f.Name,
+		Description:     f.Description,
+		IsPrimaryKey:    f.PrimaryKey,
+		AutoID:          f.AutoID,
+		DataType:        schema.DataType(f.DataType),
+		TypeParams:      MapKvPairs(f.TypeParams),
+		IndexParams:     MapKvPairs(f.IndexParams),
+		IsDynamic:       f.IsDynamic,
+		IsPartitionKey:  f.IsPartitionKey,
+		IsClusteringKey: f.IsClusteringKey,
+		ElementType:     schema.DataType(f.ElementType),
 	}
 }
 
@@ -214,6 +216,11 @@ func (f *Field) WithIsPartitionKey(isPartitionKey bool) *Field {
 	return f
 }
 
+func (f *Field) WithIsClusteringKey(isClusteringKey bool) *Field {
+	f.IsClusteringKey = isClusteringKey
+	return f
+}
+
 /*
 func (f *Field) WithDefaultValueBool(defaultValue bool) *Field {
 	f.DefaultValue = &schema.ValueField{
@@ -318,6 +325,7 @@ func (f *Field) ReadProto(p *schema.FieldSchema) *Field {
 	f.IndexParams = KvPairsMap(p.GetIndexParams())
 	f.IsDynamic = p.GetIsDynamic()
 	f.IsPartitionKey = p.GetIsPartitionKey()
+	f.IsClusteringKey = p.GetIsClusteringKey()
 	f.ElementType = FieldType(p.GetElementType())
 
 	return f


Test patch:
diff --git a/entity/schema_test.go b/entity/schema_test.go
index 811a46b..3a67643 100644
--- a/entity/schema_test.go
+++ b/entity/schema_test.go
@@ -28,6 +28,7 @@ func TestFieldSchema(t *testing.T) {
 		NewField().WithName("array_field").WithDataType(FieldTypeArray).WithElementType(FieldTypeBool).WithMaxCapacity(128),
 		NewField().WithName("fp16_field").WithDataType(FieldTypeFloat16Vector).WithDim(128),
 		NewField().WithName("bf16_field").WithDataType(FieldTypeBFloat16Vector).WithDim(128),
+		NewField().WithName("cluster_key").WithDataType(FieldTypeInt32).WithIsClusteringKey(true),
 		/*
 			NewField().WithName("default_value_bool").WithDataType(FieldTypeBool).WithDefaultValueBool(true),
 			NewField().WithName("default_value_int").WithDataType(FieldTypeInt32).WithDefaultValueInt(1),
@@ -45,6 +46,7 @@ func TestFieldSchema(t *testing.T) {
 		assert.Equal(t, field.AutoID, fieldSchema.GetAutoID())
 		assert.Equal(t, field.PrimaryKey, fieldSchema.GetIsPrimaryKey())
 		assert.Equal(t, field.IsPartitionKey, fieldSchema.GetIsPartitionKey())
+		assert.Equal(t, field.IsClusteringKey, fieldSchema.GetIsClusteringKey())
 		assert.Equal(t, field.IsDynamic, fieldSchema.GetIsDynamic())
 		assert.Equal(t, field.Description, fieldSchema.GetDescription())
 		assert.Equal(t, field.TypeParams, KvPairsMap(fieldSchema.GetTypeParams()))
@@ -60,6 +62,7 @@ func TestFieldSchema(t *testing.T) {
 		assert.Equal(t, field.Description, nf.Description)
 		assert.Equal(t, field.IsDynamic, nf.IsDynamic)
 		assert.Equal(t, field.IsPartitionKey, nf.IsPartitionKey)
+		assert.Equal(t, field.IsClusteringKey, nf.IsClusteringKey)
 		assert.EqualValues(t, field.TypeParams, nf.TypeParams)
 		assert.EqualValues(t, field.ElementType, nf.ElementType)
 	}