Sub topic 10
Vector databases and libraries: what they add beyond an index
This chapter assumes you know what an approximate nearest-neighbor (ANN) index does, as covered in the previous chapter. It also assumes you can read basic Python. The goal is to explain what a vector database provides that a bare index does not, so you can decide which one your project needs.
An index is only one part
An index such as FAISS IndexFlatIP or IndexHNSWFlat answers one question: which stored vectors are closest to this query vector? It returns integer positions and scores. It does not know what text those positions came from.
A real application needs more than that. A vector database or store wraps an index and adds the missing pieces.
| Need | Bare index (library) | Vector database or store |
|---|---|---|
| Store original text and metadata | You build it yourself | Built in |
| Filter by field (language, date, owner) | Not supported | Supported |
| Update or delete one item | Often limited | Supported |
| Survive restarts | You save and load files | Persistent storage |
| Many users and machines | Not supported | Replication, sharding, access control |
The id mapping problem
A FAISS index returns positions such as 7 or 412. You must keep your own table that maps each position back to a document. This sketch shows the minimum bookkeeping.
import numpy as np
import faiss
d = 4
records = [
{"text": "Reset your password in settings", "lang": "en"},
{"text": "Réinitialisez votre mot de passe", "lang": "fr"},
{"text": "Office hours are 9 to 5", "lang": "en"},
]
vectors = np.array(
[[0.9, 0.1, 0.0, 0.0], [0.8, 0.2, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]],
dtype="float32",
)
faiss.normalize_L2(vectors)
index = faiss.IndexFlatIP(d)
index.add(vectors) # position i in the index matches records[i]
query = np.array([[1.0, 0.0, 0.0, 0.0]], dtype="float32")
faiss.normalize_L2(query)
scores, ids = index.search(query, 3)
for score, i in zip(scores[0], ids[0]):
print(f"{score:.3f} {records[i]['lang']} {records[i]['text']}")If you delete a record from the list, every later position shifts and the mapping breaks. A database assigns stable ids so this does not happen.
Metadata filtering
Suppose users want only English results. With a bare index you have two poor choices:
- Search first, then drop non-matching results. This can leave you with fewer than k results.
- Keep a separate index per filter value. This works for a few values and fails for many.
Vector databases apply the filter during the search. This is the main practical reason to adopt one. The cost is that filtering interacts with ANN graphs. A very selective filter can reduce recall or slow the query, so test it on your own data.
Choosing a tool
The 100k figure is a rough guide, not a rule. A flat search over 100k vectors of 384 dimensions is a single matrix product and usually finishes in milliseconds on a modern CPU.
Trade-offs to weigh
- Operations: a database is a service you must run, back up, and upgrade. A library lives inside your process.
- Consistency: many stores make new writes visible to search after a short delay, not instantly.
- Index settings: the HNSW and IVF parameters from the previous chapter still apply. A database hides them behind defaults that may not suit your data.
- Lock-in: the vectors are portable, but query syntax and filter features differ between products.
Exercise
Your collection has 20,000 documents, no filtering needs, and one developer. Which option from the diagram fits, and why?
Show answer
A NumPy or FAISS flat index with your own id table. At 20,000 vectors an exact search is fast, so ANN tuning and a database service add operational cost without a benefit.