From 1d6f52d8627822ba9253dc0a1c013b9306e45326 Mon Sep 17 00:00:00 2001 From: nameczz Date: Thu, 18 Jan 2024 18:21:05 +0800 Subject: [PATCH 1/2] support array for cli Signed-off-by: nameczz --- milvus_cli/Collection.py | 32 ++++++++++++++++++++++++---- milvus_cli/Types.py | 2 ++ milvus_cli/Validation.py | 8 +++---- milvus_cli/scripts/collection_cli.py | 2 +- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/milvus_cli/Collection.py b/milvus_cli/Collection.py index 33d8f57..ab535ba 100644 --- a/milvus_cli/Collection.py +++ b/milvus_cli/Collection.py @@ -36,14 +36,14 @@ def create_collection( ): fieldList = [] for field in fields: - [fieldName, fieldType, fieldData] = field.split(":") + [fieldName, fieldType, *restData] = field.split(":") upperFieldType = fieldType.upper() if upperFieldType in ["BINARY_VECTOR", "FLOAT_VECTOR"]: fieldList.append( FieldSchema( name=fieldName, dtype=DataType[upperFieldType], - dim=int(fieldData), + dim=int(restData[0]), ) ) elif upperFieldType == "VARCHAR": @@ -51,15 +51,29 @@ def create_collection( FieldSchema( name=fieldName, dtype=DataType[upperFieldType], - max_length=fieldData, + max_length=restData[0], ) ) + elif upperFieldType == "ARRAY": + upperElementType = restData[1].upper() + max_capacity = restData[0] + maxLength = restData[2] if len(restData) == 3 else None + fieldList.append( + FieldSchema( + name=fieldName, + dtype=DataType[upperFieldType], + element_type=DataType[upperElementType], + max_capacity=max_capacity, + max_length=maxLength, + ) + ) + else: fieldList.append( FieldSchema( name=fieldName, dtype=DataType[upperFieldType], - description=fieldData, + description=restData[0], ) ) schema = CollectionSchema( @@ -147,11 +161,21 @@ def get_collection_details(self, collectionName="", collection=None): fieldSchemaDetails = "" for fieldSchema in schema.fields: _name = f"{'*' if fieldSchema.is_primary else ''}{fieldSchema.name}" + _type = DataTypeByNum[fieldSchema.dtype] _desc = fieldSchema.description _params = fieldSchema.params _dim = _params.get("dim") _params_desc = f"dim: {_dim}" if _dim else "" + if fieldSchema.dtype == DataType.ARRAY: + _max_length = _params.get("max_length") + _element_type = fieldSchema.element_type + _max_capacity = _params.get("max_capacity") + _params_desc = ( + f"max_capacity: {_max_capacity},element_type: {_element_type}" + ) + _params_desc += f",max_length: {_max_length}" if _max_length else "" + fieldSchemaDetails += f"\n - {_name} {_type} {_params_desc} {_desc}" schemaDetails = """Description: {}\n\nAuto ID: {}\n\nFields(* is the primary field):{}""".format( schema.description, schema.auto_id, fieldSchemaDetails diff --git a/milvus_cli/Types.py b/milvus_cli/Types.py index dbc2d7e..4e9bf65 100644 --- a/milvus_cli/Types.py +++ b/milvus_cli/Types.py @@ -33,6 +33,7 @@ def __str__(self): "BINARY_VECTOR", "FLOAT_VECTOR", "JSON", + "ARRAY", ] IndexTypes = [ @@ -144,6 +145,7 @@ def __str__(self): 11: "DOUBLE", 20: "STRING", 21: "VARCHAR", + 22: "ARRAY", 23: "JSON", 100: "BINARY_VECTOR", 101: "FLOAT_VECTOR", diff --git a/milvus_cli/Validation.py b/milvus_cli/Validation.py index 5e1fa87..c08e30a 100644 --- a/milvus_cli/Validation.py +++ b/milvus_cli/Validation.py @@ -27,11 +27,11 @@ def validateCollectionParameter(collectionName, primaryField, fields): fieldNames = [] for field in fields: fieldList = field.split(":") - if not (len(fieldList) == 3): + if len(fieldList) < 3: raise ParameterException( - 'Field should contain three parameters concatenated by ":".' + 'Field should contain three parameters(array field need more) concatenated by ":".' ) - [fieldName, fieldType, fieldData] = fieldList + [fieldName, fieldType, *restData] = fieldList upperFieldType = fieldType.upper() fieldNames.append(fieldName) if upperFieldType not in FiledDataTypes: @@ -42,7 +42,7 @@ def validateCollectionParameter(collectionName, primaryField, fields): ) if upperFieldType in ["BINARY_VECTOR", "FLOAT_VECTOR"]: try: - int(fieldData) + int(restData[0]) except ValueError as e: raise ParameterException("""Vector's dim should be int.""") # Dedup field name. diff --git a/milvus_cli/scripts/collection_cli.py b/milvus_cli/scripts/collection_cli.py index 437022c..db5f1b9 100644 --- a/milvus_cli/scripts/collection_cli.py +++ b/milvus_cli/scripts/collection_cli.py @@ -85,7 +85,7 @@ def create_collection( Example: - create collection -c car -f id:INT64:primary_field -f vector:FLOAT_VECTOR:128 -f color:INT64:color -f brand:INT64:brand -p id -A -d 'car_collection' + create collection -c array -f id:INT64:primary_field -f vector:FLOAT_VECTOR:128 -f color:INT64:color -f brand:ARRAY:64:VARCHAR:128 -p id -A -d 'car_collection' """ try: validateCollectionParameter( From 54048caf79bd7cca513af2224831fd6fe7fe566c Mon Sep 17 00:00:00 2001 From: nameczz Date: Thu, 18 Jan 2024 18:22:18 +0800 Subject: [PATCH 2/2] update readme Signed-off-by: nameczz --- README.md | 2 +- milvus_cli/scripts/collection_cli.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2a2c986..d183e23 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Python >= 3.8.5 #### Install from PyPI (Recommended) Run `pip install pymilvus==2.3.4` -Run `pip install milvus-cli==0.4.1` +Run `pip install milvus-cli==0.4.2` #### Install from a tarball diff --git a/milvus_cli/scripts/collection_cli.py b/milvus_cli/scripts/collection_cli.py index db5f1b9..1ed1de6 100644 --- a/milvus_cli/scripts/collection_cli.py +++ b/milvus_cli/scripts/collection_cli.py @@ -56,7 +56,7 @@ "-f", "--schema-field", "fields", - help='[Multiple] - FieldSchema. Usage is "::"', + help='[Multiple] - FieldSchema. Usage is "::", Array Type is :::(:if Varchar)', default=None, multiple=True, ) @@ -85,7 +85,7 @@ def create_collection( Example: - create collection -c array -f id:INT64:primary_field -f vector:FLOAT_VECTOR:128 -f color:INT64:color -f brand:ARRAY:64:VARCHAR:128 -p id -A -d 'car_collection' + create collection -c car -f id:INT64:primary_field -f vector:FLOAT_VECTOR:128 -f color:INT64:color -f brand:ARRAY:64:VARCHAR:128 -p id -A -d 'car_collection' """ try: validateCollectionParameter(