How does a per-document content hash let a nightly RAG re-index skip unchanged documents, and what does a text-only hash miss?
answer
- compare before you chunk
- a skip signal, not an identity
- what else lands on the chunk
- the pipeline's own version
- save the hash after the write
basics
~20 sStore a hash of each document's normalised text and skip parsing, chunking and embedding when tonight's hash matches. A text-only hash misses changes that alter the stored chunks without touching the words: metadata, access lists and the pipeline's own parser or chunker version.
solid answer
~50 sFor each document the registry keeps a hash, for example SHA-256 of the extracted text after normalising whitespace and stripping volatile bits such as a rendered "last viewed" date. Tonight's run hashes the fresh text; if it matches, the job skips chunking, embedding and writing, which is where most of the cost is. The trap is that the chunks carry more than text. A title change, a new URL, a changed access list or a new chunker version all change what should be stored, yet a text-only hash still matches and the stale chunks stay. So hash a canonical record of everything that determines the stored chunks, or keep two hashes: a content hash that forces re-embedding and a metadata hash that only rewrites payloads. Save the new hash only after the write succeeds. And never use the hash as the document's ID; it changes with every edit.
go deeper
Recall that the hash is compared before the expensive steps and that a match skips chunking and embedding for that document.
Explain normalisation, the inputs a text-only hash misses, and why the hash is saved only after the chunks are written.
Design the two-hash split so metadata-only changes rewrite payloads without embedding calls, and name the access-list case as the one that leaks.
Decide which fields count as content versus metadata for the whole platform, since that line sets both the embedding bill and the risk of stale access lists.
## Why a nightly re-index needs a skip signal A retrieval-augmented generation (RAG) index is rebuilt from its sources on a schedule: every night the ingestion job lists the pages of, say, a product-documentation site and processes each one. Processing means extracting text, splitting it into **chunks**, calling an embedding model for every chunk and writing the results to a vector store. The embedding calls dominate the cost and the time. On a typical night only a small share of pages changed, so re-embedding all of them wastes nearly the whole run. A **content hash** is the cheap test that lets the job skip the rest. The registry (a table keyed by document ID that the pipeline keeps beside the store) holds each document's hash from the last successful run. Tonight the job extracts the text, hashes it and compares. A match means nothing changed and the document is skipped. A mismatch sends it through the full path. ## Making the hash fire only on real change The hash is only useful if it changes when, and only when, the stored chunks should change. 1. **Normalise before hashing.** Collapse whitespace, fix line endings, and strip volatile elements a page renders on every request, such as a "last viewed" timestamp, a visitor counter or a rotating banner. Otherwise the hash differs every night and the skip never fires. 2. **Hash the extracted text, not the raw file**, when the file format carries noise (embedded timestamps, reordered attributes) that does not change the text. 3. **Use a cryptographic hash** such as SHA-256, so accidental collisions between different texts can be ignored. A modified time from the source is a useful cheap pre-filter, but it is not proof: some edits do not bump it, and some saves bump it with no change. Detecting that a source changed at all is the connector's concern; the hash decides whether this pipeline must rewrite anything. ## What a text-only hash misses The stored chunks are a function of more than the text. Any input to that function can change while the text stays identical: | Change | Text hash | Stored chunks should | |---|---|---| | Title, breadcrumb or URL edited | Unchanged | Get new payload metadata | | Access list on the page changed | Unchanged | Carry the new access list | | Product-version label or language tag changed | Unchanged | Carry the new filterable field | | Parser upgraded (tables now extracted) | May be unchanged if hashed pre-parse | Be re-chunked from the better text | | Chunker size or rules changed | Unchanged | Be re-chunked | | Prefix or context template changed | Unchanged | Be re-embedded | The access-list row is the dangerous one: a page restricted to a smaller audience keeps its old, wider access list on every chunk, and any query-time filter that reads the chunk's payload enforces the stale list. Two designs close the gap: - **One hash over a canonical record**: the normalised text, every metadata field copied onto the chunks, and a pipeline version string (parser, chunker and prefix-template versions). Any change forces full re-processing. - **Two hashes**: a **content hash** over the text plus the chunking and embedding inputs, and a **metadata hash** over payload-only fields. A content change re-chunks and re-embeds; a metadata-only change rewrites payloads on the existing chunk IDs with no embedding calls. This saves real money when access lists or titles change in bulk. ```json { "doc_id": "docs-site:4812", "content_hash": "sha256:9f2c...", "meta_hash": "sha256:1ab0...", "pipeline_version": "parser-3/chunker-7/prefix-2", "chunk_count": 9 } ``` (An illustrative registry row; the field names are not any product's.) A change of **embedding model** also invalidates every vector without touching the text or metadata hashes. Putting the model version in the record makes the skip refuse to keep old vectors, but the whole-corpus re-embed that follows is its own job, sized and resumed separately. ## Two ordering and identity traps - **Save the hash last.** If the new hash is written before the chunks and the write then fails, every later run sees a match and skips the page forever, leaving the old chunks in place. Write chunks, delete stale ones, then save the hash. - **A hash is a skip signal, not an ID.** Using the content hash as the document's identity breaks two ways: every edit gives the page a new identity, orphaning its old chunks, and two pages with identical text (a duplicated template, a page copied into two sections) collapse into one. Identity comes from the source's stable document ID; the hash only answers "did it change?". ## What interviewers listen for - Normalisation, or the skip never fires. - The list of non-text inputs, with access lists named. - The split between re-embedding and payload-only updates. - The write-then-save ordering and the hash-is-not-an-ID point.
- A thousand pages had only their access lists changed. How do you avoid re-embedding them?Keep a separate metadata hash. When the content hash matches but the metadata hash differs, rewrite the payload on each existing chunk ID, or update the access field in place where the store supports partial updates, and make no embedding calls. Only a content-hash change re-chunks and re-embeds.
- The skip never fires; every page re-embeds every night. What do you check first?Diff the normalised text of one page across two runs. Usually something volatile is being hashed: a rendered date, a session value, a randomised attribute order, or whitespace from a changed extractor. Strip it before hashing, or hash a narrower, stable extraction.
- If the text hash matches, nothing about the stored chunks can be stale.
- The content hash makes a good document ID because it is deterministic.
- Save the new hash first, then write the chunks.
- The source's modified time is enough; hashing is redundant.
- Hashing raw files is fine even when they embed timestamps.