Browse Source

Support AlterCollectionField (#1251)

Signed-off-by: yhmo <yihua.mo@zilliz.com>
groot 4 months ago
parent
commit
25ec12e045

+ 17 - 9
sdk-core/src/main/java/io/milvus/v2/client/MilvusClientV2.java

@@ -307,7 +307,7 @@ public class MilvusClientV2 {
     }
     /**
      * Alter database with key value pair. (Available from Milvus v2.4.4)
-     * Deprecated, replaced by alterDatabaseProperties from v2.5.3, to keep consistence with other SDKs
+     * Deprecated, replaced by alterDatabaseProperties from SDK v2.5.3, to keep consistence with other SDKs
      * @param request alter database request
      */
     @Deprecated
@@ -318,14 +318,14 @@ public class MilvusClientV2 {
                 .build());
     }
     /**
-     * Alter a database's properties (Available from Milvus v2.5.3)
+     * Alter a database's properties.
      * @param request alter database properties request
      */
     public void alterDatabaseProperties(AlterDatabasePropertiesReq request) {
         retry(()-> databaseService.alterDatabaseProperties(this.getRpcStub(), request));
     }
     /**
-     * drop a database's properties (Available from Milvus v2.5.3)
+     * drop a database's properties.
      * @param request alter database properties request
      */
     public void dropDatabaseProperties(DropDatabasePropertiesReq request) {
@@ -375,7 +375,7 @@ public class MilvusClientV2 {
     }
     /**
      * Alter a collection in Milvus.
-     * Deprecated, replaced by alterCollectionProperties from v2.5.3, to keep consistence with other SDKs
+     * Deprecated, replaced by alterCollectionProperties from SDK v2.5.3, to keep consistence with other SDKs
      *
      * @param request alter collection request
      */
@@ -388,7 +388,7 @@ public class MilvusClientV2 {
                 .build());
     }
     /**
-     * Alter a collection's properties (Available from Milvus v2.5.3).
+     * Alter a collection's properties.
      *
      * @param request alter collection properties request
      */
@@ -396,7 +396,15 @@ public class MilvusClientV2 {
         retry(()-> collectionService.alterCollectionProperties(this.getRpcStub(), request));
     }
     /**
-     * drop a collection's properties (Available from Milvus v2.5.3)
+     * Alter a field's properties.
+     *
+     * @param request alter field properties request
+     */
+    public void alterCollectionField(AlterCollectionFieldReq request) {
+        retry(()-> collectionService.alterCollectionField(this.getRpcStub(), request));
+    }
+    /**
+     * drop a collection's properties.
      * @param request drop collection properties request
      */
     public void dropCollectionProperties(DropCollectionPropertiesReq request) {
@@ -492,7 +500,7 @@ public class MilvusClientV2 {
     }
     /**
      * Alter an index in Milvus.
-     * Deprecated, replaced by alterIndexProperties from v2.5.3, to keep consistence with other SDKs
+     * Deprecated, replaced by alterIndexProperties from SDK v2.5.3, to keep consistence with other SDKs
      *
      * @param request alter index request
      */
@@ -506,7 +514,7 @@ public class MilvusClientV2 {
                 .build());
     }
     /**
-     * Alter an index's properties (Available from Milvus v2.5.3)
+     * Alter an index's properties.
      *
      * @param request alter index request
      */
