DuckDBVector Store
The DuckDB storage implementation provides an embedded high-performance vector search solution using DuckDB, an in-process analytical database. It uses the VSS extension for vector similarity search with HNSW indexing, offering a lightweight and efficient vector database that requires no external server.
It's part of the @mastra/duckdb package and offers efficient vector similarity search with metadata filtering.
InstallationDirect link to Installation
npm install @mastra/duckdb@beta
UsageDirect link to Usage
import { DuckDBVector } from "@mastra/duckdb";
// Create a new vector store instance
const store = new DuckDBVector({
id: "duckdb-vector",
path: ":memory:", // or './vectors.duckdb' for file persistence
});
// Create an index
await store.createIndex({
indexName: "myCollection",
dimension: 1536,
metric: "cosine",
});
// Add vectors with metadata
const vectors = [[0.1, 0.2, ...], [0.3, 0.4, ...]];
const metadata = [
{ text: "first document", category: "A" },
{ text: "second document", category: "B" },
];
await store.upsert({
indexName: "myCollection",
vectors,
metadata,
});
// Query similar vectors
const queryVector = [0.1, 0.2, ...];
const results = await store.query({
indexName: "myCollection",
queryVector,
topK: 10,
filter: { category: "A" },
});
// Clean up
await store.close();
Constructor OptionsDirect link to Constructor Options
id:
path?:
dimensions?:
metric?:
MethodsDirect link to Methods
createIndex()Direct link to createIndex()
Creates a new vector collection with optional HNSW index for fast approximate nearest neighbor search.
indexName:
dimension:
metric?:
upsert()Direct link to upsert()
Adds or updates vectors and their metadata in the index.
indexName:
vectors:
metadata?:
ids?:
query()Direct link to query()
Searches for similar vectors with optional metadata filtering.
indexName:
queryVector:
topK?:
filter?:
includeVector?:
describeIndex()Direct link to describeIndex()
Gets information about an index.
indexName:
Returns:
interface IndexStats {
dimension: number;
count: number;
metric: "cosine" | "euclidean" | "dotproduct";
}
deleteIndex()Direct link to deleteIndex()
Deletes an index and all its data.
indexName:
listIndexes()Direct link to listIndexes()
Lists all vector indexes in the database.
Returns: Promise<string[]>
updateVector()Direct link to updateVector()
Update a single vector by ID or by metadata filter. Either id or filter must be provided, but not both.
indexName:
id?:
filter?:
update:
update.vector?:
update.metadata?:
deleteVector()Direct link to deleteVector()
Deletes a specific vector entry from an index by its ID.
indexName:
id:
deleteVectors()Direct link to deleteVectors()
Delete multiple vectors by IDs or by metadata filter. Either ids or filter must be provided, but not both.
indexName:
ids?:
filter?:
close()Direct link to close()
Closes the database connection and releases resources.
await store.close();
Response TypesDirect link to Response Types
Query results are returned in this format:
interface QueryResult {
id: string;
score: number;
metadata: Record<string, any>;
vector?: number[]; // Only included if includeVector is true
}
Filter OperatorsDirect link to Filter Operators
DuckDB vector store supports MongoDB-like filter operators:
| Category | Operators |
|---|---|
| Comparison | $eq, $ne, $gt, $gte, $lt, $lte |
| Logical | $and, $or, $not, $nor |
| Array | $in, $nin |
| Element | $exists |
| Text | $contains |
Filter ExamplesDirect link to Filter Examples
// Allegato operators
const results = await store.query({
indexName: "docs",
queryVector: [...],
filter: {
$and: [
{ category: "electronics" },
{ price: { $gte: 100, $lte: 500 } },
],
},
});
// Nested field access
const results = await store.query({
indexName: "docs",
queryVector: [...],
filter: { "user.profile.tier": "premium" },
});
Distance MetricsDirect link to Distance Metrics
| Metric | Description | Score Interpretation | Best For |
|---|---|---|---|
cosine | Cosine similarity | 0-1 (1 = most similar) | Text embeddings, normalized vectors |
euclidean | L2 distance | 0-∞ (0 = most similar) | Image embeddings, spatial data |
dotproduct | Inner product | Higher = more similar | When vector magnitude matters |
Error HandlingDirect link to Error Handling
The store throws specific errors for different failure cases:
try {
await store.query({
indexName: "my-collection",
queryVector: queryVector,
});
} catch (error) {
if (error.message.includes("not found")) {
console.error("The specified index does not exist");
} else if (error.message.includes("Invalid identifier")) {
console.error("Index name contains invalid characters");
} else {
console.error("Vector store error:", error.message);
}
}
Common error cases include:
- Invalid index name format
- Index/table not found
- Dimension mismatch between query vector and index
- Empty filter or ids array in delete/update operations
- Mutual exclusivity violations (providing both
idandfilter)
Use CasesDirect link to Use Cases
Embedded Semantic SearchDirect link to Embedded Semantic Search
Build offline-capable AI applications with semantic search that runs entirely in-process:
const store = new DuckDBVector({
id: "offline-search",
path: "./search.duckdb",
});
Local RAG PipelinesDirect link to Local RAG Pipelines
Process sensitive documents locally without sending data to cloud vector databases:
const store = new DuckDBVector({
id: "private-rag",
path: "./confidential.duckdb",
dimensions: 1536,
});
Development and TestingDirect link to Development and Testing
Rapidly prototype vector search features with zero infrastructure:
const store = new DuckDBVector({
id: "dev-store",
path: ":memory:", // Fast in-memory for tests
});