跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Astra vector store

The AstraVector class provides vector search using DataStax Astra DB, a cloud-native, serverless database built on Apache Cassandra. It provides vector search capabilities with enterprise-grade capacity and high availability.

Constructor options
Constructor options的直接链接

token:

string
Astra DB API token

endpoint:

string
Astra DB API endpoint

keyspace?:

string
Optional keyspace name

Methods
Methods的直接链接

createIndex()
createindex的直接链接

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 (maps to dot_product for dotproduct)

upsert()
upsert的直接链接

indexName:

string
Name of the index to upsert into

vectors:

number[][]
Array of embedding vectors

metadata?:

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

ids?:

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

query()
query的直接链接

indexName:

string
Name of the index to query

queryVector:

number[]
Query vector to find similar vectors

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

listIndexes()
listindexes的直接链接

Returns an array of index names as strings.

describeIndex()
describeindex的直接链接

indexName:

string
Name of the index to describe

Returns:

interface IndexStats {
dimension: number
count: number
metric?: 'cosine' | 'euclidean' | 'dotproduct'
}

deleteIndex()
deleteindex的直接链接

indexName:

string
Name of the index to delete

updateVector()
updatevector的直接链接

indexName:

string
Name of the index containing the vector

id:

string
ID of the vector to update

update:

object
Update object containing vector and/or metadata changes
number[]
Record<string, any>

deleteVector()
deletevector的直接链接

indexName:

string
Name of the index containing the vector

id:

string
ID of the vector to delete

Response types
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
document?: string // Only returned by stores that keep document content
}

Error handling
Error 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_ASTRA_QUERY_DB_ERROR'
console.log(error.domain) // 'MASTRA_VECTOR'
console.log(error.category) // 'USER' | 'THIRD_PARTY'
console.log(error.details) // Additional error context
}
}

Environment variables
Environment variables的直接链接

Required environment variables:

  • ASTRA_DB_TOKEN: Your Astra DB API token
  • ASTRA_DB_ENDPOINT: Your Astra DB API endpoint