createCollection - Create a collection
The createCollection() method is used to create a new collection, which is equivalent to a table in the database.
This API can only be used when connected with SeekdbClient. For more information about SeekdbClient, see SeekdbClient.
Prerequisites
-
You have installed seekdb-js. For more information, see Quick start.
-
If you use seekdb in server mode, make sure seekdb server mode is installed. For more information, see Deploy seekdb with package managers.
-
You have connected to the database. For more information, see SeekdbClient.
-
The user to which you are connected has the
CREATEpermission. For more information about how to view the current user permissions, see View user privileges. If the user does not have this permission, contact the administrator to grant it. For more information about how to directly grant permissions, see Directly grant permissions. -
You have installed or created the embedding function (
embeddingFunction) you intend to use. The embedding function is used for vectorization when adding to or querying the collection. If you do not specify an embedding function, the default embedding function is used. Before using the default, run the following command to install@seekdb/default-embed. For more information, see Default embedding function.npm install @seekdb/default-embed
Define a table name
When creating a table, you must first define a table name. The following requirements must be met when defining a table name:
-
In seekdb, the name of each table must be unique within the database.
-
Table names support lengths of more than 64 characters and fewer than 512 characters.
-
We recommend that you use a meaningful name for the table instead of a generic name such as t1 or table1. For more information about table naming conventions, see Table naming conventions.
Request parameters
createCollection(options: CreateCollectionOptions)
| Parameter | Type | Required | Description | Example value |
|---|---|---|---|---|
name | string | Yes | The name of the collection to be created. | my_collection |
Schema | vectorIndex | sparseVectorIndex | fulltextIndex | No | Index configuration that specifies vector dimensions and distance metrics. If omitted, the default is an HNSW dense index and a full-text index that uses the IK tokenizer. | 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" }) }); |
embeddingFunction | EmbeddingFunction | null | No | The embedding function used to convert text to vectors. Pass null to disable the embedding function. | DefaultEmbeddingFunction() |
The parameters in Schema are described as follows:
| Parameter | Type | Required | Description | Example value |
|---|---|---|---|---|
vectorIndex | VectorIndexConfig | HNSWConfiguration | None | No | Dense index configuration. If omitted, an HNSW dense index is used by default. For details, see Vector index. | vectorIndex: new VectorIndexConfig({ hnsw: { dimension: 384, distance: "cosine" }, }) |
sparseVectorIndex | SparseVectorIndexConfig | None | No | Sparse index configuration. If omitted, no sparse index is used. For details, see Sparse vector index. | sparseVectorIndex: new SparseVectorIndexConfig({sourceKey: K.DOCUMENT, embeddingFunction: new Bm25EmbeddingFunction(), }) |
fulltextIndex | FulltextIndexConfig | None | No | Full-text index configuration. It includes analyzer and properties. If omitted, defaults apply; the default is analyzer='ik'. For details, see Full-text index. | fulltextIndex: new FulltextIndexConfig("ik", { ik_mode: "smart" }) |
-
If you specify the
embeddingFunctionparameter, the system automatically calculates the vector dimension by calling the function. If you also specifyconfiguration.dimension, its value must match the dimension of theembeddingFunction, otherwise a ValueError is returned. -
embeddingFunction(EmbeddingFunction, optional): The function used to convert documents to vectors. If you specify this parameter, the system automatically calculates the dimension and verifies it againstconfiguration.dimension.
Request example
import { SeekdbClient } from "seekdb";
// 1. Connect
// Embedded mode - Database management (seekdb Embedded)
// const client = new SeekdbClient({
// path: "./seekdb.db",
// database: "test",
// });
// Remote server mode - Database management (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" });
Response parameters
None