getOrCreateCollection - Create or query a collection
getOrCreateCollection() is used to create or query a collection. If the collection does not exist in the database, it will be created. If the collection exists, the corresponding result will be obtained.
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 server mode of seekdb. For more information, see Deploy by using yum install.
-
You have connected to the database. For more information, see SeekdbClient.
-
Make sure that the user to which you have connected has the
CREATEprivilege. For more information about how to view the privileges of the current user, see View user privileges. If the user does not have the privilege, contact the administrator to grant the privilege. For more information about how to directly grant privileges to a user, see Directly grant privileges.
Define a table name
Before you create a table, you must define a table name. The following requirements must be met when you define a table name:
-
In seekdb, the name of each table must be unique in the database.
-
The length of a table name must be between 64 and 512 characters.
-
We recommend that you use a meaningful name for a table instead of a generic name such as t1 or table1. For more information about table naming conventions, see Table naming conventions.
Request parameters
getOrCreateCollection(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 a vector. If you pass null, the embedding function is not used. | DefaultEmbeddingFunction() |
HNSWConfiguration parameters are described as follows:
| Parameter | Type | Required | Description | Example Value |
|---|---|---|---|---|
dimension | number | No | The dimension of the vector. If you provide the embeddingFunction, the system automatically infers the dimension from it. | dimension: 384,distance: 'cosine' |
distance | DistanceMetric | No | The distance metric. Valid values: 'l2' (Euclidean distance), 'cosine' (cosine similarity), and 'inner_product' (inner product). | HNSWConfiguration(dimension=384, distance='cosine') |
-
If you provide the
embeddingFunction, the system automatically calculates the vector dimension by calling the function. If you also provide theconfiguration.dimension, it must match the dimension of theembeddingFunction. Otherwise, a ValueError is returned. -
embeddingFunction(EmbeddingFunction, optional): The function used to convert a document to a vector. If you provide this parameter, the system automatically calculates the dimension and verifies that it matches theconfiguration.dimension.
Request example
import { SeekdbClient } from "seekdb";
// 1. Connect
const client = new SeekdbClient({
host: "127.0.0.1",
port: 2881,
user: "root",
password: "",
database: "test",
});
// If the collection exists, get it. Otherwise, create it.
const collection = await client.getOrCreateCollection({
name: 'my_collection',
configuration: {
dimension: 768,
distance: 'cosine'
}
});
Response parameters
None