Skip to main content

A fast, memory-efficient hybrid search system combining BM25 and vector search

Reason this release was yanked:

bm25-chroma is correct name

Project description

Enhanced Hybrid Retriever

A fast, memory-efficient hybrid search system combining optimized BM25 and vector search with Reciprocal Rank Fusion (RRF).

Features

  • Optimized BM25: Memory-efficient with integer indices and pre-sorted postings
  • Vector Search: Semantic similarity using ChromaDB and sentence transformers
  • Hybrid Fusion: Industry-standard Reciprocal Rank Fusion (RRF)
  • Dual Processing Modes: Sequential or unified batch processing
  • State Persistence: Automatic save/load of BM25 index

Quick Start

import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from hybrid_retriever import HybridRetriever as HybridRetriever

# Initialize
retriever = HybridRetriever(
    chroma_path="./my_db",
    collection_name="my_docs"
)

# Add documents
documents = [
    "Machine learning helps analyze data patterns.",
    "Natural language processing understands human text.",
    "Deep learning uses neural networks for complex tasks."
]

retriever.add_documents_batch(
    documents,
    mode="unified",  # or "sequential"
    show_progress=True
)

# Search
results = retriever.hybrid_search("machine learning", top_k=5)
for doc_id, score, metadata in results:
    print(f"{doc_id}: {score:.3f} - {metadata['text'][:100]}...")

Installation

pip install -r requirements.txt

Testing

Run tests to verify functionality:

pytest tests/

Or run directly:

python tests/test_examples.py

Examples

  • examples/basic_usage.py - Simple example with custom documents
  • examples/brown_corpus_demo.py - Full demo with Brown corpus

Processing Modes

Unified Mode (Recommended)

  • Processes both BM25 and ChromaDB together
  • Usually faster for large datasets
  • Better for production use

Sequential Mode

  • Processes ChromaDB first, then BM25
  • Better for debugging and optimization
  • Separate timing for each system

Performance

The system is designed for efficiency through incremental operations:

No Full Recalculation: Adding or removing documents updates only affected components:

  • Vocabulary set adds/removes only new/orphaned terms
  • Inverted index updates only posting lists for changed terms
  • Document statistics incrementally adjust averages and counts

Python Native Libraries: Heavy lifting handled by optimized built-ins:

  • Counter.most_common() provides pre-sorted frequency lists
  • heapq.merge() efficiently combines sorted posting lists
  • set operations for O(1) vocabulary lookups and updates
  • defaultdict(Counter) for sparse term-document matrices

Batch Processing: Configurable batch sizes balance memory usage and processing speed:

  • Pending additions buffer reduces index update frequency
  • Automatic flush mechanism maintains data consistency
  • Progress tracking for large document collections

API

Main Classes

  • BM25: Fast BM25 implementation
  • HybridRetriever: Main hybrid search interface

Key Methods

  • add_documents_batch(): Add documents in batches
  • search_bm25(): BM25-only search
  • search_vector(): Vector-only search
  • hybrid_search(): Combined search with RRF
  • get_system_stats(): Performance statistics

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hybrid_retriever-0.1.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hybrid_retriever-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file hybrid_retriever-0.1.0.tar.gz.

File metadata

  • Download URL: hybrid_retriever-0.1.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.0

File hashes

Hashes for hybrid_retriever-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e921fe1850891bc304de4840ac74a761ee9ac8ad85d65d739e257119ae38b44d
MD5 4ceb5f760f70a207d0448319d91d116a
BLAKE2b-256 654e8e7ce1001fa362fa61fe20e016984dec9b04398f51f5e797785cf6df3eb0

See more details on using hashes here.

File details

Details for the file hybrid_retriever-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hybrid_retriever-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c7a64e484fddb69751ab59a43a725a0697ff9a35c748374dc6b8d218cb8215e
MD5 749e5c45ce3ee67ada6075d757450f71
BLAKE2b-256 f3f0f2b6f89b859334fc7d6d8759223b72da083643abc793ea012a714f902a51

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page