get_or_create_collection - Create or query a collection
The get_or_create_collection() function creates or queries a collection. If the collection does not exist in the database, it is created. If it exists, the corresponding result is obtained.
This API is only available when using a client. For more information about the client, see Client.
Prerequisites
-
You have installed pyseekdb. For more information about how to install pyseekdb, see Quick Start.
-
You have connected to the database. For more information about how to connect, see Client.
-
If you are using seekdb in server mode or OceanBase Database, ensure that the connected user has the
CREATEprivilege. For more information about how to check the privileges of the current user, see Check User Privileges. If the user does not have this privilege, contact the administrator to grant it. For more information about how to directly grant privileges, see Directly Grant Privileges.
Define a table name
When creating a table, you need to define a table name. The following requirements must be met:
-
In seekdb, each table name must be unique within the database.
-
The table name must be no longer than 64 characters.
-
It is recommended to use meaningful names for tables instead of generic names like t1 or table1. For more information about table naming conventions, see Table Naming Conventions.
Request parameters
create_collection(name = name,configuration = configuration, embedding_function = embedding_function )
| Parameter | Value Type | Required | Description | Example Value |
|---|---|---|---|---|
name | string | Yes | The name of the collection to be created. | my_collection |
configuration | HNSWConfiguration | No | The index configuration with dimension and distance metric. If not provided, the default value is used, which is dimension=384, distance='cosine'. If set to None, the dimension will be calculated from the embedding_function value. | HNSWConfiguration(dimension=384, distance='cosine') |
embedding_function | EmbeddingFunction | No | The function to convert to vectors. If not provided, DefaultEmbeddingFunction()(384 dimensions) is used. If set to None, the collection will not include embedding functionality. If embedding functionality is provided, it will be automatically calculated based on configuration.dimension. | DefaultEmbeddingFunction() |
When embedding_function is provided, the system will automatically calculate the vector dimension by calling the function. If configuration.dimension is also provided, it must match the dimension of embedding_function, otherwise a ValueError will be raised.
Request example
import pyseekdb
from pyseekdb import DefaultEmbeddingFunction, HNSWConfiguration
# Create a client
client = pyseekdb.Client()
# Get or create collection (creates if doesn't exist)
collection = client.get_or_create_collection(
name="my_collection4",
configuration=HNSWConfiguration(dimension=384, distance='cosine'),
embedding_function=DefaultEmbeddingFunction()
)
Response parameters
None