CollectionsClient
Synchronous client for managing collections. Create, list, get, and delete collections using this client.
Get a CollectionsClient instance from the main client:
from topk_sdk import Client
client = Client(api_key="YOUR_KEY", region="aws-us-east-1-elastica")
collections = client.collections()
create()
Create a new collection with the specified schema.
from topk_sdk.schema import text, int, semantic_index
collection = client.collections().create(
"books",
schema={
"title": text().required().index(semantic_index()),
"year": int().required()
}
)
The name of the collection to create. Must be unique within your project.
schema
Mapping[str, FieldSpec]
required
A dictionary mapping field names to field specifications. See Schema documentation for available field types and indexes.
The created collection object containing metadata like name, org_id, project_id, region, and schema.
list()
List all collections in your project.
all_collections = client.collections().list()
for collection in all_collections:
print(f"Collection: {collection.name}")
print(f"Region: {collection.region}")
print(f"Schema: {collection.schema}")
A list of all collections in your project.
get()
Get information about a specific collection.
collection = client.collections().get("books")
print(f"Name: {collection.name}")
print(f"Schema: {collection.schema}")
print(f"Region: {collection.region}")
The name of the collection to retrieve.
The collection object containing metadata.
delete()
Delete a collection and all its documents.
This operation is irreversible. All documents in the collection will be permanently deleted.
client.collections().delete("books")
The name of the collection to delete.
This method does not return a value.
AsyncCollectionsClient
Asynchronous client for managing collections. All methods are identical to CollectionsClient but return awaitables.
from topk_sdk import AsyncClient
from topk_sdk.schema import text, semantic_index
client = AsyncClient(api_key="YOUR_KEY", region="aws-us-east-1-elastica")
# Create a collection
collection = await client.collections().create(
"books",
schema={"title": text().index(semantic_index())}
)
# List all collections
all_collections = await client.collections().list()
# Get a specific collection
collection = await client.collections().get("books")
# Delete a collection
await client.collections().delete("books")
create()
Create a new collection asynchronously.
The name of the collection to create.
schema
Mapping[str, FieldSpec]
required
A dictionary mapping field names to field specifications.
An awaitable that resolves to the created collection object.
list()
List all collections asynchronously.
return
Awaitable[list[Collection]]
An awaitable that resolves to a list of all collections.
get()
Get information about a specific collection asynchronously.
The name of the collection to retrieve.
An awaitable that resolves to the collection object.
delete()
Delete a collection asynchronously.
The name of the collection to delete.
An awaitable that resolves when the deletion is complete.
Collection Object
The Collection object represents a collection in TopK and is returned by the create(), get(), and list() methods.
collection = client.collections().get("books")
# Access collection properties
print(collection.name) # "books"
print(collection.org_id) # Your organization ID
print(collection.project_id) # Your project ID
print(collection.region) # "aws-us-east-1-elastica"
print(collection.schema) # {"title": FieldSpec(...), ...}
The name of the collection.
The organization ID that owns the collection.
The project ID that owns the collection.
The region where the collection is stored.
The schema definition for the collection. Maps field names to their specifications.
Example: Full Workflow
Here’s a complete example showing how to create, use, and delete a collection:
from topk_sdk import Client
from topk_sdk.schema import text, int, semantic_index, keyword_index
from topk_sdk.query import select, field, fn
# Initialize client
client = Client(api_key="YOUR_KEY", region="aws-us-east-1-elastica")
# Create a collection with schema
collection = client.collections().create(
"books",
schema={
"title": text().required().index(semantic_index()),
"description": text().index(keyword_index()),
"year": int()
}
)
print(f"Created collection: {collection.name}")
# Insert some documents
client.collection("books").upsert([
{"_id": "1", "title": "The Great Gatsby", "year": 1925},
{"_id": "2", "title": "1984", "year": 1949}
])
# Query the collection
results = client.collection("books").query(
select(
"title", "year",
similarity=fn.semantic_similarity("title", "dystopian novel")
).topk(field("similarity"), 10)
)
print(f"Found {len(results)} results")
# List all collections
all_collections = client.collections().list()
print(f"Total collections: {len(all_collections)}")
# Get collection info
collection_info = client.collections().get("books")
print(f"Collection region: {collection_info.region}")
# Delete the collection
client.collections().delete("books")
print("Collection deleted")