Hybrid Search

TL;DR — Combine semantic meaning with keyword precision. Best results when you know some of the words but not the exact phrasing.

Pure semantic search excels when you don’t know the exact terminology. But sometimes you do know a keyword — a function name, an error code, a library name. Hybrid mode gives you both.

How It Works

Hybrid search runs two searches in parallel and blends their scores:

  1. Semantic — Model2Vec embeds your query and every code chunk, ranks by cosine similarity
  2. BM25 — ripgrep does keyword matching with tf-idf scoring
  3. Fusion — scores are combined with a configurable weight (default 70% semantic, 30% keyword)

Usage

sgrep --hybrid "database connection pool" src/

When to Use Hybrid

ScenarioModeWhy
Know the concept, not the codeSemantic (default)No keywords to match
Know some keywords + conceptHybridKeywords boost precision
Know exact stringJust use grep/rgExact match is faster

Tuning

The --alpha flag controls the balance:

# 70% semantic, 30% keyword (default)
sgrep --hybrid "error recovery" src/

# More keyword weight
sgrep --hybrid --alpha 0.4 "ConnectionPool" src/

# More semantic weight
sgrep --hybrid --alpha 0.9 "gracefully shut down" src/

Requires ripgrep — Hybrid mode shells out to rg for BM25 scoring. Install it with brew install ripgrep or cargo install ripgrep.

Was this page helpful?