Updating the Index
Add and remove.
Help pages change. New ones are added, old ones are removed. Rebuilding everything each time is wasteful. Instead, we give every chunk an ID, so we can add and remove chunks one by one.

- Pages change
- Don't rebuild everything
- Give chunks IDs
Removing the old price list
Index ID map lets us choose an ID for every vector. We add all chunks, using their position as the ID. Then we find the old price list's chunks, and remove them by ID. Run it. The count goes down, and a price search can no longer find the old page.
import numpy as np
from rag_kit import load_docs, make_chunks, embed
import faiss
chunks = make_chunks(load_docs())
index = faiss.IndexIDMap(faiss.IndexFlatIP(384))
index.add_with_ids(embed([c["text"] for c in chunks]), np.arange(len(chunks)))
print("before:", index.ntotal)
old = [i for i, c in enumerate(chunks) if c["source"] == "price_list_2025.md"]
index.remove_ids(np.array(old))
print("after: ", index.ntotal)
_, ids = index.search(embed(["How much is the Volt e-bike?"]), 2)
print([chunks[i]["source"] for i in ids[0]])before: 44 after: 43 ['price_list_2026.md', 'servicing.md']
Also uses rag_kit.py
import warnings; warnings.filterwarnings("ignore")
from pathlib import Path
import numpy as np
from sentence_transformers import SentenceTransformer
import faiss # after torch, so the two libraries load safely together
faiss.omp_set_num_threads(1)
from langchain_text_splitters import RecursiveCharacterTextSplitter
def load_docs(folder="data/rag/northwind"):
return [{"source": p.name, "text": p.read_text()} for p in sorted(Path(folder).glob("*.md"))]
def make_chunks(docs, size=300, overlap=50):
splitter = RecursiveCharacterTextSplitter(chunk_size=size, chunk_overlap=overlap)
return [{"source": d["source"], "text": t.replace("\n", " ")}
for d in docs for t in splitter.split_text(d["text"])]
embedder = SentenceTransformer("all-MiniLM-L6-v2", device="cpu")
def embed(texts):
return embedder.encode(texts, normalize_embeddings=True)
class Index:
def __init__(self, chunks):
self.chunks = chunks
self.vectors = embed([c["text"] for c in chunks])
def search(self, query, k=3):
scores = self.vectors @ embed([query])[0]
top = np.argsort(-scores)[:k]
return [(round(float(scores[i]), 2), self.chunks[i]) for i in top]Keeping in sync
Use stable IDs, like file name plus chunk number. When a file changes, remove its old chunks, and add the new ones. When a file is deleted, remove its chunks. We'll automate this in the production section.
| key | value |
|---|---|
| Stable IDs | file + chunk number |
| File changed | remove, then add |
| File deleted | remove its chunks |
| Automate | later |
Summary
Give chunks IDs, so you can add and remove them without rebuilding. Next, the vector search quiz.
Key takeaways- IndexIDMap
- add_with_ids
- remove_ids