docs(en): Update English docs for Vector SQL Integration
- docs/en/vector.md — add SQL usage section (CREATE TABLE VECTOR, distance functions, <-> operator, CREATE INDEX USING hnsw) - docs/en/baraql.md — update vector search section with real SQL syntax, add VECTOR(n) to data types, update keyword table - docs/en/changelog.md — add Vector SQL Integration and bugfixes to [Unreleased] - docs/ARCHITECTURE.md — add SQL Integration bullet to Vector Engine - README.md — update vector engine section with SQL examples, add Vector SQL to roadmap, bump test count to 340+
This commit is contained in:
+89
-8
@@ -1,8 +1,89 @@
|
||||
# Vector Search Engine
|
||||
|
||||
Native HNSW and IVF-PQ indexes for similarity search.
|
||||
Native HNSW and IVF-PQ indexes for similarity search with full SQL integration.
|
||||
|
||||
## Usage
|
||||
## SQL Usage
|
||||
|
||||
### Creating Vector Columns
|
||||
|
||||
```sql
|
||||
CREATE TABLE items (
|
||||
id INT PRIMARY KEY,
|
||||
embedding VECTOR(768)
|
||||
);
|
||||
```
|
||||
|
||||
The `VECTOR(n)` type stores float32 arrays of fixed dimension `n`.
|
||||
|
||||
### Inserting Vectors
|
||||
|
||||
```sql
|
||||
INSERT INTO items (id, embedding) VALUES (1, '[0.1, 0.2, 0.3, ...]');
|
||||
```
|
||||
|
||||
### Vector Distance Functions
|
||||
|
||||
```sql
|
||||
-- Cosine distance (0 = identical, 1 = orthogonal)
|
||||
SELECT id, cosine_distance(embedding, '[0.1, 0.2, 0.3]') AS dist
|
||||
FROM items;
|
||||
|
||||
-- Euclidean / L2 distance
|
||||
SELECT id, euclidean_distance(embedding, '[0.1, 0.2, 0.3]') AS dist
|
||||
FROM items;
|
||||
|
||||
-- L2 distance with <-> operator
|
||||
SELECT id, embedding <-> '[0.1, 0.2, 0.3]' AS dist
|
||||
FROM items;
|
||||
|
||||
-- Inner product (negative dot product for minimization)
|
||||
SELECT id, inner_product(embedding, '[0.1, 0.2, 0.3]') AS dist
|
||||
FROM items;
|
||||
|
||||
-- Manhattan / L1 distance
|
||||
SELECT id, l1_distance(embedding, '[0.1, 0.2, 0.3]') AS dist
|
||||
FROM items;
|
||||
```
|
||||
|
||||
### Nearest Neighbor Search
|
||||
|
||||
```sql
|
||||
-- Top-10 nearest neighbors by cosine distance
|
||||
SELECT id FROM items
|
||||
ORDER BY cosine_distance(embedding, '[0.1, 0.2, 0.3]') ASC
|
||||
LIMIT 10;
|
||||
|
||||
-- Top-5 nearest neighbors by Euclidean distance
|
||||
SELECT id FROM items
|
||||
ORDER BY embedding <-> '[0.1, 0.2, 0.3]'
|
||||
LIMIT 5;
|
||||
```
|
||||
|
||||
### Vector Indexes
|
||||
|
||||
```sql
|
||||
-- Create HNSW index for approximate nearest neighbor search
|
||||
CREATE INDEX idx_items_vec ON items(embedding) USING hnsw;
|
||||
|
||||
-- The index is automatically maintained on INSERT and UPDATE
|
||||
```
|
||||
|
||||
Supported index methods:
|
||||
- `USING hnsw` — Hierarchical Navigable Small World (default: cosine metric)
|
||||
- `USING ivfpq` — Inverted File with Product Quantization
|
||||
|
||||
### Dimension Validation
|
||||
|
||||
BaraDB validates vector dimensions at insert time:
|
||||
|
||||
```sql
|
||||
-- This will fail: expected 768 dimensions but got 3
|
||||
INSERT INTO items (id, embedding) VALUES (2, '[1.0, 2.0, 3.0]');
|
||||
```
|
||||
|
||||
## Native Nim API
|
||||
|
||||
For embedded or high-performance use, use the native Nim API directly:
|
||||
|
||||
```nim
|
||||
import barabadb/vector/engine
|
||||
@@ -48,12 +129,12 @@ var ivfpq = newIVFPQIndex(
|
||||
|
||||
## Distance Metrics
|
||||
|
||||
| Metric | Description |
|
||||
|--------|-------------|
|
||||
| `cosine` | Cosine similarity |
|
||||
| `euclidean` | L2 distance |
|
||||
| `dotproduct` | Dot product similarity |
|
||||
| `manhattan` | L1 distance |
|
||||
| Metric | SQL Function | Description |
|
||||
|--------|--------------|-------------|
|
||||
| `cosine` | `cosine_distance(a, b)` | Cosine dissimilarity (1 - similarity) |
|
||||
| `euclidean` | `euclidean_distance(a, b)` / `<->` | L2 distance |
|
||||
| `dotproduct` | `inner_product(a, b)` | Negative dot product |
|
||||
| `manhattan` | `l1_distance(a, b)` | L1 distance |
|
||||
|
||||
## Quantization
|
||||
|
||||
|
||||
Reference in New Issue
Block a user