Skip to content

Vector Search

Built-in HNSW vector indexing for similarity search, RAG applications, and AI/ML workloads.

Creating Vector Tables

CREATE TABLE documents (
    id INTEGER PRIMARY KEY,
    content TEXT,
    embedding VECTOR(384)
);

Inserting Vectors

INSERT INTO documents (id, content, embedding)
VALUES (1, 'Machine learning basics', '[0.1, 0.2, 0.3, ...]');

Vector Search

Nearest Neighbor (Euclidean)

SELECT content FROM documents
ORDER BY embedding <-> '[0.15, 0.25, ...]'
LIMIT 10;

Cosine Similarity

SELECT content FROM documents
ORDER BY embedding <=> '[0.15, 0.25, ...]'
LIMIT 10;

Creating Vector Indexes

HNSW Index

CREATE INDEX docs_embedding_idx ON documents
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 200);

IVF Index (Large Scale)

CREATE INDEX docs_embedding_idx ON documents
USING ivf (embedding)
WITH (lists = 100, quantization = 'pq');

Vector Operators

Operator Description
<-> Euclidean distance
<=> Cosine distance
<#> Negative inner product
SELECT content FROM documents
WHERE category = 'tech'
ORDER BY embedding <-> '[0.1, 0.2, ...]'
LIMIT 10;

Tuning Performance

SET hnsw.ef_search = 100;  -- Higher = more accurate, slower
SET ivf.probes = 20;       -- More probes = more accurate

REPL Commands

\vectors              # List vector stores
\vector create docs 384 cosine
\vector stats docs