---
title: "Vector Search"
---

# Vector Search

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

## Creating Vector Tables

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

## Inserting Vectors

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

## Vector Search

### Nearest Neighbor (Euclidean)

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

### Cosine Similarity

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

## Creating Vector Indexes

### HNSW Index

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

### IVF Index (Large Scale)

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

## Vector Operators

| Operator | Description |
|----------|-------------|
| `<->` | Euclidean distance |
| `<=>` | Cosine distance |
| `<#>` | Negative inner product |

## Hybrid Search

```sql
SELECT content FROM documents
WHERE category = 'tech'
ORDER BY embedding <-> '[0.1, 0.2, ...]'
LIMIT 10;
```

## Tuning Performance

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

## REPL Commands

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

## Related

- [SQL Reference](https://www.heliosdb.com/docs/lite/user-guide/sql-reference/) - Vector syntax
- [API Reference](https://www.heliosdb.com/docs/lite/api/rest-api/) - Vector API
