DEV Community

Ryan Giggs
Ryan Giggs

Posted on

I Built a Hybrid Search Engine From Scratch — Here's What I Learned (LLM Zoomcamp 2026, Module 2)

I just completed Module 2 of the LLM Zoomcamp 2026 by @DataTalksClub — and this module completely changed how I think about search.

Module 1 taught me RAG and agentic pipelines. Module 2 taught me that the search step inside RAG matters far more than I realized — and that keyword search is only half the story.

Here's everything I built and learned.


What Is Vector Search and Why Does It Matter?

Traditional keyword search matches words. If you search for "enroll", it finds documents containing "enroll" — but misses documents about "joining", "signing up", or "registration" even if they mean exactly the same thing.

Vector search matches meaning, not words.

Every piece of text gets converted into a vector — a list of hundreds of numbers that captures its semantic meaning. Similar meanings produce similar vectors, so you can find relevant documents even when they use completely different words.

This is the foundation of modern AI-powered search, and it's what makes RAG systems actually work at scale.


What I Built in Module 2

1. Text Embeddings with a Lightweight ONNX Model

Instead of downloading the full PyTorch + CUDA stack (~2GB), I used a lightweight ONNX runtime embedder — same vectors, 30x smaller installation, runs on any CPU:

from embedder import Embedder

embedder = Embedder()  # loads Xenova/all-MiniLM-L6-v2 via ONNX
v = embedder.encode("How does approximate nearest neighbor search work?")
print(len(v))  # 384 dimensions
Enter fullscreen mode Exit fullscreen mode

The model produces 384-dimensional vectors — each number represents a dimension of meaning in the text.

2. Vector Search From Scratch with NumPy

Before using any library, I implemented vector search by hand to understand what's happening under the hood:

import numpy as np

# cosine similarity — vectors are normalized, so dot product works directly
def cosine_similarity(a, b):
    return np.dot(a, b)

# score all chunks against a query
scores = X.dot(v)  # X is the matrix of all chunk embeddings
best_idx = np.argmax(scores)
Enter fullscreen mode Exit fullscreen mode

This is exactly what vector databases like Qdrant and pgvector do internally — just much faster at scale using HNSW indexing.

3. Chunking Long Documents for Better Retrieval

Full pages are too long and dilute the embedding — a match buried deep in a 10,000-character page still pulls in the whole page. The fix is chunking:

from gitsource import chunk_documents
chunks = chunk_documents(documents, size=2000, step=1000)
# 72 pages → 295 overlapping chunks
Enter fullscreen mode Exit fullscreen mode

Overlapping chunks (step < size) ensure sentences at boundaries don't get cut off. After chunking, retrieval becomes far more precise.

4. Vector Search with minsearch

minsearch now has a VectorSearch class that wraps the numpy math into a clean interface:

from minsearch import VectorSearch

vector_index = VectorSearch(keyword_fields=["filename"])
vector_index.fit(X, chunks)

results = vector_index.search(query_vector, num_results=5)
Enter fullscreen mode Exit fullscreen mode

5. Comparing Keyword vs Vector Search

For the query "How do I store vectors in PostgreSQL?":

  • Keyword search — missed 08-pgvector.md entirely because "pgvector" wasn't in the query
  • Vector search — ranked 08-pgvector.md first because it understood the semantic connection between "store vectors" and "pgvector"

This is the key insight: vector search finds meaning, keyword search finds words.

6. Hybrid Search with Reciprocal Rank Fusion (RRF)

Neither approach is perfect on its own:

  • Vector search can miss exact terms, names, and rare keywords
  • Keyword search misses paraphrases and synonyms

The solution is hybrid search — run both and merge the results using RRF:

def rrf(result_lists, k=60, num_results=5):
    scores = {}
    docs = {}
    for results in result_lists:
        for rank, doc in enumerate(results):
            key = (doc["filename"], doc["start"])
            scores[key] = scores.get(key, 0) + 1 / (k + rank)
            docs[key] = doc
    ranked = sorted(scores, key=scores.get, reverse=True)
    return [docs[key] for key in ranked[:num_results]]

results = rrf([vector_results, text_results])
Enter fullscreen mode Exit fullscreen mode

RRF ignores raw scores (which live on different scales) and only looks at rank position. A document that ranks well in both lists beats one that's only strong in a single list — even if it wasn't first in either.


Key Takeaways

1. Embeddings capture meaning, not words. "Enroll" and "join" produce similar vectors. "Pizza" and "enrollment" don't. This is what makes semantic search powerful.

2. Chunking is not optional. Full pages dilute embeddings. 2,000-character overlapping chunks dramatically improve retrieval precision and cut LLM input tokens by 3x.

3. Neither keyword nor vector search is best. Use hybrid search (RRF) in production. It consistently outperforms either approach alone.

4. ONNX makes embeddings practical anywhere. No GPU, no PyTorch, no CUDA. 67MB download, runs on a basic laptop. There's no reason not to use vector search even in constrained environments.

5. The right search approach depends on your data. Vector search wins for semantic queries. Keyword search wins for exact terms (names, codes, IDs). Hybrid wins most of the time — but measure to be sure.


My Homework Solution

All my code for Module 2 is open source:

github.com/Derrick-Ryan-Giggs/llm-zoomcamp-2026

It includes:

  • vector-search.ipynb — embeddings, Qdrant, and vector RAG pipeline
  • Vector Search Homework.ipynb

Want to Learn Too?

LLM Zoomcamp is completely free — no paywall, no certificate fees.

Sign up: github.com/DataTalksClub/llm-zoomcamp


Are you working through LLM Zoomcamp 2026? Drop a comment — I'd love to compare notes.

Top comments (0)