Vector search
The vec/ sub-package is a typed Table API over sqlite-vec (bundled by modernc.org/sqlite/vec). Blank-importing vec auto-registers the extension on every connection.
import ( _ "gosqlite.org" "gosqlite.org/vec")
tbl, _ := vec.Create(ctx, db, "docs", 8, vec.Options{Metric: vec.Cosine})tbl.BatchInsert(ctx, items)for m, err := range tbl.KNN(ctx, query, 5) { // streaming iter.Seq2 if err != nil { return err } fmt.Println(m.Rowid, m.Distance)}KNNSlice is the non-streaming form. Runnable: examples/features/search/vec-search/.
Capabilities
Section titled “Capabilities”- Metrics: L2, Cosine, Dot (an alias for L1), Hamming.
- Encodings: JSON, binary, and quantized
int8/bitfor compact storage. - Columns: metadata, partition-key, and auxiliary columns, plus
chunk_sizetuning. - Filtering:
WithFilter(predicate, args...)pushes a non-vector constraint into the KNN query (evaluated alongside MATCH). - Keyed tables:
KeyedTable[K]keys the search on a string / UUID instead of an int rowid — seeexamples/features/search/vec-keyed/. - Escape hatch:
KNNSQLreturns the generated SQL if you need to compose it by hand.
Quirks worth knowing
Section titled “Quirks worth knowing”INSERT OR REPLACEis not honored by vec0 — use(*Table).Updatefor in-place replacement.- Metadata / partition columns reject
NULL(only auxiliary columns allow it). bit[N]needsN % 8 == 0;chunk_sizemust be a multiple of 8.- The
LIMIT/kis inlined as a literal because sqlite-vec’s planner needs it visible.
The full matrix of column options, metrics, encodings, and KNN forms is in dev/coverage/vec.md. For combining vector + lexical results, see Hybrid search. For the gorm sidecar, see gorm.