@@ -514,7 +522,7 @@ public class MilvusClientV2 {
         retry(()->indexService.alterIndexProperties(this.getRpcStub(), request));
     }
     /**
-     * drop an index's properties (Available from Milvus v2.5.3)
+     * drop an index's properties.
      * @param request drop index properties request
      */
     public void dropIndexProperties(DropIndexPropertiesReq request) {

+ 19 - 0
sdk-core/src/main/java/io/milvus/v2/service/collection/CollectionService.java

@@ -204,6 +204,25 @@ public class CollectionService extends BaseService {
         return null;
     }
 
+    public Void alterCollectionField(MilvusServiceGrpc.MilvusServiceBlockingStub blockingStub, AlterCollectionFieldReq request) {
+        String title = String.format("AlterCollectionFieldReq collectionName:%s", request.getCollectionName());
+        AlterCollectionFieldRequest.Builder builder = AlterCollectionFieldRequest.newBuilder()
+                .setCollectionName(request.getCollectionName())
+                .setFieldName(request.getFieldName());
+        List<KeyValuePair> propertiesList = ParamUtils.AssembleKvPair(request.getProperties());
+        if (CollectionUtils.isNotEmpty(propertiesList)) {
+            propertiesList.forEach(builder::addProperties);
+        }
+        if (StringUtils.isNotEmpty(request.getDatabaseName())) {
+            builder.setDbName(request.getDatabaseName());
+        }
+
+        Status response = blockingStub.alterCollectionField(builder.build());
+        rpcUtils.handleResponse(title, response);
+
+        return null;
+    }
+
     public Void dropCollectionProperties(MilvusServiceGrpc.MilvusServiceBlockingStub blockingStub, DropCollectionPropertiesReq request) {
         String title = String.format("DropCollectionPropertiesReq collectionName:%s", request.getCollectionName());
         AlterCollectionRequest.Builder builder = AlterCollectionRequest.newBuilder()

+ 48 - 0
sdk-core/src/main/java/io/milvus/v2/service/collection/request/AlterCollectionFieldReq.java

@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package io.milvus.v2.service.collection.request;
+
+import lombok.Builder;
+import lombok.Data;
+import lombok.experimental.SuperBuilder;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Data
+@SuperBuilder
+public class AlterCollectionFieldReq {
+    private String collectionName;
+    private String fieldName;
+    private String databaseName;
+    @Builder.Default
+    private final Map<String, String> properties = new HashMap<>();
+
+    public static abstract class AlterCollectionFieldReqBuilder<C extends AlterCollectionFieldReq, B extends AlterCollectionFieldReq.AlterCollectionFieldReqBuilder<C, B>> {
+        public B property(String key, String value) {
+            if(null == this.properties$value ){
+                this.properties$value = new HashMap<>();
+            }
+            this.properties$value.put(key, value);
+            this.properties$set = true;
+            return self();
+        }
+    }
+}

+ 2 - 1
sdk-core/src/main/java/io/milvus/v2/service/vector/VectorService.java

@@ -34,6 +34,7 @@ import io.milvus.v2.service.index.IndexService;
 import io.milvus.v2.service.vector.request.*;
 import io.milvus.v2.service.vector.response.*;
 import io.milvus.v2.utils.DataUtils;
+import io.milvus.v2.utils.VectorUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -237,7 +238,7 @@ public class VectorService extends BaseService {
         if (request.getFilter() != null && !request.getFilter().isEmpty()) {
             Map<String, Object> filterTemplateValues = request.getFilterTemplateValues();
             filterTemplateValues.forEach((key, value)->{
-                builder.putExprTemplateValues(key, vectorUtils.deduceAndCreateTemplateValue(value));
+                builder.putExprTemplateValues(key, VectorUtils.deduceAndCreateTemplateValue(value));
             });
         }
         MutationResult response = blockingStub.delete(builder.build());

+ 15 - 0
sdk-core/src/test/java/io/milvus/v2/client/MilvusClientV2DockerTest.java

@@ -1093,6 +1093,11 @@ class MilvusClientV2DockerTest {
                 .dataType(DataType.FloatVector)
                 .dimension(DIMENSION)
                 .build());
+        collectionSchema.addField(AddFieldReq.builder()
+                .fieldName("name")
+                .dataType(DataType.VarChar)
+                .maxLength(100)
+                .build());
 
         List<IndexParam> indexes = new ArrayList<>();
         Map<String,Object> extra = new HashMap<>();
@@ -1125,6 +1130,13 @@ class MilvusClientV2DockerTest {
                 .collectionName(randomCollectionName)
                 .build());
 
+        // alter field properties
+        client.alterCollectionField(AlterCollectionFieldReq.builder()
+                .collectionName(randomCollectionName)
+                .fieldName("name")
+                .property("max_length", "9")
+                .build());
+
         // collection alter properties
         Map<String, String> properties = new HashMap<>();
         properties.put(Constant.TTL_SECONDS, "10");
@@ -1145,6 +1157,9 @@ class MilvusClientV2DockerTest {
         Assertions.assertEquals("true", collProps.get(Constant.MMAP_ENABLED));
         Assertions.assertEquals("val", collProps.get("prop"));
 
+        CreateCollectionReq.FieldSchema fieldScheam = descCollResp.getCollectionSchema().getField("name");
+        Assertions.assertEquals(9, fieldScheam.getMaxLength());
+
         client.dropCollectionProperties(DropCollectionPropertiesReq.builder()
                 .collectionName(randomCollectionName)
                 .propertyKeys(Collections.singletonList("prop"))