WeaviateEx.Query.Generate (WeaviateEx v0.7.4)

View Source

Generative search query builder.

Combines vector/keyword search with generative AI to produce both search results and AI-generated content in a single query.

Features

  • All search types: near_text, near_vector, near_object, bm25, hybrid
  • Single prompt: Generate content for each object individually
  • Grouped task: Generate content from all results together
  • Full filter, limit, offset support

Examples

# Vector search with per-object generation
Generate.new("Article")
|> Generate.near_text("machine learning")
|> Generate.single_prompt("Summarize this article: {title}")
|> Generate.limit(5)
|> Generate.execute(client)

# BM25 search with grouped generation
Generate.new("Article")
|> Generate.bm25("elixir")
|> Generate.grouped_task("Write a summary of these articles", properties: ["title", "content"])
|> Generate.execute(client)

# Hybrid search with both single and grouped prompts
Generate.new("Article")
|> Generate.hybrid("AI research", alpha: 0.7)
|> Generate.single_prompt("Key point: {title}")
|> Generate.grouped_task("Overall theme")
|> Generate.return_properties(["title", "content"])
|> Generate.execute(client)

Summary

Functions

Sets additional metadata fields to return.

Adds BM25 keyword search to the query.

Executes the generative query against Weaviate.

Sets the grouped task for generation across all results.

Adds hybrid search to the query.

Sets the maximum number of results to return.

Adds near_object search to the query.

Adds near_text search to the query.

Adds near_vector search to the query.

Creates a new generative query builder for a collection.

Sets the number of results to skip.

Parses an API response into a GenerativeResult struct.

Sets the properties to return from the search.

Sets the single prompt for per-object generation.

Sets the tenant for multi-tenant collections.

Converts the builder to a GraphQL query string.

Validates the query builder has required fields.

Sets a filter condition for the query.

Types

search_type()

@type search_type() :: :near_text | :near_vector | :near_object | :bm25 | :hybrid

t()

@type t() :: %WeaviateEx.Query.Generate{
  additional: [String.t()] | nil,
  collection: String.t(),
  grouped_properties: [String.t()] | nil,
  grouped_task: String.t() | nil,
  limit: pos_integer() | nil,
  offset: non_neg_integer() | nil,
  return_properties: [String.t()] | nil,
  search_opts: keyword(),
  search_query: term(),
  search_type: search_type() | nil,
  single_prompt: String.t() | nil,
  tenant: String.t() | nil,
  where: map() | nil
}

Functions

additional(builder, fields)

@spec additional(t(), [String.t()]) :: t()

Sets additional metadata fields to return.

Examples

Generate.additional(builder, ["distance", "certainty", "vector"])

bm25(builder, query, opts \\ [])

@spec bm25(t(), String.t(), keyword()) :: t()

Adds BM25 keyword search to the query.

Options

  • :properties - Properties to search in
  • :operator - BM25 operator (:and or :or)

Examples

Generate.bm25(builder, "elixir programming", properties: ["title", "content"])

execute(builder, client)

@spec execute(t(), WeaviateEx.Client.t()) ::
  {:ok, WeaviateEx.Query.GenerativeResult.t()} | {:error, term()}

Executes the generative query against Weaviate.

Examples

{:ok, result} = Generate.execute(builder, client)

grouped_task(builder, task, opts \\ [])

@spec grouped_task(t(), String.t(), keyword()) :: t()

Sets the grouped task for generation across all results.

Options

  • :properties - Properties to include in the context for generation

Examples

Generate.grouped_task(builder, "Summarize all articles")
Generate.grouped_task(builder, "Write a report", properties: ["title", "content"])

hybrid(builder, query, opts \\ [])

@spec hybrid(t(), String.t(), keyword()) :: t()

Adds hybrid search to the query.

Options

  • :alpha - Weight between vector (1.0) and keyword (0.0) search
  • :fusion_type - Fusion algorithm (:ranked or :relative_score)
  • :properties - Properties to search for BM25 component

Examples

Generate.hybrid(builder, "machine learning", alpha: 0.7)

limit(builder, limit)

@spec limit(t(), pos_integer()) :: t()

Sets the maximum number of results to return.

Examples

Generate.limit(builder, 10)

near_object(builder, object_id, opts \\ [])

@spec near_object(t(), String.t(), keyword()) :: t()

Adds near_object search to the query.

Options

  • :certainty - Minimum certainty threshold (0.0 to 1.0)
  • :distance - Maximum distance threshold

Examples

Generate.near_object(builder, "uuid-123", certainty: 0.85)

near_text(builder, query, opts \\ [])

@spec near_text(t(), String.t() | [String.t()], keyword()) :: t()

Adds near_text search to the query.

Options

  • :certainty - Minimum certainty threshold (0.0 to 1.0)
  • :distance - Maximum distance threshold
  • :move_to - Concepts to move towards
  • :move_away - Concepts to move away from

Examples

Generate.near_text(builder, "machine learning", certainty: 0.8)

near_vector(builder, vector, opts \\ [])

@spec near_vector(t(), [float()], keyword()) :: t()

Adds near_vector search to the query.

Options

  • :certainty - Minimum certainty threshold (0.0 to 1.0)
  • :distance - Maximum distance threshold
  • :target_vectors - Target named vectors

Examples

Generate.near_vector(builder, [0.1, 0.2, 0.3], certainty: 0.9)

new(collection)

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

Creates a new generative query builder for a collection.

Examples

builder = Generate.new("Article")

offset(builder, offset)

@spec offset(t(), non_neg_integer()) :: t()

Sets the number of results to skip.

Examples

Generate.offset(builder, 20)

parse_response(arg1, collection)

@spec parse_response(map(), String.t()) :: WeaviateEx.Query.GenerativeResult.t()

Parses an API response into a GenerativeResult struct.

return_properties(builder, properties)

@spec return_properties(t(), [String.t()]) :: t()

Sets the properties to return from the search.

Examples

Generate.return_properties(builder, ["title", "content", "author"])

single_prompt(builder, prompt)

@spec single_prompt(t(), String.t()) :: t()

Sets the single prompt for per-object generation.

The prompt can include property placeholders like {title} that will be replaced with actual property values for each object.

Examples

Generate.single_prompt(builder, "Summarize: {title}")
Generate.single_prompt(builder, "Write about {title} and its {category}")

tenant(builder, tenant)

@spec tenant(t(), String.t()) :: t()

Sets the tenant for multi-tenant collections.

Examples

Generate.tenant(builder, "tenant-a")

to_graphql(builder)

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

Converts the builder to a GraphQL query string.

valid?(builder)

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

Validates the query builder has required fields.

A valid generative query must have:

  • A search type (near_text, near_vector, etc.)
  • At least one prompt (single_prompt or grouped_task)

where(builder, filter)

@spec where(t(), map()) :: t()

Sets a filter condition for the query.

Examples

Generate.where(builder, %{path: ["status"], operator: "Equal", valueText: "published"})