WeaviateEx.API.Data (WeaviateEx v0.7.4)

View Source

Data operations API for CRUD operations on objects.

This module provides a clean, protocol-based API for managing data objects in Weaviate collections with support for:

  • Custom UUIDs
  • Vector embeddings
  • Multi-tenancy
  • Consistency levels
  • Named vectors (future)

Examples

# Create an object
{:ok, object} = Data.insert(client, "Article", %{
  properties: %{"title" => "Hello", "content" => "World"}
})

# Create with custom UUID
{:ok, object} = Data.insert(client, "Article", %{
  id: "550e8400-e29b-41d4-a716-446655440000",
  properties: %{"title" => "Hello"}
})

# Create with vector
{:ok, object} = Data.insert(client, "Article", %{
  properties: %{"title" => "Hello"},
  vector: [0.1, 0.2, 0.3]
})

# Get object
{:ok, object} = Data.get_by_id(client, "Article", uuid)

# Update (full replacement)
{:ok, updated} = Data.update(client, "Article", uuid, %{
  properties: %{"title" => "Updated"}
})

# Patch (partial update)
{:ok, patched} = Data.patch(client, "Article", uuid, %{
  properties: %{"title" => "Patched"}
})

# Delete
{:ok, _} = Data.delete_by_id(client, "Article", uuid)

# Check existence
{:ok, true} = Data.exists?(client, "Article", uuid)

# Validate before insert
{:ok, result} = Data.validate(client, "Article", %{
  properties: %{"title" => "Test"}
})

Summary

Functions

Insert a new object into a collection.

Patch an object (partial update).

Replace an object entirely (full replacement using PUT).

Update an object (full replacement).

Validate object data without creating it.

Types

collection_name()

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

object_data()

@type object_data() :: map()

object_id()

@type object_id() :: String.t()

opts()

@type opts() :: keyword()

Functions

delete_by_id(client, collection_name, id, opts \\ [])

@spec delete_by_id(WeaviateEx.Client.t(), collection_name(), object_id(), opts()) ::
  {:ok, map()} | {:error, WeaviateEx.Error.t()}

Delete an object by its UUID.

Parameters

  • client - WeaviateEx client
  • collection_name - Name of the collection
  • id - Object UUID
  • opts - Options (:tenant, :consistency_level)

Examples

Data.delete_by_id(client, "Article", uuid)

Data.delete_by_id(client, "Article", uuid, tenant: "TenantA")

Returns

  • {:ok, map()} - Empty map on success
  • {:error, Error.t()} - Error if deletion fails

exists?(client, collection_name, id, opts \\ [])

@spec exists?(WeaviateEx.Client.t(), collection_name(), object_id(), opts()) ::
  {:ok, boolean()}

Check if an object exists.

Parameters

  • client - WeaviateEx client
  • collection_name - Name of the collection
  • id - Object UUID
  • opts - Options (:tenant, :consistency_level)

Examples

Data.exists?(client, "Article", uuid)
# => {:ok, true}

Data.exists?(client, "Article", "non-existent")
# => {:ok, false}

Returns

  • {:ok, boolean()} - True if exists, false otherwise

fetch_objects_by_ids(client, collection_name, ids, opts \\ [])

@spec fetch_objects_by_ids(
  WeaviateEx.Client.t(),
  collection_name(),
  [object_id()],
  opts()
) ::
  {:ok, [map()]} | {:error, WeaviateEx.Error.t()}

Fetch multiple objects by their UUIDs.

Uses a GraphQL query with ContainsAny filter for efficient batch retrieval.

Parameters

  • client - WeaviateEx client
  • collection_name - Name of the collection
  • ids - List of object UUIDs to fetch
  • opts - Options:
    • :return_properties - List of property names to return (default: all)
    • :tenant - Tenant name for multi-tenant collections

Examples

ids = ["uuid-1", "uuid-2", "uuid-3"]
{:ok, objects} = Data.fetch_objects_by_ids(client, "Article", ids)

# With specific properties
{:ok, objects} = Data.fetch_objects_by_ids(client, "Article", ids,
  return_properties: ["title", "content"]
)

# With tenant
{:ok, objects} = Data.fetch_objects_by_ids(client, "Article", ids,
  tenant: "tenant-a"
)

Returns

  • {:ok, list()} - List of matching objects
  • {:error, Error.t()} - Error if query fails

get_by_id(client, collection_name, id, opts \\ [])

@spec get_by_id(WeaviateEx.Client.t(), collection_name(), object_id(), opts()) ::
  {:ok, map()} | {:error, WeaviateEx.Error.t()}

