rag.search.hybrid

Hybrid search combining vector and BM25 retrieval.

Hybrid search combining vector and BM25 retrieval.

class HybridSearch(vector_search_fn=None, bm25_search_fn=None, alpha=0.6, rrf_k=60)[source]

Bases: object

Hybrid search combining vector similarity and BM25 keyword matching.

Uses reciprocal rank fusion (RRF) to combine results from multiple retrieval methods.

Parameters:
  • vector_search_fn (Callable[[str, int], list[dict[str, Any]]] | None) – Function that takes (query, n_results) and returns vector results.

  • bm25_search_fn (Callable[[str, int], list[dict[str, Any]]] | None) – Function that takes (query, n_results) and returns BM25 results.

  • alpha (float) – Weight for vector search (1-alpha for BM25). Default 0.6.

  • rrf_k (int) – RRF constant for rank fusion. Default 60.

search(query, n_results=5, mode='hybrid')[source]

Perform search using the specified mode.

Parameters:
  • query (str) – Search query.

  • n_results (int) – Maximum number of results.

  • mode (str) – Search mode - “vector”, “bm25”, or “hybrid”.

Returns:

List of search results with merged scores.

Return type:

list[dict[str, Any]]

set_alpha(alpha)[source]

Set the alpha parameter (vector weight).

Parameters:

alpha (float) – Weight for vector search (0.0 to 1.0).

Return type:

None

set_search_functions(vector_search_fn=None, bm25_search_fn=None)[source]

Set or update search functions.

Parameters:
Return type:

None

reciprocal_rank_fusion(result_lists, k=60, weights=None)[source]

Combine multiple result lists using reciprocal rank fusion.

Parameters:
  • result_lists (list[list[dict[str, Any]]]) – List of result lists to fuse.

  • k (int) – RRF constant.

  • weights (list[float] | None) – Optional weights for each result list.

Returns:

Fused and sorted results.

Return type:

list[dict[str, Any]]

Classes

class HybridSearch(vector_search_fn=None, bm25_search_fn=None, alpha=0.6, rrf_k=60)[source]

Bases: object

Hybrid search combining vector similarity and BM25 keyword matching.

Uses reciprocal rank fusion (RRF) to combine results from multiple retrieval methods.

Parameters:
  • vector_search_fn (Callable[[str, int], list[dict[str, Any]]] | None) – Function that takes (query, n_results) and returns vector results.

  • bm25_search_fn (Callable[[str, int], list[dict[str, Any]]] | None) – Function that takes (query, n_results) and returns BM25 results.

  • alpha (float) – Weight for vector search (1-alpha for BM25). Default 0.6.

  • rrf_k (int) – RRF constant for rank fusion. Default 60.

search(query, n_results=5, mode='hybrid')[source]

Perform search using the specified mode.

Parameters:
  • query (str) – Search query.

  • n_results (int) – Maximum number of results.

  • mode (str) – Search mode - “vector”, “bm25”, or “hybrid”.

Returns:

List of search results with merged scores.

Return type:

list[dict[str, Any]]

set_alpha(alpha)[source]

Set the alpha parameter (vector weight).

Parameters:

alpha (float) – Weight for vector search (0.0 to 1.0).

Return type:

None

set_search_functions(vector_search_fn=None, bm25_search_fn=None)[source]

Set or update search functions.

Parameters:
Return type:

None

Functions

reciprocal_rank_fusion(result_lists, k=60, weights=None)[source]

Combine multiple result lists using reciprocal rank fusion.

Parameters:
  • result_lists (list[list[dict[str, Any]]]) – List of result lists to fuse.

  • k (int) – RRF constant.

  • weights (list[float] | None) – Optional weights for each result list.

Returns:

Fused and sorted results.

Return type:

list[dict[str, Any]]