WeaviateEx.Objects (WeaviateEx v0.7.4)

View Source

Functions for managing individual objects in Weaviate.

Objects are data records stored in collections with optional vector embeddings.

Examples

# Create an object
{:ok, object} = WeaviateEx.Objects.create("Article", %{
  properties: %{
    title: "My Article",
    content: "Article content"
  },
  vector: [0.1, 0.2, 0.3, ...]  # Optional
})

# Get an object
{:ok, object} = WeaviateEx.Objects.get("Article", uuid)

# List objects
{:ok, objects} = WeaviateEx.Objects.list("Article", limit: 10)

# Fetch an object with tenant scoping and _additional metadata
{:ok, object} =
  WeaviateEx.Objects.get("Article", uuid, tenant: "tenant-a", include: ["_additional"])

# Update an object (full replacement using PUT)
{:ok, updated} = WeaviateEx.Objects.update("Article", uuid, %{
  properties: %{title: "Updated Title"}
})

# Replace an object (semantic alias for update, uses PUT)
{:ok, replaced} = WeaviateEx.Objects.replace("Article", uuid, %{
  properties: %{title: "Replaced Title", content: "New Content"}
})

# Patch an object (partial update using PATCH)
{:ok, patched} = WeaviateEx.Objects.patch("Article", uuid, %{
  properties: %{title: "New Title"}
})

# Delete an object
{:ok, _} = WeaviateEx.Objects.delete("Article", uuid)

# Check if object exists (using HEAD request)
{:ok, true} = WeaviateEx.Objects.exists?("Article", uuid)

Summary

Functions

Creates a new object in a collection.

Checks if an object exists (using HEAD request).

Fetch multiple objects by their UUIDs.

Retrieves an object by collection name and ID.

Lists objects from a collection.

Patches an object (partial update).

Replaces an object entirely (full replacement using PUT).

Updates an object (full replacement).

Validates an object without creating it.

Types

collection_name()

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

object_data()

@type object_data() :: map()

object_id()

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

Functions

create(collection_name, data, opts \\ [])

Creates a new object in a collection.

Parameters

  • collection_name - The collection to create the object in
  • data - Object data including properties and optional vector
  • opts - Additional options (consistency_level, etc.)

Data Fields

  • :id - Optional UUID (auto-generated if not provided)
  • :properties - Object properties matching the collection schema
  • :vector - Optional vector embedding (if not using auto-vectorization)

Examples

# Simple object
{:ok, obj} = Objects.create("Article", %{
  properties: %{title: "Hello"}
})

# With custom ID and vector
{:ok, obj} = Objects.create("Article", %{
  id: "550e8400-e29b-41d4-a716-446655440000",
  properties: %{title: "Hello"},
  vector: [0.1, 0.2, 0.3]
})

delete(collection_name, id, opts \\ [])

Deletes an object.

Examples

{:ok, _} = Objects.delete("Article", uuid)

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

@spec exists?(collection_name(), object_id(), Keyword.t()) ::
  {:ok, boolean()} | {:error, term()}

Checks if an object exists (using HEAD request).

Returns {:ok, true} if the object exists (204 status). Returns {:error, ...} if the object doesn't exist (404 status).

Examples

case Objects.exists?("Article", uuid) do
  {:ok, true} -> IO.puts("Object exists")
  {:error, %{status: 404}} -> IO.puts("Object not found")
end

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

@spec fetch_objects_by_ids(collection_name(), [object_id()], Keyword.t()) ::
  WeaviateEx.api_response()

Fetch multiple objects by their UUIDs.

Uses a GraphQL query with a ContainsAny filter and preserves input ordering.

Options

  • :return_properties - List of property names to return (default: all)
  • :tenant - Tenant name for multi-tenant collections

get(collection_name, id, opts \\ [])

Retrieves an object by collection name and ID.

Parameters

  • collection_name - The collection containing the object
  • id - The object UUID
  • opts - Additional options (consistency_level, tenant, include)

Examples

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

list(collection_name, opts \\ [])

Lists objects from a collection.

Parameters

  • collection_name - The collection to list objects from
  • opts - Pagination and filtering options

Options

  • :limit - Maximum number of objects to return
  • :offset - Number of objects to skip
  • :after - Cursor for pagination
  • :include - Additional fields to include (e.g., "vector", "classification")
  • :sort - Sort order
  • :order - Sort direction ("asc" or "desc")

Examples

# Get first 10 objects
{:ok, result} = Objects.list("Article", limit: 10)

# With pagination
{:ok, result} = Objects.list("Article", limit: 10, offset: 20)

# Include vectors
{:ok, result} = Objects.list("Article", limit: 10, include: "vector")

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

Patches an object (partial update).

This performs a PATCH request which merges changes with existing data. After patching, the updated object is fetched and returned.

NOTE: PATCH operations should not include vectors. If you need to update the vector, use update/4 (PUT) instead which replaces the entire object.

Examples

{:ok, patched} = Objects.patch("Article", uuid, %{
  properties: %{title: "Updated Title"}
})

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

Replaces an object entirely (full replacement using PUT).

This is a semantic alias for update/4 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

  • collection_name - The collection containing the object
  • id - The object UUID
  • data - Complete object data to replace with
  • opts - Additional options (consistency_level, tenant, keep_vector)

Options

  • :consistency_level - Write consistency level ("ONE", "QUORUM", "ALL")
  • :tenant - Tenant name for multi-tenant collections
  • :keep_vector - If true, keeps the existing vector (default: false)

Examples

# Replace entire object
{:ok, replaced} = Objects.replace("Article", uuid, %{
  properties: %{title: "New Title", content: "New Content"}
})

# Replace with new vector
{:ok, replaced} = Objects.replace("Article", uuid, %{
  properties: %{title: "New Title"},
  vector: [0.1, 0.2, 0.3]
})

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

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

Updates an object (full replacement).

This performs a PUT request which replaces the entire object.

Examples

{:ok, updated} = Objects.update("Article", uuid, %{
  properties: %{title: "New Title", content: "New Content"}
})

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

Validates an object without creating it.

Useful for checking if object data is valid before insertion.

Examples

{:ok, result} = Objects.validate("Article", %{
  properties: %{title: "Test"}
})