Skip to content

Commit

Permalink
Refine interfaces for milvus_client (#1859)
Browse files Browse the repository at this point in the history
The main focus of this PR is to port some interfaces from ORM
and transform them from an object-oriented form to a functional form.
As these are new interfaces of MilvusClient, certain parameters and return values
have been modified compared to the original ORM interfaces.

Signed-off-by: zhenshan.cao <[email protected]>
  • Loading branch information
czs007 authored Jan 15, 2024
1 parent 5de9116 commit d7a402b
Show file tree
Hide file tree
Showing 18 changed files with 1,165 additions and 283 deletions.
63 changes: 0 additions & 63 deletions examples/hello_milvus_simple.py

This file was deleted.

47 changes: 0 additions & 47 deletions examples/hello_milvus_simple2.py

This file was deleted.

67 changes: 67 additions & 0 deletions examples/milvus_client/alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import time
import numpy as np
from pymilvus import (
MilvusClient,
)

fmt = "\n=== {:30} ===\n"
dim = 8
collection_name = "hello_milvus"
milvus_client = MilvusClient("http://localhost:19530")
milvus_client.drop_collection(collection_name)
milvus_client.create_collection(collection_name, dim, consistency_level="Strong", metric_type="L2", auto_id=True)

collection_name2 = "hello_milvus2"
milvus_client.drop_collection(collection_name2)
milvus_client.create_collection(collection_name2, dim, consistency_level="Strong", metric_type="L2", auto_id=True)


print("collections:", milvus_client.list_collections())

desc_c1 = milvus_client.describe_collection(collection_name)
print(f"{collection_name} :", desc_c1)

rng = np.random.default_rng(seed=19530)

rows = [
{"vector": rng.random((1, dim))[0], "a": 100},
{"vector": rng.random((1, dim))[0], "b": 200},
{"vector": rng.random((1, dim))[0], "c": 300},
]

print(fmt.format(f"Start inserting entities to {collection_name}"))
insert_result = milvus_client.insert(collection_name, rows)
print(insert_result)

rows = [
{"vector": rng.random((1, dim))[0], "d": 400},
{"vector": rng.random((1, dim))[0], "e": 500},
{"vector": rng.random((1, dim))[0], "f": 600},
]

print(fmt.format(f"Start inserting entities to {collection_name2}"))
insert_result2 = milvus_client.insert(collection_name2, rows)
print(insert_result2)

alias = "alias_hello_milvus"
milvus_client.create_alias(collection_name, alias)

assert milvus_client.describe_collection(alias) == milvus_client.describe_collection(collection_name)

milvus_client.alter_alias(collection_name2, alias)
assert milvus_client.describe_collection(alias) == milvus_client.describe_collection(collection_name2)

query_results = milvus_client.query(alias, filter= "f == 600")
print("results of query 'f == 600' is ")
for ret in query_results:
print(ret)


milvus_client.drop_alias(alias)
has_collection = milvus_client.has_collection(alias)
assert not has_collection
has_collection = milvus_client.has_collection(collection_name2)
assert has_collection

milvus_client.drop_collection(collection_name)
milvus_client.drop_collection(collection_name2)
70 changes: 70 additions & 0 deletions examples/milvus_client/customize_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import time
import numpy as np
from pymilvus import (
MilvusClient,
DataType
)

fmt = "\n=== {:30} ===\n"
dim = 8
collection_name = "hello_milvus"
milvus_client = MilvusClient("http://localhost:19530")

has_collection = milvus_client.has_collection(collection_name, timeout=5)
if has_collection:
milvus_client.drop_collection(collection_name)

schema = milvus_client.create_schema(enable_dynamic_field=True)
schema.add_field("id", DataType.INT64, is_primary=True)
schema.add_field("embeddings", DataType.FLOAT_VECTOR, dim=dim)
schema.add_field("title", DataType.VARCHAR, max_length=64)


index_params = milvus_client.prepare_index_params()
index_params.add_index(field_name = "embeddings", metric_type="L2")
milvus_client.create_collection(collection_name, schema=schema, index_params=index_params, consistency_level="Strong")

print(fmt.format(" all collections "))
print(milvus_client.list_collections())

print(fmt.format(f"schema of collection {collection_name}"))
print(milvus_client.describe_collection(collection_name))

rng = np.random.default_rng(seed=19530)
rows = [
{"id": 1, "embeddings": rng.random((1, dim))[0], "a": 100, "title": "t1"},
{"id": 2, "embeddings": rng.random((1, dim))[0], "b": 200, "title": "t2"},
{"id": 3, "embeddings": rng.random((1, dim))[0], "c": 300, "title": "t3"},
{"id": 4, "embeddings": rng.random((1, dim))[0], "d": 400, "title": "t4"},
{"id": 5, "embeddings": rng.random((1, dim))[0], "e": 500, "title": "t5"},
{"id": 6, "embeddings": rng.random((1, dim))[0], "f": 600, "title": "t6"},
]

print(fmt.format("Start inserting entities"))
insert_result = milvus_client.insert(collection_name, rows)
print(fmt.format("Inserting entities done"))
print(insert_result)


print(fmt.format("Start load collection "))
milvus_client.load_collection(collection_name)

print(fmt.format("Start query by specifying primary keys"))
query_results = milvus_client.query(collection_name, ids=[2])
print(query_results[0])

print(fmt.format("Start query by specifying filtering expression"))
query_results = milvus_client.query(collection_name, filter= "f == 600 or title == 't2'")
for ret in query_results:
print(ret)

rng = np.random.default_rng(seed=19530)
vectors_to_search = rng.random((1, dim))

print(fmt.format(f"Start search with retrieve serveral fields."))
result = milvus_client.search(collection_name, vectors_to_search, limit=3, output_fields=["pk", "a", "b"])
for hits in result:
for hit in hits:
print(f"hit: {hit}")

milvus_client.drop_collection(collection_name)
87 changes: 87 additions & 0 deletions examples/milvus_client/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import time
import numpy as np
from pymilvus import (
MilvusClient,
DataType
)

fmt = "\n=== {:30} ===\n"
dim = 8
collection_name = "hello_milvus"
milvus_client = MilvusClient("http://localhost:19530")

has_collection = milvus_client.has_collection(collection_name, timeout=5)
if has_collection:
milvus_client.drop_collection(collection_name)

schema = milvus_client.create_schema(enable_dynamic_field=True)
schema.add_field("id", DataType.INT64, is_primary=True)
schema.add_field("embeddings", DataType.FLOAT_VECTOR, dim=dim)
schema.add_field("title", DataType.VARCHAR, max_length=64)

# collection is not loaded after creation
milvus_client.create_collection(collection_name, schema=schema, consistency_level="Strong")

rng = np.random.default_rng(seed=19530)
rows = [
{"id": 1, "embeddings": rng.random((1, dim))[0], "a": 100, "title": "t1"},
{"id": 2, "embeddings": rng.random((1, dim))[0], "b": 200, "title": "t2"},
{"id": 3, "embeddings": rng.random((1, dim))[0], "c": 300, "title": "t3"},
{"id": 4, "embeddings": rng.random((1, dim))[0], "d": 400, "title": "t4"},
{"id": 5, "embeddings": rng.random((1, dim))[0], "e": 500, "title": "t5"},
{"id": 6, "embeddings": rng.random((1, dim))[0], "f": 600, "title": "t6"},
]

print(fmt.format("Start inserting entities"))
insert_result = milvus_client.insert(collection_name, rows)
print(fmt.format("Inserting entities done"))
print(insert_result)

index_params = milvus_client.prepare_index_params()
index_params.add_index(field_name = "embeddings", metric_type="L2")
index_params.add_index(field_name = "title", index_type = "TRIE", index_name="my_trie")

print(fmt.format("Start create index"))
milvus_client.create_index(collection_name, index_params)


index_names = milvus_client.list_indexes(collection_name)
print(f"index names for {collection_name}:", index_names)
for index_name in index_names:
index_info = milvus_client.describe_index(collection_name, index_name=index_name)
print(f"index info for index {index_name} is:", index_info)

print(fmt.format("Start load collection"))
milvus_client.load_collection(collection_name)

print(fmt.format("Start query by specifying primary keys"))
query_results = milvus_client.query(collection_name, ids=[2])
print(query_results[0])

print(fmt.format("Start query by specifying filtering expression"))
query_results = milvus_client.query(collection_name, filter= "f == 600 or title == 't2'")
for ret in query_results:
print(ret)

vectors_to_search = rng.random((1, dim))
print(fmt.format(f"Start search with retrieve serveral fields."))
result = milvus_client.search(collection_name, vectors_to_search, limit=3, output_fields=["title"])
for hits in result:
for hit in hits:
print(f"hit: {hit}")



field_index_names = milvus_client.list_indexes(collection_name, field_name = "embeddings")
print(f"index names for {collection_name}`s field embeddings:", field_index_names)

try:
milvus_client.drop_index(collection_name, "my_trie")
except Exception as e:
print(f"cacthed {e}")

milvus_client.release_collection(collection_name)

milvus_client.drop_index(collection_name, "my_trie")

milvus_client.drop_collection(collection_name)
Loading

0 comments on commit d7a402b

Please sign in to comment.