Skip to content

Concepts

What is flat (exact) vector search?

How SrvDB's default flat mode scores every stored vector by cosine similarity, what it costs per vector, and when exact search is the right choice.

Flat search compares the query with every stored vector and returns the k with the highest cosine similarity. It is SrvDB’s default mode: exact results, no index to build, and the baseline every other mode is measured against.

Flat search: the query is scored against every vector in blocks of 256, and a top-k heap keeps the best query vectors.bin, scanned in blocks of 256 (parallel) block 0 block 1 block ... cosine per vector top-k (exact)
Every live vector is read and scored; deleted ids are skipped.
  • Storage. Each vector is one fixed-size record of 4 × D bytes (little-endian f32) in vectors.bin, after a 24-byte versioned header. A .crc sidecar holds one CRC32C per 1 MiB block (ADR 0007), so damage is detected on open instead of returned as results.
  • Scan. The file is memory-mapped and scored in blocks of 256 vectors in parallel. Each block keeps its own best k, and the blocks are merged.
  • Scores. Results are true cosine similarities in [-1, 1].
  • Durability. After save() returns, every vector added before it survives a crash; see Durability and save().
import random
import srvdb
rng = random.Random(7)
vectors = [[rng.gauss(0, 1) for _ in range(128)] for _ in range(1000)]
ids = [f"doc-{i}" for i in range(len(vectors))]
db = srvdb.create("./flat-db", dimension=128, index="flat")
db.add(ids, vectors, ["{}"] * len(ids))
db.save()
top = db.search(vectors[42], k=3)
print(top[0][0], round(top[0][1], 3))
# doc-42 1.0
Flat
Recall 1.0 by definition: the ranking is exact
Search work Reads all n × 4 × D bytes for every query; grows linearly with n
Bytes per vector 4 × D: 512 at D = 128, 1536 at D = 384, 3072 at D = 768
Build or training None
  • Collections where a linear scan is fast enough for you: measure your own query latency first, because it is often the case for local data.
  • Whenever results must be exact, or as ground truth when you measure the recall of HNSW, SQ8, PQ or RaBitQ.
  • Move on when the scan is too slow (try RaBitQ or HNSW) or the files are too large (try SQ8 or PQ). Choosing an index mode shows a recall check.

Yes. Every live vector is scored by cosine similarity against the query, so the top k results are the true nearest neighbours.

Does flat mode need an index build or training?

Section titled “Does flat mode need an index build or training?”

No. Vectors are searchable as soon as add() returns; there is nothing to build, train or tune.

How much disk does flat mode use per vector?

Section titled “How much disk does flat mode use per vector?”

4 bytes per dimension in vectors.bin, for example 3072 bytes at dimension 768, plus metadata and a small checksum sidecar.

Next: What is HNSW?

Exact search needs no paper; every approximate method on these pages is judged against it. The concept pages for each approximate mode list the papers behind them.