Careeroza
What is a Vector Index
Search by Meaning
A vector database (or vector index) stores many embeddings together with the original text and metadata, and answers one question fast: which stored vectors are closest to this query vector? This is semantic search: "phone heating up while charging" finds the help article "Battery temperature issues" even without shared words (with a real embedding model).
indexing (once): document ──► embedding model ──► vector ──► store (vector + text + metadata) searching: query ──► embedding model ──► vector ──► nearest vectors ──► results
A Tiny Vector Store
Example
A complete in-memory vector store in about 30 lines. The hashed bag-of-words embed() from the Embeddings module stands in for a real embedding model:
import hashlib, math, re
def embed(text, dim=64):
v = [0.0] * dim
for w in re.findall(r"[a-z0-9]+", text.lower()):
h = int(hashlib.md5(w.encode()).hexdigest(), 16)
v[h % dim] += 1.0 if (h >> 8) % 2 == 0 else -1.0
n = math.sqrt(sum(x * x for x in v)) or 1.0
return [x / n for x in v]
class VectorStore:
def __init__(self):
self.items = [] # (id, vector, text, metadata)
def add(self, id, text, metadata=None):
self.items.append((id, embed(text), text, metadata or {}))
def search(self, query, k=3):
q = embed(query)
scored = [(sum(a * b for a, b in zip(q, v)), id, text) for id, v, text, _ in self.items]
return sorted(scored, reverse=True)[:k]
store = VectorStore()
store.add("faq-1", "How long does a refund take? Refunds take 5-7 working days.")
store.add("faq-2", "Delivery usually takes 3-5 days in metro cities.")
store.add("faq-3", "You can cancel an order before it is shipped.")
store.add("faq-4", "Exchange clothes for a different size within 10 days.")
for score, id, text in store.search("how many days for refund", k=2):
print(f"{score:.3f} {id} {text}")Output
0.359 faq-1 How long does a refund take? Refunds take 5-7 working days. 0.298 faq-4 Exchange clothes for a different size within 10 days.