Get an object by its UUID.

Parameters

  • client - WeaviateEx client
  • collection_name - Name of the collection
  • id - Object UUID
  • opts - Options (:tenant, :consistency_level, :include)

Examples

Data.get_by_id(client, "Article", "550e8400-e29b-41d4-a716-446655440000")

Data.get_by_id(client, "Article", uuid,
  tenant: "TenantA",
  include: "vector"
)

Returns

  • {:ok, object} - Retrieved object
  • {:error, Error.t()} - Error if not found

insert(client, collection_name, data, opts \\ [])

@spec insert(WeaviateEx.Client.t(), collection_name(), object_data(), opts()) ::
  {:ok, map()} | {:error, WeaviateEx.Error.t()}

Insert a new object into a collection.

Parameters

  • client - WeaviateEx client
  • collection_name - Name of the collection
  • data - Object data map with :properties and optionally :id, :vector
  • opts - Options (:tenant, :consistency_level)

Examples

Data.insert(client, "Article", %{
  properties: %{"title" => "Hello"}
})

Data.insert(client, "Article", %{
  id: "550e8400-e29b-41d4-a716-446655440000",
  properties: %{"title" => "Hello"},
  vector: [0.1, 0.2, 0.3]
}, tenant: "TenantA")

Returns

  • {:ok, object} - Created object with UUID
  • {:error, Error.t()} - Error if creation fails

patch(client, collection_name, id, data, opts \\ [])

@spec patch(
  WeaviateEx.Client.t(),
  collection_name(),
  object_id(),
  object_data(),
  opts()
) ::
  {:ok, map()} | {:error, WeaviateEx.Error.t()}

Patch an object (partial update).

This merges changes with existing data.

Parameters

  • client - WeaviateEx client
  • collection_name - Name of the collection
  • id - Object UUID
  • data - Partial object data
  • opts - Options (:tenant, :consistency_level)

Examples

Data.patch(client, "Article", uuid, %{
  properties: %{"title" => "Updated Title"}
})

Returns

  • {:ok, object} - Updated object (retrieved after patch)
  • {:error, Error.t()} - Error if patch fails

replace(client, collection_name, id, data, opts \\ [])

@spec replace(
  WeaviateEx.Client.t(),
  collection_name(),
  object_id(),
  object_data(),
  opts()
) ::
  {:ok, map()} | {:error, WeaviateEx.Error.t()}

Replace an object entirely (full replacement using PUT).

This is a semantic alias for update/5 that makes the intent clearer. Use this when you want to replace the entire object with new data.

All existing properties will be replaced with the new data. Properties not included in the new data will be removed.

Parameters

  • client - WeaviateEx client
  • collection_name - Name of the collection
  • id - Object UUID
  • data - Complete object data to replace with
  • opts - Options (:tenant, :consistency_level, :keep_vector)

Examples

Data.replace(client, "Article", uuid, %{
  properties: %{"title" => "New Title", "content" => "New Content"}
})

# Replace but keep existing vector
Data.replace(client, "Article", uuid, %{
  properties: %{"title" => "New Title"}
}, keep_vector: true)

Returns

  • {:ok, object} - Replaced object
  • {:error, Error.t()} - Error if replace fails

update(client, collection_name, id, data, opts \\ [])

@spec update(
  WeaviateEx.Client.t(),
  collection_name(),
  object_id(),
  object_data(),
  opts()
) ::
  {:ok, map()} | {:error, WeaviateEx.Error.t()}

Update an object (full replacement).

This replaces the entire object with new data.

Parameters

  • client - WeaviateEx client
  • collection_name - Name of the collection
  • id - Object UUID
  • data - New object data
  • opts - Options (:tenant, :consistency_level)

Examples

Data.update(client, "Article", uuid, %{
  properties: %{"title" => "New Title", "content" => "New Content"}
})

Returns

  • {:ok, object} - Updated object
  • {:error, Error.t()} - Error if update fails

validate(client, collection_name, data, opts \\ [])

@spec validate(WeaviateEx.Client.t(), collection_name(), object_data(), opts()) ::
  {:ok, map()} | {:error, WeaviateEx.Error.t()}

Validate object data without creating it.

Useful for checking if object data is valid before insertion.

Parameters

  • client - WeaviateEx client
  • collection_name - Name of the collection
  • data - Object data to validate
  • opts - Options

Examples

Data.validate(client, "Article", %{
  properties: %{"title" => "Test"}
})

Returns

  • {:ok, result} - Validation result
  • {:error, Error.t()} - Validation error