{"slug": "i-built-a-hybrid-search-engine-from-scratch-here-s-what-i-learned-llm-zoomcamp-2", "title": "I Built a Hybrid Search Engine From Scratch — Here's What I Learned (LLM Zoomcamp 2026, Module 2)", "summary": "A developer completed Module 2 of the LLM Zoomcamp 2026 and built a hybrid search engine from scratch, combining keyword and vector search. The project implemented vector search using ONNX runtime embeddings and cosine similarity, then integrated it with keyword search using Reciprocal Rank Fusion (RRF) for improved retrieval accuracy. The developer found that vector search captures semantic meaning while keyword search matches exact words, and hybrid search outperforms either alone.", "body_md": "I just completed Module 2 of the **LLM Zoomcamp 2026** by [@DataTalksClub](https://github.com/DataTalksClub/llm-zoomcamp/) — and this module completely changed how I think about search.\n\nModule 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.\n\nHere's everything I built and learned.\n\nTraditional 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.\n\n**Vector search matches meaning, not words.**\n\nEvery 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.\n\nThis is the foundation of modern AI-powered search, and it's what makes RAG systems actually work at scale.\n\nInstead of downloading the full PyTorch + CUDA stack (~2GB), I used a lightweight ONNX runtime embedder — same vectors, 30x smaller installation, runs on any CPU:\n\n``` python\nfrom embedder import Embedder\n\nembedder = Embedder()  # loads Xenova/all-MiniLM-L6-v2 via ONNX\nv = embedder.encode(\"How does approximate nearest neighbor search work?\")\nprint(len(v))  # 384 dimensions\n```\n\nThe model produces **384-dimensional vectors** — each number represents a dimension of meaning in the text.\n\nBefore using any library, I implemented vector search by hand to understand what's happening under the hood:\n\n``` python\nimport numpy as np\n\n# cosine similarity — vectors are normalized, so dot product works directly\ndef cosine_similarity(a, b):\n    return np.dot(a, b)\n\n# score all chunks against a query\nscores = X.dot(v)  # X is the matrix of all chunk embeddings\nbest_idx = np.argmax(scores)\n```\n\nThis is exactly what vector databases like Qdrant and pgvector do internally — just much faster at scale using HNSW indexing.\n\nFull 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:\n\n``` python\nfrom gitsource import chunk_documents\nchunks = chunk_documents(documents, size=2000, step=1000)\n# 72 pages → 295 overlapping chunks\n```\n\nOverlapping chunks (step < size) ensure sentences at boundaries don't get cut off. After chunking, retrieval becomes far more precise.\n\n`minsearch`\n\nnow has a `VectorSearch`\n\nclass that wraps the numpy math into a clean interface:\n\n``` python\nfrom minsearch import VectorSearch\n\nvector_index = VectorSearch(keyword_fields=[\"filename\"])\nvector_index.fit(X, chunks)\n\nresults = vector_index.search(query_vector, num_results=5)\n```\n\nFor the query **\"How do I store vectors in PostgreSQL?\"**:\n\n`08-pgvector.md`\n\nentirely because \"pgvector\" wasn't in the query`08-pgvector.md`\n\nfirst because it understood the semantic connection between \"store vectors\" and \"pgvector\"This is the key insight: vector search finds meaning, keyword search finds words.\n\nNeither approach is perfect on its own:\n\nThe solution is **hybrid search** — run both and merge the results using RRF:\n\n``` python\ndef rrf(result_lists, k=60, num_results=5):\n    scores = {}\n    docs = {}\n    for results in result_lists:\n        for rank, doc in enumerate(results):\n            key = (doc[\"filename\"], doc[\"start\"])\n            scores[key] = scores.get(key, 0) + 1 / (k + rank)\n            docs[key] = doc\n    ranked = sorted(scores, key=scores.get, reverse=True)\n    return [docs[key] for key in ranked[:num_results]]\n\nresults = rrf([vector_results, text_results])\n```\n\nRRF 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.\n\n**1. Embeddings capture meaning, not words.** \"Enroll\" and \"join\" produce similar vectors. \"Pizza\" and \"enrollment\" don't. This is what makes semantic search powerful.\n\n**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.\n\n**3. Neither keyword nor vector search is best.** Use hybrid search (RRF) in production. It consistently outperforms either approach alone.\n\n**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.\n\n**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.\n\nAll my code for Module 2 is open source:\n\n[github.com/Derrick-Ryan-Giggs/llm-zoomcamp-2026](https://github.com/Derrick-Ryan-Giggs/llm-zoomcamp-2026)\n\nIt includes:\n\n`vector-search.ipynb`\n\n— embeddings, Qdrant, and vector RAG pipeline`Vector Search Homework.ipynb`\n\nLLM Zoomcamp is **completely free** — no paywall, no certificate fees.\n\nSign up: [github.com/DataTalksClub/llm-zoomcamp](https://github.com/DataTalksClub/llm-zoomcamp/)\n\n*Are you working through LLM Zoomcamp 2026? Drop a comment — I'd love to compare notes.*", "url": "https://wpnews.pro/news/i-built-a-hybrid-search-engine-from-scratch-here-s-what-i-learned-llm-zoomcamp-2", "canonical_source": "https://dev.to/derrickryangiggs/i-built-a-hybrid-search-engine-from-scratch-heres-what-i-learned-llm-zoomcamp-2026-module-2-3jdj", "published_at": "2026-06-26 11:49:10+00:00", "updated_at": "2026-06-26 12:05:16.646490+00:00", "lang": "en", "topics": ["large-language-models", "artificial-intelligence", "machine-learning", "natural-language-processing", "developer-tools"], "entities": ["DataTalksClub", "LLM Zoomcamp", "ONNX", "Xenova/all-MiniLM-L6-v2", "Qdrant", "pgvector", "HNSW", "minsearch"], "alternates": {"html": "https://wpnews.pro/news/i-built-a-hybrid-search-engine-from-scratch-here-s-what-i-learned-llm-zoomcamp-2", "markdown": "https://wpnews.pro/news/i-built-a-hybrid-search-engine-from-scratch-here-s-what-i-learned-llm-zoomcamp-2.md", "text": "https://wpnews.pro/news/i-built-a-hybrid-search-engine-from-scratch-here-s-what-i-learned-llm-zoomcamp-2.txt", "jsonld": "https://wpnews.pro/news/i-built-a-hybrid-search-engine-from-scratch-here-s-what-i-learned-llm-zoomcamp-2.jsonld"}}