方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Build an incremental document indexer

Build an Incremental Document Indexer

Re-indexing your entire corpus every time a document changes is wasteful and creates operational risk. Incremental indexing tracks which documents have changed by hashing their content, then only re-embeds and re-indexes what is new or updated.

What you are building

Create an IncrementalIndexer that:

  1. Tracks document state in a persistent JSON file: {doc_id: {"hash": str, "chunk_ids": list[str], "indexed_at": str}}.
  2. Detects changes by comparing content hash to stored hash. A document is dirty if its hash changed or is absent from state.
  3. Handles upserts: for a changed document, delete old chunk vectors, re-chunk, embed, and add new chunks.
  4. Handles deletes: when a document is removed, delete its chunk vectors and remove from state.
  5. Reports indexing results via an IndexSyncResult dataclass.
  6. Is idempotent: calling sync twice with the same input makes no API calls on the second run.

Why this matters

Without incremental indexing, every CI deploy triggers a full re-index that may take hours and cost thousands of API calls. With it, a corpus of 100K documents where 50 changed today only costs indexing those 50.

Constraints

  • State file is JSON (human-readable for debugging).
  • Chunker and embed+add functions are injected (testable in isolation).
  • Timestamps in ISO 8601 format.

Rag Systems / medium / Step 5 of 6

Practice stage

General drill

Hint

Keep the solution explicit and reviewable.

Success criteria

Make the solution explicit, debuggable, and easy to explain.

Review checklist

Review where the boundary is, what gets validated, and what would be hard to debug later.

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.