Skip to content
AyoKoding

Overview

Prerequisites

  • Prior topics: SQL Essentials and Data Structures and Algorithms Essentials -- see this topic's own Overview for the full prerequisite rationale.
  • Tools & environment: a macOS/Linux terminal; Python at a recent stable release with type hints and pyright. Every example is pure-Python and stdlib-only.
  • Assumed knowledge: dictionaries, sets, and sorting by a key; reading and writing a small file from Python.

Why this exists -- the big idea

Index-then-rank: build a term -> documents map once, then let every query do a cheap lookup instead of a per-query scan. This tier turns that one idea into 80 runnable, progressively deeper examples -- from a hand-rolled inverted index through BM25 ranking and evaluation to a typed, incremental, persisted mini search engine.

How this topic is organized

  • Beginner (Examples 1-28) -- tokenization (whitespace vs regex), case-folding, building an inverted index and posting lists, boolean AND/OR/NOT, the two-pointer posting-list merge and why scanning loses to indexing, term frequency and idf, stop-words and their recall risk, a from-scratch Porter (1980) stemmer, skip pointers, ranking by tf-idf, and the vector-space model's cosine similarity.
  • Intermediate (Examples 29-56) -- BM25's own RSJ idf, saturating tf, and length normalization, sweeping k1/b to see them genuinely change (and even flip) a ranking, the Lucene/Elasticsearch software defaults versus the paper's own recommended range, top-k selection with a size-k heap, the full precision/recall/F1/precision@k/MAP/nDCG evaluation family over a relevance-judgment set, a typed analyzer pipeline model, a small boolean query DSL parsed and executed, positional indexes and the phrase/proximity queries they enable, and a segment-merge model.
  • Advanced (Examples 57-80) -- near-real-time refresh semantics, a typed InvertedIndex class with incremental add proven identical to a from-scratch rebuild, JSON/binary/delta-encoded persistence, an incrementally-maintained avgdl, BM25F field weighting versus a broken naive alternative, fuzzy matching (Levenshtein, Damerau-Levenshtein, spelling correction), edge and character n-grams for autocomplete and substring search, synonym expansion, toy semantic embeddings and cosine ranking, approximate versus exact k-NN, hybrid lexical+vector search, PageRank's power iteration alone and combined with BM25, and a final mini search engine assembling the whole pipeline.
  • Capstone -- a typed inverted index, BM25 top-k ranking, precision@k evaluation across two analyzer configs, and incremental indexing, assembled behind one small library over a real 8-document text corpus.

Every code example is real, runnable, fully type-annotated, colocated under learning/code/ex-NN-<slug>/, actually executed to capture its documented output -- every printed number on these pages is a genuine, captured transcript, never a fabricated one -- and pyright-clean under # pyright: strict. Run each example with python3 <file>.py from its own learning/code/ex-NN-<slug>/ directory. Where a Lucene-family engine (Elasticsearch/OpenSearch) is discussed, the code remains a pure-Python model of its behavior, never a live dependency.

Examples by Level

Beginner (Examples 1–28)

Intermediate (Examples 29–56)

Advanced (Examples 57–80)


← Previous: Overview · Next: Beginner Examples

Last updated July 26, 2026

Command Palette

Search for a command to run...