Skip to content

Vector Search

Zatabase provides first-class vector search powered by ZQL, Zatabase’s patent-pending query engine. ZQL uses a novel approach to similarity search that delivers exceptional speed and memory efficiency. Zatabase also supports dense f32 vectors with HNSW approximate nearest neighbor indexing and KNN query syntax through both SQL and REST APIs.

Define vector columns using the VECTOR(N) type, where N is the vector dimension:

CREATE TABLE embeddings (
title TEXT,
content TEXT,
embedding VECTOR(384)
);

Insert vectors using the ARRAY[...] literal syntax:

INSERT INTO embeddings (title, content, embedding)
VALUES (
'Machine Learning Basics',
'An introduction to ML concepts...',
ARRAY[0.12, -0.34, 0.56, ..., 0.78]
);

Zatabase supports three distance metrics for vector similarity:

MetricDescriptionUse Case
L2 (Euclidean)Straight-line distance between pointsGeneral-purpose, unnormalized vectors
CosineAngle between vectors (1 - cosine similarity)Text embeddings, normalized representations
Inner ProductDot product (negated for distance)Maximum inner product search, recommendation

Dense f32 distance computations are optimized for throughput. Zatabase selects the best available implementation at runtime for L2, cosine, and inner product metrics.

For exact K-nearest neighbor search, use the SQL distance operator with ORDER BY and LIMIT:

Terminal window
curl -s -X POST https://your-project.zatabase.io/v1/sql \
-H "Authorization: Bearer $ZATABASE_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"query": "SELECT * FROM embeddings ORDER BY embedding <-> ARRAY[0.1, 0.2, ...] LIMIT 10"
}'

Exact KNN computes the distance from the query vector to every row in the table. This guarantees perfect recall but is O(n) in the number of rows.

You can also use the dedicated KNN endpoint for batch operations:

Terminal window
curl -s -X POST https://your-project.zatabase.io/v1/knn \
-H "Authorization: Bearer $ZATABASE_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"table": "embeddings",
"column": "embedding",
"query": [0.1, 0.2, 0.3, ...],
"k": 10,
"metric": "cosine"
}'

For large datasets, build an HNSW index to enable sub-linear approximate nearest neighbor (ANN) search.

Terminal window
curl -s -X POST https://your-project.zatabase.io/v1/indexes/hnsw/build \
-H "Authorization: Bearer $ZATABASE_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"table": "embeddings",
"column": "embedding",
"metric": "cosine",
"m": 16,
"ef_construction": 200
}'
ParameterDefaultDescription
m16Maximum number of connections per node per layer. Higher values improve recall but increase memory and build time. Typical range: 12-48.
ef_construction200Size of the dynamic candidate list during index construction. Higher values improve index quality at the cost of slower builds. Typical range: 100-500.
metric"l2"Distance metric: "l2", "cosine", or "inner_product". Must match your data characteristics.

Tuning guidelines:

  • Small datasets (< 10K vectors): m=12, ef_construction=100 — fast builds, good recall
  • Medium datasets (10K-1M): m=16, ef_construction=200 — balanced performance
  • Large datasets (> 1M): m=32, ef_construction=400 — higher recall, slower builds
Terminal window
curl -s -X POST https://your-project.zatabase.io/v1/indexes/hnsw/search \
-H "Authorization: Bearer $ZATABASE_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"table": "embeddings",
"column": "embedding",
"query": [0.1, 0.2, 0.3, ...],
"k": 10,
"ef_search": 100
}'

The ef_search parameter controls the tradeoff between search accuracy and latency at query time:

ef_searchRecallLatencyUse Case
10~85%FastestReal-time suggestions, low-stakes retrieval
50~95%FastProduction search, recommendation engines
100~98%ModerateHigh-quality search (default)
200~99.5%SlowerNear-exact results, evaluation benchmarks
400+~100%SlowestWhen recall matters more than speed

ef_search must be >= k (the number of results requested). Setting ef_search = k gives the fastest but lowest-quality results.

List all HNSW indexes:

Terminal window
curl -s https://your-project.zatabase.io/v1/indexes/hnsw \
-H "Authorization: Bearer $ZATABASE_TOKEN" | jq

Get index statistics:

Terminal window
curl -s https://your-project.zatabase.io/v1/indexes/hnsw/embeddings/embedding/stats \
-H "Authorization: Bearer $ZATABASE_TOKEN" | jq

In addition to dense f32 vectors, Zatabase’s ZQL engine provides a patent-pending native similarity search that delivers ultra-fast comparisons with minimal memory overhead.

ZQL similarity search is accessed via the ZQL API:

Terminal window
# Similarity search using the ~ operator
curl -s -X POST https://your-project.zatabase.io/api/zql/search \
-H "Authorization: Bearer $ZATABASE_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"store": "documents",
"field": "content",
"value": "machine learning introduction",
"threshold": 0.6,
"limit": 10
}'

The threshold parameter (0.0 to 1.0) filters results by minimum similarity. Meaningful matches typically have similarity > 0.55.

  • ZQL native search: ZQL’s patent-pending representation enables extremely fast similarity search with minimal memory overhead
  • Dense f32 vectors: Standard floating-point vectors support L2, cosine, and inner product metrics with optimized distance computations
  • Batch processing: The HNSW search endpoint processes distance calculations in optimized batches with top-k selection
  • Memory efficiency: HNSW graphs are stored on disk and memory-mapped for large-scale deployments
  • O(1) exact match: The = operator provides instant exact matching, bypassing similarity search entirely