WeaviateEx.TenantCollection (WeaviateEx v0.7.4)

View Source

A tenant-scoped collection reference for fluent multi-tenant operations.

This module provides a Python-like with_tenant experience, allowing you to get a tenant-scoped collection reference where all operations are automatically scoped to the specified tenant.

Usage

# Get tenant-scoped collection
tenant_col = WeaviateEx.Collections.with_tenant(client, "Articles", "tenant_A")

# All operations automatically scoped to tenant_A
{:ok, obj} = WeaviateEx.TenantCollection.insert(tenant_col, %{title: "Hello"})
{:ok, results} = tenant_col
  |> WeaviateEx.TenantCollection.query()
  |> WeaviateEx.Query.bm25("search term")
  |> WeaviateEx.Query.execute(client)

Alternative API

You can also use the existing TenantClient module which provides a similar fluent API but with a different method chaining style:

tenant_client = client
  |> WeaviateEx.TenantClient.with_tenant("tenant_A")
  |> WeaviateEx.TenantClient.collection("Articles")

Comparison with Python Client

Python:

tenant_col = collection.with_tenant("tenant_A")
tenant_col.data.insert({"name": "test"})
tenant_col.query.bm25("query")

Elixir:

tenant_col = Collections.with_tenant(client, "Articles", "tenant_A")
TenantCollection.insert(tenant_col, %{name: "test"})
tenant_col |> TenantCollection.query() |> Query.bm25("query")

Summary

Functions

Starts a background batch processor for the tenant's collection.

Returns the underlying client.

Returns the collection name.

Deletes an object from the tenant's collection.

Checks if an object exists in the tenant's collection.

Gets an object by UUID from the tenant's collection.

Inserts an object into the tenant's collection.

Inserts multiple objects into the tenant's collection.

Creates a new tenant-scoped collection reference.

Creates a query builder for the tenant's collection.

Replaces an object in the tenant's collection (alias for update).

Returns the tenant name.

Updates an object in the tenant's collection (full replacement).

Creates a batch context for the tenant's collection.

Types

t()

@type t() :: %WeaviateEx.TenantCollection{
  client: WeaviateEx.Client.t(),
  collection: String.t(),
  tenant: String.t()
}

Functions

batch(tc, opts \\ [])

@spec batch(
  t(),
  keyword()
) :: {:ok, pid()} | {:error, term()}

Starts a background batch processor for the tenant's collection.

Returns a batch processor that runs asynchronously. Use Batch.Background functions to add objects and stop the processor.

Options

  • :batch_size - Objects per batch (default: 100)
  • :concurrent_requests - Max concurrent requests (default: 2)
  • :flush_interval - Auto-flush interval in ms (default: 1000)

Examples

{:ok, batcher} = TenantCollection.batch(tenant_col, batch_size: 100)

for article <- articles do
  :ok = Batch.Background.add_object(batcher, %{
    class: tenant_col.collection,
    properties: article,
    tenant: tenant_col.tenant
  })
end

results = Batch.Background.stop(batcher, flush: true)

client(tenant_collection)

@spec client(t()) :: WeaviateEx.Client.t()

Returns the underlying client.

Examples

client = TenantCollection.client(tenant_col)

collection_name(tenant_collection)

@spec collection_name(t()) :: String.t()

Returns the collection name.

Examples

"Articles" = TenantCollection.collection_name(tenant_col)

delete(tc, uuid, opts \\ [])

@spec delete(t(), String.t(), keyword()) :: :ok | {:ok, map()} | {:error, term()}

Deletes an object from the tenant's collection.

Parameters

  • tc - TenantCollection reference
  • uuid - Object UUID
  • opts - Additional options

Examples

:ok = TenantCollection.delete(tenant_col, "550e8400-e29b-41d4-a716-446655440000")

exists?(tc, uuid, opts \\ [])

@spec exists?(t(), String.t(), keyword()) :: {:ok, boolean()} | {:error, term()}

Checks if an object exists in the tenant's collection.

Parameters

  • tc - TenantCollection reference
  • uuid - Object UUID
  • opts - Additional options

Examples

{:ok, true} = TenantCollection.exists?(tenant_col, uuid)
{:ok, false} = TenantCollection.exists?(tenant_col, "non-existent-uuid")

get(tc, uuid, opts \\ [])

@spec get(t(), String.t(), keyword()) :: {:ok, map()} | {:error, term()}

Gets an object by UUID from the tenant's collection.

Parameters

  • tc - TenantCollection reference
  • uuid - Object UUID
  • opts - Additional options

Options

  • :include - Additional fields to include (e.g., "vector", "classification")
  • :consistency_level - Consistency level for the operation

Examples

{:ok, object} = TenantCollection.get(tenant_col, "550e8400-e29b-41d4-a716-446655440000")

# Include vector
{:ok, object} = TenantCollection.get(tenant_col, uuid, include: "vector")

insert(tc, object, opts \\ [])

