Upstash vector store
The UpstashVector class provides vector search using Upstash Vector, a serverless vector database service that provides vector similarity search with metadata filtering capabilities and hybrid search support.
Constructor optionsConstructor options的直接链接
id:
url:
token:
MethodsMethods的直接链接
createIndex()createindex的直接链接
Note: This method is a no-op for Upstash as indexes are created automatically.
indexName:
dimension:
metric?:
upsert()upsert的直接链接
indexName:
vectors:
sparseVectors?:
metadata?:
ids?:
query()query的直接链接
indexName:
queryVector:
sparseVector?:
topK?:
filter?:
includeVector?:
fusionAlgorithm?:
queryMode?:
listIndexes()listindexes的直接链接
Returns an array of index names (namespaces) as strings.
describeIndex()describeindex的直接链接
indexName:
Returns:
interface IndexStats {
dimension: number
count: number
metric: 'cosine' | 'euclidean' | 'dotproduct'
}
deleteIndex()deleteindex的直接链接
indexName:
updateVector()updatevector的直接链接
indexName:
id:
update:
The update object can have the following properties:
vector(optional): An array of numbers representing the new dense vector.sparseVector(optional): A sparse vector object withindicesandvaluesarrays for hybrid indexes.metadata(optional): A record of key-value pairs for metadata.
deleteVector()deletevector的直接链接
indexName:
id:
Attempts to delete an item by its ID from the specified index. Logs an error message if the deletion fails.
Hybrid vector searchHybrid vector search的直接链接
Upstash Vector supports hybrid search that combines semantic search (dense vectors) with keyword-based search (sparse vectors) for improved relevance and accuracy.
Basic Hybrid UsageBasic Hybrid Usage的直接链接
import { UpstashVector } from '@mastra/upstash'
const vectorStore = new UpstashVector({
id: 'upstash-vector',
url: process.env.UPSTASH_VECTOR_URL,
token: process.env.UPSTASH_VECTOR_TOKEN,
})
// Upsert vectors with both dense and sparse components
const denseVectors = [
[0.1, 0.2, 0.3],
[0.4, 0.5, 0.6],
]
const sparseVectors = [
{ indices: [1, 5, 10], values: [0.8, 0.6, 0.4] },
{ indices: [2, 6, 11], values: [0.7, 0.5, 0.3] },
]
await vectorStore.upsert({
indexName: 'hybrid-index',
vectors: denseVectors,
sparseVectors: sparseVectors,
metadata: [{ title: 'Document 1' }, { title: 'Document 2' }],
})
// Query with hybrid search
const results = await vectorStore.query({
indexName: 'hybrid-index',
queryVector: [0.1, 0.2, 0.3],
sparseVector: { indices: [1, 5], values: [0.9, 0.7] },
topK: 10,
})
Advanced Hybrid Search OptionsAdvanced Hybrid Search Options的直接链接
import { FusionAlgorithm, QueryMode } from '@upstash/vector'
// Query with specific fusion algorithm
const fusionResults = await vectorStore.query({
indexName: 'hybrid-index',
queryVector: [0.1, 0.2, 0.3],
sparseVector: { indices: [1, 5], values: [0.9, 0.7] },
fusionAlgorithm: FusionAlgorithm.RRF,
topK: 10,
})
// Dense-only search
const denseResults = await vectorStore.query({
indexName: 'hybrid-index',
queryVector: [0.1, 0.2, 0.3],
queryMode: QueryMode.DENSE,
topK: 10,
})
// Sparse-only search
const sparseResults = await vectorStore.query({
indexName: 'hybrid-index',
queryVector: [0.1, 0.2, 0.3], // Still required for index structure
sparseVector: { indices: [1, 5], values: [0.9, 0.7] },
queryMode: QueryMode.SPARSE,
topK: 10,
})
Updating Hybrid VectorsUpdating Hybrid Vectors的直接链接
// Update both dense and sparse components
await vectorStore.updateVector({
indexName: 'hybrid-index',
id: 'vector-id',
update: {
vector: [0.2, 0.3, 0.4],
sparseVector: { indices: [2, 7, 12], values: [0.9, 0.8, 0.6] },
metadata: { title: 'Updated Document' },
},
})
Response typesResponse 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
}
Error handlingError handling的直接链接
The store throws MastraError instances from @mastra/core/error:
import { MastraError } from '@mastra/core/error'
try {
await store.query({
indexName: 'index_name',
queryVector: queryVector,
})
} catch (error) {
if (error instanceof MastraError) {
console.log(error.id) // 'MASTRA_VECTOR_UPSTASH_QUERY_FAILED'
console.log(error.domain) // 'STORAGE'
console.log(error.category) // 'USER' | 'THIRD_PARTY'
console.log(error.details) // Additional error context
}
}
Environment variablesEnvironment variables的直接链接
Required environment variables:
UPSTASH_VECTOR_URL: Your Upstash Vector database URLUPSTASH_VECTOR_TOKEN: Your Upstash Vector API token
Usage exampleUsage example的直接链接
Local embeddings with fastembedLocal embeddings with fastembed的直接链接
Embeddings are numeric vectors used by memory's semanticRecall to retrieve related messages by meaning (not keywords). This setup uses @mastra/fastembed to generate vector embeddings.
Install fastembed to get started:
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/fastembed@latest
pnpm add @mastra/fastembed@latest
yarn add @mastra/fastembed@latest
bun add @mastra/fastembed@latest
Add the following to your agent:
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
import { UpstashStore, UpstashVector } from '@mastra/upstash'
import { fastembed } from '@mastra/fastembed'
export const upstashAgent = new Agent({
id: 'upstash-agent',
name: 'Upstash Agent',
instructions:
'You are an AI agent with the ability to automatically recall memories from previous interactions.',
model: 'openai/gpt-5.6-sol',
memory: new Memory({
storage: new UpstashStore({
id: 'upstash-agent-storage',
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
}),
vector: new UpstashVector({
id: 'upstash-agent-vector',
url: process.env.UPSTASH_VECTOR_REST_URL!,
token: process.env.UPSTASH_VECTOR_REST_TOKEN!,
}),
embedder: fastembed,
options: {
lastMessages: 10,
semanticRecall: {
topK: 3,
messageRange: 2,
},
},
}),
})