Skip to main content

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 OptionsDirect link to Constructor Options

url:

string
Upstash Vector database URL

token:

string
Upstash Vector API token

MethodsDirect link to Methods

createIndex()Direct link to createIndex()

Note: This method is a no-op for Upstash as indexes are created automatically.

indexName:

string
Name of the index to create

dimension:

number
Vector dimension (must match your embedding model)

metric?:

'cosine' | 'euclidean' | 'dotproduct'
= cosine
Distance metric for similarity search

upsert()Direct link to upsert()

indexName:

string
Name of the index to upsert into

vectors:

number[][]
Array of embedding vectors

sparseVectors?:

{ indices: number[], values: number[] }[]
Array of sparse vectors for hybrid search. Each sparse vector must have matching indices and values arrays.

metadata?:

Record<string, any>[]
Metadata for each vector

ids?:

string[]
Optional vector IDs (auto-generated if not provided)

query()Direct link to query()

indexName:

string
Name of the index to query

queryVector:

number[]
Query vector to find similar vectors

sparseVector?:

{ indices: number[], values: number[] }
Optional sparse vector for hybrid search. Must have matching indices and values arrays.

topK?:

number
= 10
Number of results to return

filter?:

Record<string, any>
Metadata filters for the query

includeVector?:

boolean
= false
Whether to include vectors in the results

fusionAlgorithm?:

FusionAlgorithm
Algorithm used to combine dense and sparse search results in hybrid search (e.g., RRF - Reciprocal Rank Fusion)

queryMode?:

QueryMode
Search mode: 'DENSE' for dense-only, 'SPARSE' for sparse-only, or 'HYBRID' for combined search

listIndexes()Direct link to listIndexes()

Returns an array of index names (namespaces) as strings.

describeIndex()Direct link to describeIndex()

indexName:

string
Name of the index to describe

Returns:

interface IndexStats {
dimension: number;
count: number;
metric: "cosine" | "euclidean" | "dotproduct";
}

deleteIndex()Direct link to deleteIndex()

indexName:

string
Name of the index (namespace) to delete

updateVector()Direct link to updateVector()

indexName:

string
Name of the index to update

id:

string
ID of the item to update

update:

object
Update object containing vector, sparse vector, and/or metadata

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 with indices and values arrays for hybrid indexes.
  • metadata (optional): A record of key-value pairs for metadata.

deleteVector()Direct link to deleteVector()

indexName:

string
Name of the index from which to delete the item

id:

string
ID of the item to delete

Attempts to delete an item by its ID from the specified index. Logs an error message if the deletion fails.

Upstash Vector supports hybrid search that combines semantic search (dense vectors) with keyword-based search (sparse vectors) for improved relevance and accuracy.

Basic Hybrid UsageDirect link to Basic Hybrid Usage

import { UpstashVector } from "@mastra/upstash";

const vectorStore = new UpstashVector({
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 OptionsDirect link to Advanced 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 VectorsDirect link to Updating 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 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
}

Error HandlingDirect link to Error Handling

The store throws typed errors that can be caught:

try {
await store.query({
indexName: "index_name",
queryVector: queryVector,
});
} catch (error) {
if (error instanceof VectorStoreError) {
console.log(error.code); // 'connection_failed' | 'invalid_dimension' | etc
console.log(error.details); // Additional error context
}
}

Environment VariablesDirect link to Environment Variables

Required environment variables:

  • UPSTASH_VECTOR_URL: Your Upstash Vector database URL
  • UPSTASH_VECTOR_TOKEN: Your Upstash Vector API token