跳到主要内容
版本:V1.2.0

createCollection - 创建 Collection

createCollection() 用于创建一个新的 Collection ,即数据库中的表。

信息

仅支持在使用 SeekdbClient 连接时,才能使用该接口。关于 SeekdbClient 的详细介绍,参见 SeekdbClient。

前提条件​

  • 您已经安装了 seekdb-js,有关安装 seekdb-js 的详细信息,参见 快速开始。

  • 如果您使用的是服务器模式的 seekdb,请确保已经安装了 seekdb 服务器模式,有关安装 seekdb 服务器模式的详细信息,参见 通过包管理工具部署 seekdb。

  • 您已经连接到数据库。有关连接的详细操作参见 SeekdbClient。

  • 请确保连接的用户具备 CREATE 权限。查看当前用户权限的相关操作请参见 查看用户权限。如果不具备该权限,请联系管理员为您授权,用户授权的相关操作请参见 直接授予权限。

  • 您已经安装或创建了所需的嵌入函数(embeddingFunction),嵌入函数将在添加和查询表时进行向量化。若未指定嵌入函数,将使用默认嵌入函数进行向量化。使用默认嵌入函数前,请先执行如下命令安装 @seekdb/default-embed。更多信息,请参见 默认嵌入函数。

    npm install @seekdb/default-embed

定义表名​

创建表时,需要先为表命名,以下是定义表名时需要遵循的要求:

  • 在 seekdb 中,每个表的名称必须保证在数据库内唯一。

  • 表名称支持超过 64 字符,少于 512 字符。

  • 建议给表起一个有意义的名字,不要使用 t1、table1 这样的表名。更多表命名规范信息,请参见 表命名规范。

请求参数​

createCollection(options: CreateCollectionOptions)
参数取值类型是否必选描述取值示例
namestring必选指定要创建的 Collection 的名称。my_collection
SchemavectorIndex | sparseVectorIndex | fulltextIndex可选具有维度和距离度量的索引配置。 如果未提供,则默认使用 HNSW 稠密索引和使用 IK 分词器的全文索引。const schema = new Schema({vectorIndex: new VectorIndexConfig({ hnsw: { dimension: 384, distance: "cosine" }, }), sparseVectorIndex: new SparseVectorIndexConfig({sourceKey: K.DOCUMENT, embeddingFunction: new Bm25EmbeddingFunction(), }),fulltextIndex: new FulltextIndexConfig("ik", { ik_mode: "smart" }) });
embeddingFunctionEmbeddingFunction | null可选嵌入函数,用于将文本转换为向量。传入 null 表示不使用嵌入函数DefaultEmbeddingFunction()

其中有关参数 Schema 中的参数说明如下:

参数取值类型是否必选描述取值示例
vectorIndexVectorIndexConfig | HNSWConfiguration | None可选稠密索引配置。如果不配置,则默认使用 HNSW 稠密索引。有关其配置详细介绍,请参考 向量索引vectorIndex: new VectorIndexConfig({ hnsw: { dimension: 384, distance: "cosine" }, })
sparseVectorIndexSparseVectorIndexConfig | None可选稀疏索引配置。如果不配置,则不使用稀疏索引。有关其配置详细介绍,请参考 稀疏索引 。sparseVectorIndex: new SparseVectorIndexConfig({sourceKey: K.DOCUMENT, embeddingFunction: new Bm25EmbeddingFunction(), })
fulltextIndexFulltextIndexConfig | None可选全文索引配置。其含有参数 analyzer 和 properties, 如果未提供,则使用默认值,默认为 analyzer=’ik’。有关其配置详细介绍,请参考 全文索引 。fulltextIndex: new FulltextIndexConfig("ik", { ik_mode: "smart" })
信息
  • 当提供 embeddingFunction 时,系统将通过调用该函数自动计算向量维数。如果还提供了 configuration.dimension,则它必须与 embeddingFunction 的维度匹配,否则将引发 ValueError。

  • embeddingFunction (EmbeddingFunction, 可选): 将文档转换为向量的函数。如果提供,维度将自动计算并与 configuration.dimension 验证。

请求示例​

import { SeekdbClient } from "seekdb";

// 1. Connect
// 嵌入式模式 - 数据库管理 (seekdb Embedded)
// const client = new SeekdbClient({
// path: "./seekdb.db",
// database: "test",
// });

// 远程服务器模式 - 数据库管理 (seekdb Server)
const client = new SeekdbClient({
host: "127.0.0.1",
port: 2881,
user: "root",
password: "",
database: "test",
});

// 2. Create collection
const collection = await client.createCollection({ name: "my_collection" });

返回参数​

无

相关操作​