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.
How it works
Section titled “How it works”- Storage. Each vector is one fixed-size record of
4 × Dbytes (little-endian f32) invectors.bin, after a 24-byte versioned header. A.crcsidecar 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().
Try it in SrvDB
Section titled “Try it in SrvDB”import randomimport 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.0Trade-offs
Section titled “Trade-offs”| 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 |
When to choose it
Section titled “When to choose it”- 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.
Is flat search in SrvDB exact?
Section titled “Is flat search in SrvDB exact?”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.
Related concepts
Section titled “Related concepts”Next: What is HNSW?
Papers and further reading
Section titled “Papers and further reading”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.
Was this page helpful? Tell us in Discussions.