Browse Source

Support without specifying metricType and indexType (#741)

Signed-off-by: lentitude2tk <xushuang.hu@zilliz.com>
xushuang.hu 1 năm trước cách đây
mục cha
commit
5af81f8fb6

+ 0 - 36
src/main/java/io/milvus/client/AbstractMilvusGrpcClient.java

@@ -1151,42 +1151,6 @@ public abstract class AbstractMilvusGrpcClient implements MilvusClient {
                 requestParam.getCollectionName(), requestParam.getFieldName());
 
         try {
-            // get collection schema to check input
-            DescribeCollectionParam.Builder descBuilder = DescribeCollectionParam.newBuilder()
-                    .withDatabaseName(requestParam.getDatabaseName())
-                    .withCollectionName(requestParam.getCollectionName());
-            R<DescribeCollectionResponse> descResp = describeCollection(descBuilder.build());
-            if (descResp.getStatus() != R.Status.Success.getCode()) {
-                return R.failed(descResp.getException());
-            }
-
-            DescCollResponseWrapper wrapper = new DescCollResponseWrapper(descResp.getData());
-            List<FieldType> fields = wrapper.getFields();
-            // check field existence and index_type/field_type must be matched
-            boolean fieldExists = false;
-            boolean validType = false;
-            for (FieldType field : fields) {
-                if (requestParam.getFieldName().equals(field.getName())) {
-                    fieldExists = true;
-                    if (ParamUtils.VerifyIndexType(requestParam.getIndexType(), field.getDataType())) {
-                        validType = true;
-                    }
-                    break;
-                }
-            }
-
-            if (!fieldExists) {
-                String msg = String.format("Field '%s' doesn't exist in the collection", requestParam.getFieldName());
-                logError("CreateIndexRequest failed! {}\n", msg);
-                return R.failed(R.Status.IllegalArgument, msg);
-            }
-            if (!validType) {
-                String msg = String.format("Index type '%s' doesn't match with data type of field '%s'",
-                        requestParam.getIndexType().name(), requestParam.getFieldName());
-                logError("CreateIndexRequest failed! {}\n", msg);
-                return R.failed(R.Status.IllegalArgument, msg);
-            }
-
             // prepare index parameters
             CreateIndexRequest.Builder createIndexRequestBuilder = CreateIndexRequest.newBuilder();
             List<KeyValuePair> extraParamList = ParamUtils.AssembleKvPair(requestParam.getExtraParam());

+ 1 - 1
src/main/java/io/milvus/param/IndexType.java

@@ -26,7 +26,7 @@ import lombok.Getter;
  * For more information: @see <a href="https://milvus.io/docs/v2.0.0/index_selection.md">Index Types</a>
  */
 public enum IndexType {
-    INVALID,
+    None(0),
     //Only supported for float vectors
     FLAT(1),
     IVF_FLAT(2),

+ 1 - 0
src/main/java/io/milvus/param/MetricType.java

@@ -24,6 +24,7 @@ package io.milvus.param;
  * For more information: @see <a href="https://milvus.io/docs/v2.0.0/metric.md">Similarity Metrics</a>
  */
 public enum MetricType {
+    None,
     INVALID,
     // Only for float vectors
     L2,

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

@@ -215,33 +215,6 @@ public class ParamUtils {
         return metric != MetricType.INVALID && !IsFloatMetric(metric);
     }
 
-    /**
-     * Checks if an index type is for vector field.
-     *
-     * @param idx index type
-     */
-    public static boolean IsVectorIndex(IndexType idx) {
-        return idx != IndexType.INVALID && idx.getCode() < IndexType.TRIE.getCode();
-    }
-
-    /**
-     * Checks if an index type is matched with data type.
-     *
-     * @param indexType index type
-     * @param dataType data type
-     */
-    public static boolean VerifyIndexType(IndexType indexType, DataType dataType) {
-        if (dataType == DataType.FloatVector) {
-            return (IsVectorIndex(indexType) && (indexType != IndexType.BIN_FLAT) && (indexType != IndexType.BIN_IVF_FLAT));
-        } else if (dataType == DataType.BinaryVector) {
-            return indexType == IndexType.BIN_FLAT || indexType == IndexType.BIN_IVF_FLAT;
-        } else if (dataType == DataType.VarChar) {
-            return indexType == IndexType.TRIE;
-        } else {
-            return indexType == IndexType.STL_SORT;
-        }
-    }
-
     public static class InsertBuilderWrapper {
         private InsertRequest.Builder insertBuilder;
         private UpsertRequest.Builder upsertBuilder;

+ 4 - 14
src/main/java/io/milvus/param/index/CreateIndexParam.java

@@ -55,10 +55,10 @@ public class CreateIndexParam {
         this.fieldName = builder.fieldName;
         this.indexName = builder.indexName;
         this.indexType = builder.indexType;
-        if (builder.indexType != IndexType.INVALID) {
+        if (builder.indexType != IndexType.None) {
             this.extraParam.put(Constant.INDEX_TYPE, builder.indexType.getName());
         }
-        if (builder.metricType != MetricType.INVALID) {
+        if (builder.metricType != MetricType.None) {
             this.extraParam.put(Constant.METRIC_TYPE, builder.metricType.name());
         }
         if (builder.extraParam != null) {
@@ -80,9 +80,9 @@ public class CreateIndexParam {
         private String databaseName;
         private String collectionName;
         private String fieldName;
-        private IndexType indexType = IndexType.INVALID;
+        private IndexType indexType = IndexType.None;
         private String indexName = Constant.DEFAULT_INDEX_NAME;
-        private MetricType metricType = MetricType.INVALID;
+        private MetricType metricType = MetricType.None;
         private String extraParam;
 
         // syncMode:
@@ -237,16 +237,6 @@ public class CreateIndexParam {
                 indexName = Constant.DEFAULT_INDEX_NAME;
             }
 
-            if (indexType == IndexType.INVALID) {
-                throw new ParamException("Index type is required");
-            }
-
-            if (ParamUtils.IsVectorIndex(indexType)) {
-                if (metricType == MetricType.INVALID) {
-                    throw new ParamException("Metric type is required");
-                }
-            }
-
             if (Objects.equals(syncMode, Boolean.TRUE)) {
                 if (syncWaitingInterval <= 0) {
                     throw new ParamException("Sync index waiting interval must be larger than zero");

+ 1 - 1
src/main/java/io/milvus/response/DescIndexResponseWrapper.java

@@ -84,7 +84,7 @@ public class DescIndexResponseWrapper {
                 return IndexType.valueOf(params.get(Constant.INDEX_TYPE).toUpperCase(Locale.ROOT));
             }
 
-            return IndexType.INVALID;
+            return IndexType.None;
         }
 
         public MetricType getMetricType() {

+ 0 - 18
src/test/java/io/milvus/client/MilvusServiceClientTest.java

@@ -1274,24 +1274,6 @@ class MilvusServiceClientTest {
                 .build()
         );
 
-        assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
-                .withCollectionName("collection1")
-                .withFieldName("field1")
-                .withIndexType(IndexType.INVALID)
-                .withMetricType(MetricType.L2)
-                .withExtraParam("dummy")
-                .build()
-        );
-
-        assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
-                .withCollectionName("collection1")
-                .withFieldName("field1")
-                .withIndexType(IndexType.IVF_FLAT)
-                .withMetricType(MetricType.INVALID)
-                .withExtraParam("dummy")
-                .build()
-        );
-
         assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
                 .withCollectionName("collection1")
                 .withFieldName("field1")