@spec insert(t(), map(), keyword()) :: {:ok, map()} | {:error, term()}

Inserts an object into the tenant's collection.

Parameters

  • tc - TenantCollection reference
  • object - Object properties to insert
  • opts - Additional options

Options

  • :uuid - Custom UUID for the object
  • :vector - Custom vector for the object
  • :consistency_level - Consistency level for the operation

Examples

{:ok, result} = TenantCollection.insert(tenant_col, %{
  title: "My Article",
  content: "Article content"
})

# With custom UUID
{:ok, result} = TenantCollection.insert(tenant_col, %{title: "Test"},
  uuid: "550e8400-e29b-41d4-a716-446655440000"
)

insert_many(tc, objects, opts \\ [])

@spec insert_many(t(), [map()], keyword()) :: {:ok, term()} | {:error, term()}

Inserts multiple objects into the tenant's collection.

Uses batch operations for efficient bulk inserts.

Parameters

  • tc - TenantCollection reference
  • objects - List of object property maps
  • opts - Additional options

Options

  • :consistency_level - Consistency level for the operation
  • :return_summary - If true, returns a summary with success/failure counts

Examples

{:ok, result} = TenantCollection.insert_many(tenant_col, [
  %{title: "Article 1"},
  %{title: "Article 2"},
  %{title: "Article 3"}
])

# Get summary
{:ok, summary} = TenantCollection.insert_many(tenant_col, objects,
  return_summary: true
)

new(client, collection, tenant)

@spec new(WeaviateEx.Client.t(), String.t(), String.t()) :: t()

Creates a new tenant-scoped collection reference.

Parameters

  • client - WeaviateEx.Client instance
  • collection - Collection name
  • tenant - Tenant name

Examples

tenant_col = TenantCollection.new(client, "Articles", "tenant_A")

query(tc)

@spec query(t()) :: WeaviateEx.Query.t()

Creates a query builder for the tenant's collection.

The returned query is automatically scoped to the tenant. Chain additional query methods and execute with Query.execute/2.

Examples

# Simple query
{:ok, results} = tenant_col
  |> TenantCollection.query()
  |> Query.limit(10)
  |> Query.execute(tenant_col.client)

# BM25 search
{:ok, results} = tenant_col
  |> TenantCollection.query()
  |> Query.bm25("search term")
  |> Query.fields(["title", "content"])
  |> Query.execute(tenant_col.client)

# Vector search
{:ok, results} = tenant_col
  |> TenantCollection.query()
  |> Query.near_text("machine learning", certainty: 0.7)
  |> Query.limit(5)
  |> Query.execute(tenant_col.client)

# Hybrid search
{:ok, results} = tenant_col
  |> TenantCollection.query()
  |> Query.hybrid("AI research", alpha: 0.7)
  |> Query.execute(tenant_col.client)

replace(tc, uuid, properties, opts \\ [])

@spec replace(t(), String.t(), map(), keyword()) :: {:ok, map()} | {:error, term()}

Replaces an object in the tenant's collection (alias for update).

Examples

{:ok, replaced} = TenantCollection.replace(tenant_col, uuid, %{
  title: "Replaced Title"
})

tenant_name(tenant_collection)

@spec tenant_name(t()) :: String.t()

Returns the tenant name.

Examples

"tenant_A" = TenantCollection.tenant_name(tenant_col)

update(tc, uuid, properties, opts \\ [])

@spec update(t(), String.t(), map(), keyword()) :: {:ok, map()} | {:error, term()}

Updates an object in the tenant's collection (full replacement).

This performs a PUT request which replaces the entire object.

Parameters

  • tc - TenantCollection reference
  • uuid - Object UUID
  • properties - New object properties
  • opts - Additional options

Options

  • :consistency_level - Consistency level for the operation
  • :keep_vector - If true, keeps the existing vector

Examples

{:ok, updated} = TenantCollection.update(tenant_col, uuid, %{
  title: "Updated Title",
  content: "Updated Content"
})

with_batch(tc, opts, fun)

@spec with_batch(t(), keyword(), (WeaviateEx.Batch.batch_context() ->
                              WeaviateEx.Batch.batch_context())) ::
  {:ok, term()} | {:error, term()}

Creates a batch context for the tenant's collection.

Returns a batch context that can be used with Batch.add_object/4 and related functions. All objects added will be scoped to the tenant.

Options

  • :mode - Batch mode: :fixed (default), :dynamic, or :rate_limited
  • :batch_size - Objects per batch (default: 100)
  • Other options passed to Batch.with_batch/3

Examples

# Context manager style
{:ok, results} = TenantCollection.with_batch(tenant_col, [batch_size: 100], fn batch ->
  batch
  |> Batch.add_object(tenant_col.collection, %{title: "Article 1"}, tenant: tenant_col.tenant)
  |> Batch.add_object(tenant_col.collection, %{title: "Article 2"}, tenant: tenant_col.tenant)
end)

See batch_insert/3 for a simpler bulk insert API.