Skip to main content
Module 3 · Lesson 2 of 410/48 in the course~20 min
Module lessons (2/4)

Lexical Search & BM25 Scoring

Lesson preview3 hands-on exercises readyAI Engineering & Production RAG · Module 3 — Retrieval & Vector Indexing

Why semantic search fails on exact IDs and codes; implementing Okapi BM25 with term frequency saturation.

What you'll learn in this lesson

  • Identify specific retrieval failure modes where dense vector embeddings fail and lexical search succeeds.
  • Deconstruct the Okapi BM25 ranking algorithm: TF saturation, IDF penalty, and document length normalization.
  • Implement an in-memory inverted index and BM25 scoring function in pure Python.
  • Tune k₁ and b hyperparameters for short queries and variable-length technical documentation.

Overview

While dense vector embeddings excel at grasping high-level concepts and synonyms, they suffer from a severe blind spot in enterprise applications: exact token matching.

Because subword tokenizers break unfamiliar alphanumeric strings into arbitrary byte chunks (e.g. CVE, -, 2024, -, 38077), their dense vectors land near generic cybersecurity concepts. The vector retriever frequently returns an article about CVE-2023-21554 because semantically, all CVE vulnerability discussions look identical in latent space!

Sections covered in this lesson

  1. 1Why Dense Semantic Search Fails on Exact Identifiers
  2. 2The Okapi BM25 Algorithm
  3. 3Check Your Understanding
  4. 4Practice: Inverted Index & BM25 Scoring in Python
Pro Lesson · Module 3 — Retrieval & Vector Indexing

Unlock "Lexical Search & BM25 Scoring" and the full AI Engineering & Production RAG curriculum

Why semantic search fails on exact IDs and codes; implementing Okapi BM25 with term frequency saturation.

Unlock all 3 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in AI Engineering & Production RAG plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.