Rag.Retriever.Hybrid (rag v0.3.4)

View Source

Hybrid retriever combining semantic and full-text search with RRF.

This retriever combines vector similarity search and full-text search using Reciprocal Rank Fusion (RRF) to produce better results than either approach alone.

Reciprocal Rank Fusion (RRF)

RRF is an effective technique for combining rankings from different retrieval systems. For each document, it calculates:

RRF(d) = Σ 1 / (k + rank(d))

where k is typically 60, and the sum is over all retrieval systems where the document appears.

Usage

# Create a hybrid retriever
retriever = %Rag.Retriever.Hybrid{repo: MyApp.Repo}

# Retrieve using both embedding and text query
query_embedding = [0.1, 0.2, 0.3, ...]
query_text = "search terms"
{:ok, results} = Rag.Retriever.retrieve(
  retriever,
  {query_embedding, query_text},
  limit: 10
)

Result Format

Returns results with a score field representing RRF score:

  • score = Combined RRF score from both retrieval methods
  • Higher scores indicate better overall relevance
  • Documents appearing in both result sets get higher scores

Summary

Functions

Retrieve documents using hybrid search with RRF fusion.

Returns true - Hybrid retriever supports embedding queries.

Returns true - Hybrid retriever supports text queries.

Types

t()

@type t() :: %Rag.Retriever.Hybrid{repo: module()}

Functions

retrieve(retriever, query, opts \\ [])

@spec retrieve(t(), {[float()], String.t()}, keyword()) ::
  {:ok, [Rag.Retriever.result()]} | {:error, term()}

Retrieve documents using hybrid search with RRF fusion.

Parameters

  • retriever - The Hybrid retriever struct
  • query - Tuple of {embedding, text} for hybrid search
  • opts - Options:
    • :limit - Maximum number of results (default: 10)

Returns

  • {:ok, results} - List of results with RRF scores
  • {:error, reason} - Error during retrieval

Examples

iex> retriever = %Hybrid{repo: MyRepo}
iex> Hybrid.retrieve(retriever, {[0.1, 0.2], "machine learning"}, limit: 5)
{:ok, [%{id: 1, content: "...", score: 0.032, metadata: %{}}]}

supports_embedding?()

@spec supports_embedding?() :: boolean()

Returns true - Hybrid retriever supports embedding queries.

supports_text_query?()

@spec supports_text_query?() :: boolean()

Returns true - Hybrid retriever supports text queries.