From fde81bff072388f1118d9ce4e616c671a6545933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=B8=85=27=CF=89=27=E0=B8=85?= Date: Tue, 31 Dec 2024 11:02:46 +0800 Subject: [PATCH] enhance: add example code for create_collection, drop_collection, load_collection, release_collection, create_index, drop_index, create_partition, drop_partition, load_partition and release_partition (#2533) Signed-off-by: Ruichen Bao --- examples/simple_async2.py | 75 +++++++++++++++++++++++++++ pymilvus/client/async_grpc_handler.py | 2 +- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 examples/simple_async2.py diff --git a/examples/simple_async2.py b/examples/simple_async2.py new file mode 100644 index 000000000..5db4fd7db --- /dev/null +++ b/examples/simple_async2.py @@ -0,0 +1,75 @@ +from pymilvus import ( + DataType, + MilvusClient, + AsyncMilvusClient, +) +import numpy as np +import asyncio + +num_entities, dim = 10000, 128 +nq, default_limit = 2, 3 +collection_name = "hello_milvus" +partition_name = "p1" +index_field_name = "vector" +rng = np.random.default_rng(seed=19530) +output_fields = ["id"] +uri = "http://localhost:19530" + +milvus_client = MilvusClient(uri=uri) +async_milvus_client = AsyncMilvusClient(uri=uri) + +loop = asyncio.get_event_loop() + +# create collection, partition, index +print("Start dropping all collection") +for c in milvus_client.list_collections(): + loop.run_until_complete(async_milvus_client.drop_collection(c)) +print("Dropping collection done") +print("Start creating collection") +schema = async_milvus_client.create_schema(auto_id=False) +schema.add_field("id", DataType.INT64, is_primary=True) +schema.add_field("vector", DataType.FLOAT_VECTOR, dim=dim) +loop.run_until_complete(async_milvus_client.create_collection(collection_name, schema=schema, consistency_level="Strong")) +print("Creating collection done") +print("Start creating partition") +loop.run_until_complete(async_milvus_client.create_partition(collection_name, partition_name=partition_name)) +print("Creating partition done") +print("Start creating index") +index_params = milvus_client.prepare_index_params() +index_params.add_index(field_name=index_field_name, index_type="HNSW", metric_type="COSINE", M=30, efConstruction=200) +loop.run_until_complete(async_milvus_client.create_index(collection_name, index_params)) +print("Creating index done") + +print(f"all collections: {milvus_client.list_collections()}") +print(f"schema of collection {collection_name}:", milvus_client.describe_collection(collection_name)) +print(f"load state of collection {collection_name}:", milvus_client.get_load_state(collection_name, "")) +print(f"has partition p1:", milvus_client.has_partition(collection_name, partition_name)) +print(f"load state of partition p1:", milvus_client.get_load_state(collection_name, partition_name)) +print(f"describe index {index_field_name}:", milvus_client.describe_index(collection_name, index_field_name)) + +# load collecton, partition +loop.run_until_complete(async_milvus_client.load_partitions(collection_name, partition_name)) +loop.run_until_complete(async_milvus_client.load_collection(collection_name)) + +print("Loading collecton, partition done") +print(f"load state of collection {collection_name}:", milvus_client.get_load_state(collection_name, "")) +print(f"load state of partition p1:", milvus_client.get_load_state(collection_name, partition_name)) + +# release collection, partition +loop.run_until_complete(async_milvus_client.release_partitions(collection_name, partition_name)) +loop.run_until_complete(async_milvus_client.release_collection(collection_name)) + +print("Releasing collecton, partition done") +print(f"load state of collection {collection_name}:", milvus_client.get_load_state(collection_name, "")) +print(f"load state of partition p1:", milvus_client.get_load_state(collection_name, partition_name)) + +# drop collection, partition, index +loop.run_until_complete(async_milvus_client.drop_partition(collection_name, partition_name)) +print("Dropping partition done") +print(f"has partition p1:", milvus_client.has_partition(collection_name, partition_name)) +loop.run_until_complete(async_milvus_client.drop_index(collection_name, index_field_name)) +print("Dropping index done") +print(f"describe index {index_field_name}:", milvus_client.describe_index(collection_name, index_field_name)) +loop.run_until_complete(async_milvus_client.drop_collection(collection_name)) +print("Dropping collection done") +print(f"all collections: {milvus_client.list_collections()}") diff --git a/pymilvus/client/async_grpc_handler.py b/pymilvus/client/async_grpc_handler.py index f9d8d3c1b..8ae9f29ae 100644 --- a/pymilvus/client/async_grpc_handler.py +++ b/pymilvus/client/async_grpc_handler.py @@ -801,13 +801,13 @@ async def load_partitions( timeout: Optional[float] = None, **kwargs, ): + await self.ensure_channel_ready() check_pass_param( collection_name=collection_name, partition_name_array=partition_names, replica_number=replica_number, timeout=timeout, ) - await self.ensure_channel_ready() refresh = kwargs.get("refresh", kwargs.get("_refresh", False)) resource_groups = kwargs.get("resource_groups", kwargs.get("_resource_groups")) load_fields = kwargs.get("load_fields", kwargs.get("_load_fields"))