WeaviateEx.Client (WeaviateEx v0.7.4)

View Source

WeaviateEx client with hybrid gRPC/HTTP support.

The client uses:

  • gRPC for data operations (search, batch, aggregate, tenants)
  • HTTP for schema operations (collections management)

This hybrid approach is necessary because Weaviate's gRPC API doesn't include schema management operations.

Usage

# Connect to local Weaviate
{:ok, client} = WeaviateEx.Client.connect(base_url: "http://localhost:8080")

# Connect to Weaviate Cloud
{:ok, client} = WeaviateEx.Client.connect(
  base_url: "https://my-cluster.weaviate.network",
  api_key: "your-api-key"
)

# Use the client
{:ok, results} = WeaviateEx.Query.near_text(client, "Article", "machine learning")

# Disconnect when done
:ok = WeaviateEx.Client.disconnect(client)

Summary

Functions

Close the client and release all connections.

Check if client has been closed.

Connect to a Weaviate instance.

Disconnect the client.

Execute a GraphQL query against Weaviate.

Get the gRPC channel from the client.

Check if the client has an active gRPC connection.

Build gRPC metadata from client config.

Create a new client without establishing connections.

Make an HTTP request using the configured protocol.

Get client statistics.

Get current client status.

Execute a function with an auto-managed client.

Types

t()

@type t() :: %WeaviateEx.Client{
  config: WeaviateEx.Client.Config.t(),
  grpc_channel: GRPC.Channel.t() | nil,
  protocol_impl: module(),
  state: WeaviateEx.Client.State.t()
}

Functions

close(client)

@spec close(t()) :: :ok

Close the client and release all connections.

This marks the client as closed and disconnects any active connections. Once closed, a client cannot be reused - create a new client instead.

Examples

:ok = Client.close(client)

closed?(client)

@spec closed?(t()) :: boolean()

Check if client has been closed.

Examples

false = Client.closed?(client)
Client.close(client)
true = Client.closed?(client)

connect(opts \\ [])

@spec connect(keyword()) :: {:ok, t()} | {:error, WeaviateEx.Error.t()}

Connect to a Weaviate instance.

Establishes both gRPC channel (for data operations) and HTTP client (for schema operations).

Options

  • :base_url - HTTP base URL (default: "http://localhost:8080")
  • :grpc_host - gRPC host (default: derived from base_url)
  • :grpc_port - gRPC port (default: 50051, or 443 for HTTPS)
  • :api_key - API key for authentication
  • :auth - Authentication config (WeaviateEx.Auth) for API key, bearer token, or OIDC
  • :timeout - Connection timeout in milliseconds (default: 30000)
  • :skip_grpc - Skip gRPC connection (use HTTP only)
  • :skip_init_checks - Skip meta/version/gRPC health checks (default: false)
  • :connection - Connection pool settings (WeaviateEx.Config.Connection or keyword list)
  • :proxy - Proxy settings (WeaviateEx.Config.Proxy, keyword list, or :env)

Examples

{:ok, client} = WeaviateEx.Client.connect(
  base_url: "http://localhost:8080",
  api_key: "secret-key"
)

disconnect(client)

@spec disconnect(t()) :: :ok

Disconnect the client.

Closes the gRPC channel if one is established.

Examples

:ok = WeaviateEx.Client.disconnect(client)

graphql(client, query)

@spec graphql(t(), String.t()) :: WeaviateEx.Protocol.response()

Execute a GraphQL query against Weaviate.

Used for queries that require GraphQL features not available in gRPC, such as generative search.

Examples

{:ok, response} = WeaviateEx.Client.graphql(client, "{ Get { Article { title } } }")

grpc_channel(client)

@spec grpc_channel(t()) :: GRPC.Channel.t() | nil

Get the gRPC channel from the client.

Returns nil if no gRPC channel is established.

Examples

channel = WeaviateEx.Client.grpc_channel(client)

grpc_connected?(client)

@spec grpc_connected?(t()) :: boolean()

Check if the client has an active gRPC connection.

Examples

true = WeaviateEx.Client.grpc_connected?(client)

grpc_metadata(client)

@spec grpc_metadata(t()) :: map()

Build gRPC metadata from client config.

Includes authorization header if API key is set.

Examples

metadata = WeaviateEx.Client.grpc_metadata(client)

new(opts \\ [])

@spec new(keyword()) :: {:ok, t()}

Create a new client without establishing connections.

This is a lightweight alternative to connect/1 that doesn't establish the gRPC channel upfront. Useful for testing or when you need to configure the client before connecting.

Examples

{:ok, client} = WeaviateEx.Client.new(
  base_url: "http://localhost:8080",
  api_key: "secret-key"
)

request(client, method, path, body, opts)

Make an HTTP request using the configured protocol.

Used for schema/collection operations which don't have gRPC equivalents.

Examples

{:ok, response} = WeaviateEx.Client.request(client, :get, "/v1/schema", nil, [])

stats(client)

@spec stats(t()) :: WeaviateEx.Client.State.t()

Get client statistics.

Returns state information including request counts, error counts, and timestamps.

Examples

stats = Client.stats(client)
IO.puts("Requests: #{stats.request_count}")

status(client)

@spec status(t()) :: WeaviateEx.Client.State.status()

Get current client status.

Returns one of:

  • :initializing - Client is being set up
  • :connected - Client is connected and ready
  • :disconnected - Client is disconnected but not closed
  • :closed - Client has been closed and cannot be reused

Examples

:connected = Client.status(client)

with_client(opts, fun)

@spec with_client(
  keyword(),
  (t() -> result)
) :: result
when result: term()

Execute a function with an auto-managed client.

Creates a client, executes the function, and ensures cleanup even if the function raises an error.

Examples

result = Client.with_client([base_url: url], fn client ->
  WeaviateEx.Objects.list(client, "Article")
end)