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.
-
You have installed the seekdb server mode. For more information, see Deploy by using yum install.
-
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.
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.
-
The table name can be between 64 and 512 characters in length.
-
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 |
configuration | Configuration | HNSWConfiguration | No | The collection configuration, which contains the HNSWConfiguration and FulltextAnalyzerConfig attributes. | {hnsw: {dimension: 384, distance: 'cosine'}, fulltextConfig: {analyzer: 'ik', ik_mode: 'smart'}} |
embeddingFunction | EmbeddingFunction | null | No | The embedding function used to convert text to vectors. If you set the parameter to null, the embedding function is not used. | DefaultEmbeddingFunction() |
The HNSWConfiguration parameter is described as follows:
| Parameter | Type | Required | Description | Example value |
|---|---|---|---|---|
dimension | number | No | The vector dimension. If you specify the embeddingFunction parameter, the system automatically infers the value of this parameter from the embeddingFunction. | dimension: 384,distance: 'cosine' |
distance | DistanceMetric | No | The distance metric. Valid values: 'l2' (Euclidean distance), 'cosine' (cosine similarity), and 'inner_product' (inner product). | dimension: 384, distance: 'cosine' |
-
If you specify the
embeddingFunctionparameter, the system automatically calculates the vector dimension by calling the function. If you also specify theconfiguration.dimensionparameter, the value ofconfiguration.dimensionmust match the dimension of theembeddingFunction, otherwise a ValueError will be 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 that it matches the value of theconfiguration.dimensionparameter.
Request example
import { SeekdbClient } from "seekdb";
// 1. Connect
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