get_or_create_collection - Create or query a collection
get_or_create_collection() creates or retrieves a collection. If the collection does not exist in the database, it is created. If it exists, the corresponding result is returned.
This API is only available when you are connected using Client. For more information about 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 use seekdb in server mode or OceanBase Database, make sure the user you connect as 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 the table name
When creating a table, you must first define its name. The following requirements apply:
-
In seekdb, each table name must be unique within the database.
-
The table name must be between 64 and 512 characters.
-
We recommend that you give the table a meaningful name instead of using generic names such as t1 or table1. For more information about table naming conventions, see Table naming conventions.
Request parameters
get_or_create_collection(name = name,configuration = configuration, embedding_function = embedding_function )
| Parameter | Type | Required | Description | Example value |
|---|---|---|---|---|
name | string | Yes | The name of the collection to create or retrieve. | my_collection |
Schema | vector_index | sparse_vector_index | fulltext_index | 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. | schema = Schema(vector_index=VectorIndexConfig(hnsw=HNSWConfiguration(dimension=384, distance="cosine") ),sparse_vector_index=SparseVectorIndexConfig(embedding_function=BM25EmbeddingFunction(), source_key="#document"), fulltext_index=FulltextIndexConfig(analyzer='ik', properties={'ik_mode':'max_words'})) |
embedding_function | EmbeddingFunction | No | Function used to convert input into vectors. If omitted, DefaultEmbeddingFunction() is used (384 dimensions). If set to None, the collection has no embedding capability. If an embedding function is provided, the dimension is determined automatically according to configuration.dimension. | DefaultEmbeddingFunction() |
The parameters in Schema are described as follows:
| Parameter | Type | Required | Description | Example value |
|---|---|---|---|---|
vector_index | VectorIndexConfig | HNSWConfiguration | None | No | Dense index configuration. If omitted, an HNSW dense index is used by default. For details, see Dense vector index. | vector_index=VectorIndexConfig(hnsw=HNSWConfiguration(dimension=384, distance="cosine") ) |
sparse_vector_index | SparseVectorIndexConfig | None | No | Sparse index configuration. If omitted, no sparse index is used. For details, see Sparse vector index. | sparse_vector_index=SparseVectorIndexConfig(embedding_function=BM25EmbeddingFunction(), source_key="#document") |
fulltext_index | 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. | FulltextIndexConfig(analyzer='ik', properties={'ik_mode':'max_words'})) |
When you provide embedding_function, the system automatically calculates the vector dimension by calling the function. If you also specify configuration.dimension, its value must match the dimension of embedding_function. Otherwise, a ValueError is 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()
)
config = Configuration(hnsw=HNSWConfiguration(dimension=384), fulltext_config=FulltextIndexConfig(analyzer='ik', properties={'ik_mode':'max_words'}))
collection = client.get_or_create_collection(
name="my_collection5",
configuration=config,
embedding_function=None
)
Response parameters
None