Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions examples/simple_async2.py
Original file line number Diff line number Diff line change
@@ -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()}")
2 changes: 1 addition & 1 deletion pymilvus/client/async_grpc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Loading