WeaviateEx.Collections (WeaviateEx v0.7.4)
View SourceFunctions for managing Weaviate collections (schema classes).
Collections define the structure of your data including properties, vectorization settings, and indexing configuration.
Examples
# List all collections
{:ok, schema} = WeaviateEx.Collections.list()
# Get a specific collection
{:ok, collection} = WeaviateEx.Collections.get("Article")
# Create a new collection
{:ok, collection} = WeaviateEx.Collections.create("Article", %{
description: "A collection for articles",
properties: [
%{name: "title", dataType: ["text"]},
%{name: "content", dataType: ["text"]},
%{name: "publishedAt", dataType: ["date"]}
],
vectorizer: "text2vec-openai"
})
# Update a collection
{:ok, collection} = WeaviateEx.Collections.update("Article", %{
description: "Updated description"
})
# Delete a collection
{:ok, _} = WeaviateEx.Collections.delete("Article")
# Delete all collections
{:ok, deleted_count: 3} = WeaviateEx.Collections.delete_all()
# Add a property to an existing collection
{:ok, property} = WeaviateEx.Collections.add_property("Article", %{
name: "author",
dataType: ["text"]
})
# Enable multi-tenancy and confirm the collection exists
{:ok, %{"enabled" => true}} = WeaviateEx.Collections.set_multi_tenancy("Article", true)
{:ok, true} = WeaviateEx.Collections.exists?("Article")Tenant-Scoped Operations
For multi-tenant collections, use with_tenant/3 to get a tenant-scoped
collection reference:
tenant_col = WeaviateEx.Collections.with_tenant(client, "Articles", "tenant_A")
# All operations automatically scoped to tenant_A
{:ok, _} = WeaviateEx.TenantCollection.insert(tenant_col, %{title: "Hello"})
{:ok, results} = tenant_col
|> WeaviateEx.TenantCollection.query()
|> WeaviateEx.Query.bm25("search")
|> WeaviateEx.Query.execute(client)
Summary
Functions
Adds a new property to an existing collection.
Adds tenants to a multi-tenant collection.
Creates a new collection.
Deletes a collection and all its objects.
Deletes all collections from the schema.
Checks whether a collection exists.
Gets a specific collection by name.
Gets the shards for a collection.
Gets tenants for a multi-tenant collection.
Insert multiple objects into a collection in a single batch operation.
Lists all collections in the schema.
Removes tenants from a multi-tenant collection.
Enable or disable multi-tenancy for a collection.
Updates an existing collection.
Updates a shard status.
Updates the Object TTL configuration for an existing collection.
Returns a tenant-scoped collection reference.
Types
Functions
@spec add_property(collection_name(), property(), Keyword.t()) :: WeaviateEx.api_response()
Adds a new property to an existing collection.
Parameters
collection_name- The name of the collectionproperty- Property definitionopts- Additional options
Property Definition
:name- Property name (required):dataType- Data type(s) (required, e.g., ["text"], ["int"], ["Article"]):description- Human-readable description:moduleConfig- Module-specific configuration:indexFilterable- Whether to index for filtering (default: true):indexSearchable- Whether to index for searching (default: true):tokenization- Tokenization method for text (e.g., "word", "field")
Examples
iex> WeaviateEx.Collections.add_property("Article", %{
...> name: "author",
...> dataType: ["text"],
...> description: "The article author"
...> })
{:ok, %{"name" => "author", ...}}
@spec add_tenants(collection_name(), [map()], Keyword.t()) :: WeaviateEx.api_response()
Adds tenants to a multi-tenant collection.
Examples
iex> WeaviateEx.Collections.add_tenants("Article", [
...> %{name: "tenant1"},
...> %{name: "tenant2"}
...> ])
{:ok, [...]}
@spec create(collection_name(), collection_config(), Keyword.t()) :: WeaviateEx.api_response()
Creates a new collection.
Parameters
name- The name of the collection (must start with uppercase)config- Collection configuration including properties, vectorizer, etc.opts- Additional options
Configuration Options
:description- Human-readable description:properties- List of property definitions:vectorizer- Vectorizer module to use (e.g., "text2vec-openai", "none"):vectorIndexType- Vector index type (default: "hnsw"):vectorIndexConfig- Vector index configuration:invertedIndexConfig- Inverted index configuration:replicationConfig- Replication settings:multiTenancyConfig- Multi-tenancy settings:multi_tenancy_config- Typed multi-tenancy config (WeaviateEx.Schema.MultiTenancyConfig):object_ttl- Object TTL config (WeaviateEx.Config.ObjectTTL):auto_tenant- Auto-tenant config (WeaviateEx.Config.AutoTenant)
Examples
iex> WeaviateEx.Collections.create("Article", %{
...> properties: [
...> %{name: "title", dataType: ["text"]},
...> %{name: "content", dataType: ["text"]}
...> ],
...> vectorizer: "none"
...> })
{:ok, %{"class" => "Article", ...}}
@spec delete(collection_name(), Keyword.t()) :: WeaviateEx.api_response()
Deletes a collection and all its objects.
Warning: This operation is irreversible and will delete all data in the collection.
Examples
iex> WeaviateEx.Collections.delete("Article")
{:ok, %{}}
@spec delete_all(Keyword.t()) :: {:ok, keyword()} | {:error, WeaviateEx.Error.t()}
Deletes all collections from the schema.
This operation lists all collections and then deletes each one. Returns a result with the count of successfully deleted collections.
Warning: This operation is irreversible and will delete all data in all collections.
Options
- All standard connection options (
:base_url,:api_key, etc.)
Return Value
Returns {:ok, keyword()} with the following keys:
:deleted_count- Number of collections successfully deleted:failed_count- Number of collections that failed to delete (only present if > 0):failures- List of failure details (only present if there were failures)
Examples
# Delete all collections
{:ok, deleted_count: 3} = WeaviateEx.Collections.delete_all()
# With partial failures
{:ok, [deleted_count: 2, failed_count: 1, failures: [...]]} = WeaviateEx.Collections.delete_all()
# Handle connection errors
{:error, %WeaviateEx.Error{type: :connection_error}} = WeaviateEx.Collections.delete_all()
@spec exists?(collection_name(), Keyword.t()) :: {:ok, boolean()} | {:error, term()}
Checks whether a collection exists.
Returns {:ok, true} when the collection is present, {:ok, false} when the
server returns a not-found response, or {:error, %WeaviateEx.Error{}} if another
error occurs.
@spec get(collection_name(), Keyword.t()) :: WeaviateEx.api_response()
Gets a specific collection by name.
Parameters
name- The name of the collectionopts- Additional options
Examples
iex> WeaviateEx.Collections.get("Article")
{:ok, %{"class" => "Article", "properties" => [...]}}
@spec get_shards(collection_name(), Keyword.t()) :: WeaviateEx.api_response()
Gets the shards for a collection.
Shards are used in distributed setups to partition data.
Examples
iex> WeaviateEx.Collections.get_shards("Article")
{:ok, [...]}
@spec get_tenants(collection_name(), Keyword.t()) :: WeaviateEx.api_response()
Gets tenants for a multi-tenant collection.
Examples
iex> WeaviateEx.Collections.get_tenants("Article")
{:ok, [...]}
@spec insert_many(collection_name(), [map()], Keyword.t()) :: WeaviateEx.api_response()
Insert multiple objects into a collection in a single batch operation.
This is a convenience wrapper around batch insert that automatically adds the collection name to each object.
Parameters
collection_name- Name of the collectionobjects- List of property maps or object maps with optional:uuid,:vectoropts- Additional options
Options
:tenant- Tenant name for multi-tenant collections:consistency_level- Consistency level for the operation:return_summary- Return a summary with success/failure counts
Object Format
Each object can be:
- A simple properties map:
%{title: "My Article", content: "..."} - A map with
:properties, optional:uuid,:vector:%{properties: %{...}, uuid: "..."}
Examples
# Simple properties
{:ok, result} = Collections.insert_many("Article", [
%{title: "Article 1", content: "Content 1"},
%{title: "Article 2", content: "Content 2"}
])
# With custom UUIDs
{:ok, result} = Collections.insert_many("Article", [
%{properties: %{title: "Article 1"}, uuid: "custom-uuid-1"},
%{properties: %{title: "Article 2"}, uuid: "custom-uuid-2"}
])
# With tenant
{:ok, result} = Collections.insert_many("Article", objects, tenant: "tenant-a")
# Get summary
{:ok, summary} = Collections.insert_many("Article", objects, return_summary: true)
@spec list(Keyword.t()) :: WeaviateEx.api_response()
Lists all collections in the schema.
Examples
iex> WeaviateEx.Collections.list()
{:ok, %{"classes" => [...]}}
@spec remove_tenants(collection_name(), [String.t()], Keyword.t()) :: WeaviateEx.api_response()
Removes tenants from a multi-tenant collection.
Examples
iex> WeaviateEx.Collections.remove_tenants("Article", ["tenant1", "tenant2"])
{:ok, %{}}
@spec set_multi_tenancy(collection_name(), boolean(), Keyword.t()) :: WeaviateEx.api_response()
Enable or disable multi-tenancy for a collection.
Examples
iex> WeaviateEx.Collections.set_multi_tenancy("Article", true)
{:ok, %{"enabled" => true}}
@spec update(collection_name(), collection_config(), Keyword.t()) :: WeaviateEx.api_response()
Updates an existing collection.
Note: Not all fields can be updated after creation. Check Weaviate documentation for updateable fields.
Examples
iex> WeaviateEx.Collections.update("Article", %{
...> description: "Updated description"
...> })
{:ok, %{"class" => "Article", ...}}
@spec update_shard(collection_name(), String.t(), String.t(), Keyword.t()) :: WeaviateEx.api_response()
Updates a shard status.
Parameters
collection_name- The name of the collectionshard_name- The name of the shardstatus- New status ("READY", "READONLY")opts- Additional options
Examples
iex> WeaviateEx.Collections.update_shard("Article", "shard-1", "READONLY")
{:ok, %{"status" => "READONLY"}}
@spec update_ttl(collection_name(), WeaviateEx.Config.ObjectTTL.t(), Keyword.t()) :: WeaviateEx.api_response()
Updates the Object TTL configuration for an existing collection.
This is a convenience wrapper around update/3 that specifically
updates the TTL configuration.
Parameters
name- The name of the collectionttl- AnObjectTTLstruct with the new TTL configurationopts- Additional options
Examples
# Enable 24-hour TTL
alias WeaviateEx.Config.ObjectTTL
{:ok, _} = WeaviateEx.Collections.update_ttl("Events",
ObjectTTL.from_duration(hours: 24)
)
# Change to 7-day TTL
{:ok, _} = WeaviateEx.Collections.update_ttl("Events",
ObjectTTL.from_duration(days: 7)
)
# Disable TTL
{:ok, _} = WeaviateEx.Collections.update_ttl("Events",
ObjectTTL.disable()
)
@spec with_tenant(WeaviateEx.Client.t(), String.t(), String.t()) :: WeaviateEx.TenantCollection.t()
Returns a tenant-scoped collection reference.
This provides a Python-like with_tenant pattern for cleaner multi-tenant
code. All operations on the returned TenantCollection are automatically
scoped to the specified tenant.
Parameters
client- WeaviateEx.Client instancecollection- Collection nametenant- Tenant name
Examples
# Get tenant-scoped collection
tenant_col = Collections.with_tenant(client, "Articles", "tenant_A")
# All operations automatically scoped to tenant_A
{:ok, _} = TenantCollection.insert(tenant_col, %{
title: "My Article",
content: "Article content"
})
# Query within tenant
{:ok, results} = tenant_col
|> TenantCollection.query()
|> Query.bm25("search term")
|> Query.execute(client)
# Batch insert within tenant
{:ok, _} = TenantCollection.insert_many(tenant_col, [
%{title: "Article 1"},
%{title: "Article 2"}
])Returns
A WeaviateEx.TenantCollection struct that can be used with all
TenantCollection functions.