parent
795ba9028f
commit
1756e83225
Binary file not shown.
Binary file not shown.
@ -0,0 +1,38 @@
|
||||
from pymilvus import FieldSchema, DataType, utility
|
||||
from WxMini.Milvus.Utils.MilvusCollectionManager import MilvusCollectionManager
|
||||
from WxMini.Milvus.Utils.MilvusConnectionPool import *
|
||||
from WxMini.Milvus.Config.MulvusConfig import *
|
||||
|
||||
# 1. 使用连接池管理 Milvus 连接
|
||||
milvus_pool = MilvusConnectionPool(host=MS_HOST, port=MS_PORT, max_connections=MS_MAX_CONNECTIONS)
|
||||
|
||||
# 2. 从连接池中获取一个连接
|
||||
connection = milvus_pool.get_connection()
|
||||
|
||||
# 3. 初始化集合管理器
|
||||
collection_name = MS_COLLECTION_NAME
|
||||
collection_manager = MilvusCollectionManager(collection_name)
|
||||
|
||||
# 4. 判断集合是否存在,存在则删除
|
||||
if utility.has_collection(collection_name):
|
||||
print(f"集合 '{collection_name}' 已存在,正在删除...")
|
||||
utility.drop_collection(collection_name)
|
||||
print(f"集合 '{collection_name}' 已删除。")
|
||||
|
||||
# 5. 定义集合的字段和模式
|
||||
fields = [
|
||||
FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, auto_id=True), # 主键字段,自动生成 ID
|
||||
FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=128) # 向量字段,维度为 128
|
||||
]
|
||||
schema_description = "Simple demo collection"
|
||||
|
||||
# 6. 创建集合
|
||||
print(f"正在创建集合 '{collection_name}'...")
|
||||
collection_manager.create_collection(fields, schema_description)
|
||||
print(f"集合 '{collection_name}' 创建成功。")
|
||||
|
||||
# 7. 释放连接
|
||||
milvus_pool.release_connection(connection)
|
||||
|
||||
# 8. 关闭连接池
|
||||
milvus_pool.close()
|
@ -0,0 +1,56 @@
|
||||
# insert_data.py
|
||||
import random
|
||||
|
||||
from pymilvus import FieldSchema, DataType
|
||||
|
||||
from WxMini.Milvus.Utils.MilvusCollectionManager import MilvusCollectionManager
|
||||
from WxMini.Milvus.Utils.MilvusConnectionPool import *
|
||||
from WxMini.Milvus.Config.MulvusConfig import *
|
||||
|
||||
# 1. 使用连接池管理 Milvus 连接
|
||||
milvus_pool = MilvusConnectionPool(host=MS_HOST, port=MS_PORT, max_connections=MS_MAX_CONNECTIONS)
|
||||
|
||||
# 2. 从连接池中获取一个连接
|
||||
connection = milvus_pool.get_connection()
|
||||
|
||||
# 3. 初始化集合管理器
|
||||
collection_name = MS_COLLECTION_NAME
|
||||
collection_manager = MilvusCollectionManager(collection_name)
|
||||
|
||||
# 4. 插入数据
|
||||
data = [
|
||||
[random.random() for _ in range(128)], # 第一个 128 维向量
|
||||
[random.random() for _ in range(128)], # 第二个 128 维向量
|
||||
[random.random() for _ in range(128)] # 第三个 128 维向量
|
||||
]
|
||||
entities = [data] # 插入的数据
|
||||
collection_manager.insert_data(entities)
|
||||
|
||||
# 5. 创建索引
|
||||
index_params = {
|
||||
"index_type": "IVF_FLAT", # 使用 IVF_FLAT 索引类型
|
||||
"metric_type": "L2", # 使用 L2 距离度量方式
|
||||
"params": {"nlist": 128} # 设置 IVF_FLAT 的 nlist 参数
|
||||
}
|
||||
collection_manager.create_index("embedding", index_params)
|
||||
|
||||
# 6. 加载集合到内存
|
||||
collection_manager.load_collection()
|
||||
|
||||
# 7. 查询数据,验证插入是否成功
|
||||
query_vector = [random.random() for _ in range(128)] # 随机生成一个查询向量
|
||||
search_params = {
|
||||
"metric_type": "L2", # 使用 L2 距离度量方式
|
||||
"params": {"nprobe": 10} # 设置 IVF_FLAT 的 nprobe 参数
|
||||
}
|
||||
results = collection_manager.search(query_vector, search_params, limit=2)
|
||||
print("查询结果:")
|
||||
for hits in results:
|
||||
for hit in hits:
|
||||
print(f"ID: {hit.id}, Distance: {hit.distance}")
|
||||
|
||||
# 8. 释放连接
|
||||
milvus_pool.release_connection(connection)
|
||||
|
||||
# 9. 关闭连接池
|
||||
milvus_pool.close()
|
@ -0,0 +1,37 @@
|
||||
import random
|
||||
|
||||
from WxMini.Milvus.Utils.MilvusCollectionManager import MilvusCollectionManager
|
||||
from WxMini.Milvus.Utils.MilvusConnectionPool import *
|
||||
from WxMini.Milvus.Config.MulvusConfig import *
|
||||
|
||||
# 1. 使用连接池管理 Milvus 连接
|
||||
milvus_pool = MilvusConnectionPool(host=MS_HOST, port=MS_PORT, max_connections=MS_MAX_CONNECTIONS)
|
||||
|
||||
# 2. 从连接池中获取一个连接
|
||||
connection = milvus_pool.get_connection()
|
||||
|
||||
# 3. 初始化集合管理器
|
||||
collection_name = MS_COLLECTION_NAME
|
||||
collection_manager = MilvusCollectionManager(collection_name)
|
||||
|
||||
# 4. 加载集合到内存
|
||||
collection_manager.load_collection()
|
||||
print(f"集合 '{collection_name}' 已加载到内存。")
|
||||
|
||||
# 5. 查询数据
|
||||
query_vector = [random.random() for _ in range(128)] # 随机生成一个查询向量
|
||||
search_params = {
|
||||
"metric_type": "L2", # 使用 L2 距离度量方式
|
||||
"params": {"nprobe": 10} # 设置 IVF_FLAT 的 nprobe 参数
|
||||
}
|
||||
results = collection_manager.search(query_vector, search_params, limit=2)
|
||||
print("查询结果:")
|
||||
for hits in results:
|
||||
for hit in hits:
|
||||
print(f"ID: {hit.id}, Distance: {hit.distance}")
|
||||
|
||||
# 6. 释放连接
|
||||
milvus_pool.release_connection(connection)
|
||||
|
||||
# 7. 关闭连接池
|
||||
milvus_pool.close()
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue