新建 Collection

最近更新时间:2025-07-01 10:19:42

我的收藏

接口定义

create_collection() 用于为已创建的 Base 类向量数据库创建 Collection。
def create_collection(self,
database_name: str,
collection_name: str,
shard: int,
replicas: int,
description: str = None,
index: Index = None,
embedding: Embedding = None,
timeout: float = None,
ttl_config: dict = None,
filter_index_config: FilterIndexConfig = None,
indexes: List[IndexField] = None,
) -> RPCCollection:

使用示例

说明:
当前版本一个数据库实例下,不支持创建同名的 Collection。
不做过滤查询、检索的标量字段不必建立 Filter 索引。切勿将所有标量字段建立索引,导致内存资源的浪费。
插入数据时动态增加的标量字段暂不支持创建索引。创建 Collection 时,需规划好 Filter 索引的字段。
向量读写
Embedding 文本读写
仅稀疏向量集合
二进制集合
支持 Json 与 AutoID
写入文件集合
在 Base 类数据库 db-test 下,创建一个名为 book-vector 的集合,不配置 Embedding 模型相关参数,写入3维向量数据。
import tcvectordb
from tcvectordb.model.enum import FieldType, IndexType, MetricType
from tcvectordb.model.index import Index, VectorIndex, FilterIndex, HNSWParams, SparseVector, SparseIndex

# -- index config
index = Index(
FilterIndex(name='id', field_type=FieldType.String, index_type=IndexType.PRIMARY_KEY),
FilterIndex(name='author', field_type=FieldType.String, index_type=IndexType.FILTER),
FilterIndex(name='bookName', field_type=FieldType.String, index_type=IndexType.FILTER),
FilterIndex(name='tags', field_type=FieldType.Array, index_type=IndexType.FILTER),
VectorIndex(name='vector', field_type=FieldType.Float16Vector, dimension=3, index_type=IndexType.HNSW,
metric_type=MetricType.COSINE, params=HNSWParams(m=16, efconstruction=200))
)
# create a collection
coll = client.create_collection(
database_name='db-test',
collection_name='book-vector',
shard=1,
replicas=1,
description='this is a collection of test vector',
index=index
)
print(vars(coll))
在 Base 类数据库 db-test 下,创建一个名为 book-emb 的集合 ,配置 Embedding 模型相关参数,写入原始文本。Embedding 模型自动将原始文本进行向量化。
import tcvectordb
from tcvectordb.model.enum import FieldType, IndexType, MetricType, EmbeddingModel
from tcvectordb.model.collection import Embedding
from tcvectordb.model.index import Index, VectorIndex, FilterIndex, HNSWParams, SparseVector, SparseIndex

# -- index config
index = Index(
FilterIndex(name='id', field_type=FieldType.String, index_type=IndexType.PRIMARY_KEY),
VectorIndex(name='vector', dimension=768, index_type=IndexType.HNSW,
metric_type=MetricType.COSINE, params=HNSWParams(m=16, efconstruction=200)),
# 稀疏向量索引
# SparseIndex(name='sparse_vector',
# field_type=FieldType.SparseVector,
# index_type=IndexType.SPARSE_INVERTED,
# metric_type=MetricType.IP),
FilterIndex(name='author', field_type=FieldType.String, index_type=IndexType.FILTER),
FilterIndex(name='tags', field_type=FieldType.Array, index_type=IndexType.FILTER),
FilterIndex(name='bookName', field_type=FieldType.String, index_type=IndexType.FILTER))
# Embedding config
ebd = Embedding(vector_field='vector', field='text', model_name='bge-base-zh')

# create a collection
coll = client.create_collection(
database_name='db-test',
collection_name='book-emb',
shard=1,
replicas=1,
description='this is a collection of test embedding',
embedding=ebd,
index=index
)
print(vars(coll))
在 Base 类数据库 db-test 下,创建一个名为 book-vector 的集合,不配置 Embedding 模型相关参数,写入3维向量数据,支持稀疏向量写入。
import tcvectordb
from tcvectordb.model.enum import FieldType, IndexType, MetricType
from tcvectordb.model.index import Index, VectorIndex, FilterIndex, HNSWParams, SparseVector, SparseIndex

# -- index config
index = Index(
FilterIndex(name='id', field_type=FieldType.String, index_type=IndexType.PRIMARY_KEY),
SparseIndex(name='sparse_vector',
field_type=FieldType.SparseVector,
index_type=IndexType.SPARSE_INVERTED,
metric_type=MetricType.IP))
# create a collection
coll = client.create_collection(
database_name='db-test',
collection_name='book-vector',
shard=1,
replicas=1,
description='this is a collection of test vector',
index=index)
print(vars(coll))
在 Base 类数据库 db-test 下,创建一个名为 book-vector 的集合,不配置 Embedding 模型相关参数,写入3维向量数据。
import tcvectordb
from tcvectordb.model.enum import FieldType, IndexType, MetricType
from tcvectordb.model.index import Index, VectorIndex, FilterIndex
from tcvectordb.toolkit.binary import binary_to_uint8
# -- index config
index = Index(
FilterIndex(name='id', field_type=FieldType.String, index_type=IndexType.PRIMARY_KEY),
FilterIndex(name='bookName', field_type=FieldType.String, index_type=IndexType.FILTER),
VectorIndex(name='vector', dimension=16, index_type=IndexType.BIN_FLAT,
metric_type=MetricType.HAMMING, field_type=FieldType.BinaryVector)
)
# create a collection
coll = client.create_collection(
database_name='db-test',
collection_name='bin-vector',
shard=1,
replicas=1,
description='this is a collection of test bin vector',
index=index
)
print(vars(coll))
import tcvectordb
from tcvectordb.model.enum import FieldType, IndexType, MetricType
from tcvectordb.model.index import Index, VectorIndex, FilterIndex, HNSWParams
coll = client.create_collection_if_not_exists(
database_name="db-test",
collection_name="coll-json-autoid",
shard=1,
replicas=0,
description='test collection json and autoid',
indexes=[
FilterIndex('id', field_type=FieldType.String, index_type=IndexType.PRIMARY_KEY, auto_id='uuid'),
VectorIndex('vector', 1, IndexType.HNSW, MetricType.IP, HNSWParams(m=16, efconstruction=200)),
FilterIndex(name='bookInfo', field_type=FieldType.Json, index_type=IndexType.FILTER),
],
)

# print collection
print(vars(coll))
注意:
在创建存储文件内容的 Collection 时,索引字段的设计需遵循以下限制与建议,否则可能导致上传文件失败。
文件名字段(file_name):该字段必须定义为 string 类型的 filter 索引,以支持文件的过滤检索和处理同名文件覆盖的情况。字段名可以自定义,在上传文件时,需通过参数 field_mappings 的 filename 映射自定义的字段名。
文件内容字段(text):该字段用于存储知识点的原始文本内容。由于文本内容可能较大,不建议为该字段创建索引,以避免占用过多内存空间。数据库会自动写入该字段的内容。在查询时,只需要通过 output_fields 参数指定该字段即可返回原始文本。字段名可以自定义,但需通过参数 field_mappings 的 text 映射自定义的字段名。
文件图片信息字段(image_list):该字段存放 PDF 文件中图片的 Key 列表。在原始检索出的文本块中,图片位置将以 {key} 的形式进行占位。通过调用接口 getImageUrl,可以获取图片 URL 地址列表,这些地址与 Key 一一对应,从而实现将原始 PDF 文档恢复并展示为 HTML 格式。
不建议为该字段创建索引。如果需要创建索引,必须确保其为数组(array)类型,否则接口将报错,导致文件无法上传。
字段名可以自定义,但需要在上传文件时通过参数 field_mappings 的 imageList 映射到自定义的字段名。
import tcvectordb
from tcvectordb.model.enum import FieldType, IndexType, MetricType
from tcvectordb.model.index import Index, VectorIndex, FilterIndex, HNSWParams
coll = client.create_collection_if_not_exists(
database_name="db-test",
collection_name="coll-file-test",
shard=1,
replicas=0,
description='test collection file upload',
indexes=[
FilterIndex('id', field_type=FieldType.String, index_type=IndexType.PRIMARY_KEY),
VectorIndex('vector', 768, IndexType.HNSW, MetricType.IP, HNSWParams(m=16, efconstruction=200)),
FilterIndex(name='file_name', field_type=FieldType.String, index_type=IndexType.FILTER)
],
)

# print collection
print(vars(coll))

入参描述

创建集合之前,需要创建主键索引、向量索引、标量索引、稀疏向量索引。

Index 参数

参数名
子参数
是否必选
参数配置
FilterIndex
name
配置可作为 Filter 索引的自定义扩展的标量字段名。
说明:
Filter 索引(Filter Index)对标量字段建立的索引。标量字段名称、类型均由用户自定义,不限制标量字段数量.
标量字段被建立 Filter 索引之后,向量检索时,将依据 Filter 指定的标量字段的条件表达式进行过滤查询和范围查询以此来匹配相似向量。
建立 Filter 索引时,选取需要使用 Filter 表达式高效过滤数据的标量字段。不做过滤查询、检索的标量字段不必建立 Filter 索引。切勿将所有标量字段建立索引,导致内存资源的浪费。
必须构建唯一的 Document id 为主键的 Filter 索引,配置 name 固定为 id
配置其他自定义扩展的可作为 Filter 索引的标量字段,例如:author、page 等。该标量字段名称、类型均由用户自定义,且不限制字段数量。
field_type
指定 Filter 字段的数据类型。取值如下:
String:字符型。若 name id,则该参数固定为 FieldType.String
Uint64:无符号整数,该参数可设置为 FieldType.Uint64
Array:数组类型,该参数可设置为 FieldType.Array,数组元素默认为 String
Json:由键值对组成的数据对象。具体规则,请参见 Json 类型。为 json 类型的字段创建 Filter 索引,如下所示。
注意:
开启 动态标量字段(动态 Schema )时,即使用 filterIndexConfig 开启所有标量字段全索引模式,现已支持配置 json 类型的 Filter 索引。
FilterIndex(name='bookInfo',
field_type=FieldType.Json,
index_type=IndexType.FILTER)
index_type
指定 Filter 字段的索引类型。
name id,该参数固定配置为 IndexType.PRIMARY_KEY,即以 id 为主键构建索引。
name 为其他自定义的标量字段,预在检索时,依据该字段过滤数据,可配置该字段索引类型为 IndexType.FILTER。
auto_id
自动生成 ID。指定 "autoId": "uuid",即可开启自动生成 ID 功能。
说明:
当启用 autoId 功能后,用户在写入或更新数据时,无需手动传入 id 字段。若用户在启用 autoId 的情况下仍传入了 id 字段,则系统将优先采用用户传入的 id 值作为数据的唯一标识。
在执行批量写入(batch upsert)时,如果部分数据包含 id 字段,而另一部分数据未包含 id 字段,则系统仅对未传入 id 的数据生成默认 id。
VectorIndex
name
指定向量索引字段名,固定为 vector
field_type
向量字段的数据类型。支持不同类型、不同精度的向量数据,以适用不同业务场景。更多信息,请参见 数据类型
Vector:单精度浮点数向量,采用8位指数位 + 23位尾数位 + 1位符号位(共32位),适用于需最高精度的计算任务或对误差敏感的向量检索。使用示例:field_type=FieldType.Vector
Float16Vector:半精度浮点数向量,采用5位指数位 + 10位尾数位 + 1位符号位 的格式,适用于尾数精度较高(小数点后保留更多位数)的场景,如 BGE-large 等模型生成的嵌入向量。使用示例:field_type=FieldType.Float16Vector。
BFloat16Vector:bfloat16 浮点数向量,采用8位指数位 + 7位尾数位 + 1位符号位的格式,适用于向量数值范围较大(更多bit位表达整数)的场景。
BinaryVector:每个维度仅用 1 个比特(bit) 表示0或1,无指数位、尾数位和符号位,直接存储二进制数据,适用于存储稀疏向量。
index_type
指定索引类型,取值如下所示。更多信息,请参见 索引类型
FLAT:暴力检索,召回率100%,但检索效率低,适用10万以内数据规模。
HNSW:召回率95%+,可通过参数调整召回率,检索效率高,但数据量大后写入效率会变低,适用于10万-1亿区间数据规模。
BIN_FLAT:二进制索引,暴力检索,召回率100%。
IVF_FLAT、IVF_PQ、IVF_SQ4, IVF_SQ8, IVF_SQ16:IVF 系列索引,适用于上亿规模的数据集,检索效率高,内存占用低,写入效率高。
dimension
指定向量维度。
取值类型:uint64。
取值范围:[1,4096]。
说明:
indexType BIN_FLAT,向量维度大于等于8,且必须为8的倍数。
开通 Embedding 功能,则无需配置该字段,该字段将自动配置为 Embedding 模型对应的向量维度。
metric_type
指定向量之间距离度量的算法。取值如下:
L2:全称是 Euclidean distance,指欧几里得距离,它计算向量之间的直线距离,所得的值越小,越与搜索值相似。L2在低维空间中表现良好,但是在高维空间中,由于维度灾难的影响,L2的效果会逐渐变差。
IP:全称为 Inner Product,是一种计算向量之间相似度的度量算法,它计算两个向量之间的点积(内积),所得值越大越与搜索值相似。
COSINE:余弦相似度(Cosine Similarity)算法,是一种常用的文本相似度计算方法。它通过计算两个向量在多维空间中的夹角余弦值来衡量它们的相似程度。所得值越大越与搜索值相似。
Hamming:汉明距离(Hamming Distance),计算两个二进制字符串对应位置上不同字符的数量,如果字符不同,两字符串的汉明距离就会加一。汉明距离越小,表示两个字符串之间的相似度越高。
params
指定索引类型 indexType HNSW ,需配置如下参数。
m:每个节点在检索构图中可以连接多少个邻居节点。
取值类型:uint64。
取值范围:[4,64]。默认为16。
efconstruction:搜索时,指定寻找节点邻居遍历的范围。数值越大构图效果越好,构图时间越长。
取值类型:uint64。
取值范围:[8,512]。默认为200。
索引类型 indexType IVF_FLAT、 IVF_PQ、 IVF_SQ4、IVF_SQ8、IVF_SQ16,需配置如下参数。
nlist :指索引中的聚类中心数量。
取值类型:uint64。
取值范围:[1,65536]。
m: 指乘积量化中原始数据被拆分的子向量的数量,仅 IVF_PQ 索引类型需配置。
取值要求:原始数据的向量的维度 D(即向量中元素的个数)必须能够被 m 整除,m 必须是一个正整数。
取值范围:[1,向量维度]。
SparseIndex
name
可选择构建稀疏向量索引,字段名固定为 sparse_vector每个集合仅支持创建1个稀疏向量索引,无需指定稀疏向量维度。
field_type
稀疏向量字段类型固定为 FieldType.SparseVector
index_type
稀疏向量索引类型固定为 IndexType.SPARSE_INVERTED
metric_type
稀疏向量相似性计算仅支持设置为 IP

Embedding 参数

集合开启 Embedding,需配置 Embedding 模型相关参数,写入原始文本。
子参数
是否必选
参数配置
field
指定文本字段名称。取值类型:String。
说明:
通过 upsert() 写入数据或通过 update() 更新数据时,Embedding 模型会自动将该字段的文本内容转换成向量数据。
vector_field
指定向量字段。通过 Embedding 模型生成的向量会自动存储在该字段中。固定为 vector
model_name
指定使用的 Embedding 模型的名称。您需根据业务的语言类型、数据维度要求等综合选择合适的模型。具体信息,参见 Embedding 介绍。取值如下所示:
bge-large-zh-v1.5:适用中文,1024维,推荐使用。
bge-base-zh-v1.5:适用中文,768维。
bge-large-zh:适用中文,1024维。
bge-base-zh:适用中文,768维。
m3e-base:适用中文,768维。
e5-large-v2:适用英文,1024维。
text2vec-large-chinese:适用中文,1024维。
multilingual-e5-base:适用于多种语言类型,768维。
BAAI/bge-m3:适用于多种语言类型,1024维。

Collection 参数

参数
参数含义
是否必选
参数配置
database_name
指定数据库名
Database 命名要求如下:
只能使用英文字母,数字,下划线_、中划线-,并以英文字母开头。
长度要求:[1,128]。
collection_name
指定 Collection 的名称。
Collection 命名要求如下:
只能使用英文字母,数字,下划线_、中划线-,并以英文字母开头。
长度要求:[1,128]。
replicas
指定 Collection 的副本数。副本数是指每个主分片有多个相同的备份,用来容灾和负载均衡。
取值类型:uint64。
取值范围如下所示。搜索请求量越高的索引,建议设置越多的副本数,避免负载不均衡。
单可用区实例:0。
两可用区实例:[1,节点数-1]。
三可用区实例:[2,节点数-1]。
shard
指定 Collection 的分片数。分片是把大数据集切成多个子数据集。
取值类型:uint64。
取值范围:[1,100]。例如:5。
配置建议:在搜索时,全部分片是并发执行的,分片数量越多,平均耗时越低,但是过多的分片会带来额外开销而影响性能。
单分片数据量建议控制在300万以内,例如500万向量,可设置2个分片。
如果数据量小于300万,建议使用1分片。系统对1分片有特定优化,可显著提升性能。
description
指定 Collection 的描述信息。
取值类型:string。
字符长度要求:[1,256]。
示例:this is the collection description。
index
指定 Collection 存储文档的索引属性。
将已创建的 Index 参数赋值给 index。
embedding
Embedding 相关参数。
将已创建的 Embedding 参数赋值给 embedding

ttl_config

标识数据库是否开启 TTL 配置,指定存储数据过期时间戳字段名。
enable:标识数据库是否开启 TTL 属性。
True:开启。
False:关闭。
timeField:指定存储数据过期时间戳的字段名。插入数据时,以标准的 Unix 时间戳指定该字段的值。字段名要求如下:
数据类型:uint64。
索引类型:Filter 索引。
示例:expired_at。
ttl_config={
'enable': True,
'timeField': 'expire_at'
}
filter_index_config
支持开启所有标量字段全索引模式,默认关闭
filter_all:控制标量字段全索引模式的开启和关闭。
true:开启。
false:关闭。默认值为 fasle。
fields_without_index:当 filterAll 为 true 时,支持通过 fieldWithoutFilterIndex 参数指定不创建索引的字段。
若 filterAll 为 false 时不支持配置。
默认值:NULL。
max_str_len:当 filterAll 为 true 时,支持通过 maxFieldLength 参数设定单条文档中创建索引的标量字段的最大字符数。如果字段字符数超出此限制,将按照设定的 maxStrLen 值进行截断创建索引。
默认值:32。
取值范围:[1,65536]。
filter_index_config=FilterIndexConfig(
filter_all=True,
fields_without_index=['author'],
max_str_len=32,
)
timeout
请求超时时间。


单位:秒。
默认值:VectorDBClient() 接口配置的 timeout 时长。
取值范围:大于等于0。