Most retrieval projects I see start by choosing a vector database. Many of them would be served, for a long time, by a slice of floats and a loop.

The flat index

A flat index keeps every vector and compares the query with all of them. There is nothing to tune and nothing to rebuild, and its recall is exact by definition: every other index is measured against it.

func TopK(query []float32, vectors [][]float32, k int) []int {
	scores := make([]float32, len(vectors))
	for i, v := range vectors {
		var dot float32
		for j := range v {
			dot += query[j] * v[j]
		}
		scores[i] = dot
	}
	ids := make([]int, len(vectors))
	for i := range ids {
		ids[i] = i
	}
	slices.SortFunc(ids, func(a, b int) int { return cmp.Compare(scores[b], scores[a]) })
	return ids[:min(k, len(ids))]
}

With normalised vectors the dot product is the cosine similarity, so the loop above is the whole search.

When it stops being enough

What grows What it costs
The number of vectors Every query reads all of them
The dimensions Every comparison gets longer
The queries per second The same scan, once per query

A scan is memory bandwidth: the question is how many bytes each query has to read, and whether that fits the latency you promised.

Before reaching for an engine

  • Keep the vectors contiguous, so the scan streams through memory.
  • Quantise them, so each query reads fewer bytes.
  • Filter first on the metadata you already have, so the scan covers fewer vectors.

When none of these is enough, an approximate index earns its complexity, and the flat index stays as the reference its recall is checked against.