milvus笔记、常用表结构

文章目录

通用表结构

通用表结构-简单描述

这个是给大模型的话术,它直接就能生成出来:

bash 复制代码
v_common # 表名
id # id int64
vector # 向量 float32 1536维
content # 内容 varchar(1024)
category # 分类 varchar(256)
通用表结构-代码创建表结构
python 复制代码
from custom_milvus_client import client


from pymilvus import MilvusClient, DataType

# 假设 client 已经通过 custom_milvus_client 初始化并连接
# from custom_milvus_client import client

collection_name = "v_common"

# 1. 定义 Schema
# auto_id=True: 表示 id 由 Milvus 自动生成,插入数据时无需传 id
# enable_dynamic_field=False: 强制严格模式,只能插入定义的字段
schema = client.create_schema(auto_id=True, enable_dynamic_field=False)

# 2. 添加字段
# --- 主键 ---
schema.add_field(field_name="id", datatype=DataType.INT64, is_primary=True)

# --- 向量字段 (1536维) ---
schema.add_field(field_name="vector", datatype=DataType.FLOAT_VECTOR, dim=1536)

# --- 内容字段 (Varchar 1024) ---
schema.add_field(field_name="content", datatype=DataType.VARCHAR, max_length=1024)

# --- 分类字段 (Varchar 256) ---
schema.add_field(field_name="category", datatype=DataType.VARCHAR, max_length=256)

# 3. 定义索引参数
# 使用 HNSW 索引(高性能) + COSINE 距离(适合文本向量)
index_params = client.prepare_index_params()
index_params.add_index(
    field_name="vector",
    index_type="HNSW",
    metric_type="COSINE",
    params={"M": 16, "efConstruction": 256}
)

# 4. 创建集合
# 如果集合已存在,先删除(开发环境常用,生产环境请慎用)
if client.has_collection(collection_name):
    client.drop_collection(collection_name)
    print(f"⚠️ 集合 '{collection_name}' 已存在,已删除并重建。")

client.create_collection(
    collection_name=collection_name,
    schema=schema,
    index_params=index_params,
    consistency_level="Strong"  # 强一致性,确保写入后立即能被搜到
)

print(f"✅ 集合 '{collection_name}' 创建成功!")
相关推荐
Eyfcom20 小时前
〖物流数据安全 02〗差分隐私与同态加密的工程边界:跨境物流快递系统该为隐私付出多少计算代价?
算法·机器学习·同态加密·跨境物流快递系统·物流数据安全
一只大鸽子21 小时前
游戏开发笔记16-像素画自然元素-水
笔记
郝学胜-神的一滴21 小时前
Effective STL 条款15:注意字符串实现上的差异
java·开发语言·数据结构·c++·算法·visualstudio
wuyk55521 小时前
AI 时代的嵌入式该怎么学-09-嵌入式vs后端vs算法
人工智能·stm32·单片机·嵌入式硬件·mcu·物联网·算法
sharemylove21 小时前
Java学习笔记 —— 面向对象专题
java·开发语言·笔记·学习·课程设计
labuladong1 天前
全新的算法 & Agent 课程页上线
算法·llm·agent
leobertlan1 天前
好玩系列 | 以简构繁,从把玩一把天平进入信息论和编码
算法
电科_银尘1 天前
【大模型算法与工程 48】项目实战:可调用、可追踪的大模型问答服务
算法