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

Cloudflare vector store

The CloudflareVector class provides vector search using Cloudflare Vectorize, a vector database service integrated with Cloudflare's edge network.

Constructor options
Constructor options的直接链接

accountId:

string
Cloudflare account ID

apiToken:

string
Cloudflare API token with Vectorize permissions

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 (dotproduct maps to dot-product)

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

createMetadataIndex()
createmetadataindex的直接链接

Creates an index on a metadata field to enable filtering.

indexName:

string
Name of the index containing the metadata field

propertyName:

string
Name of the metadata field to index

indexType:

'string' | 'number' | 'boolean'
Type of the metadata field

deleteMetadataIndex()
deletemetadataindex的直接链接

Removes an index from a metadata field.

indexName:

string
Name of the index containing the metadata field

propertyName:

string
Name of the metadata field to remove indexing from

listMetadataIndexes()
listmetadataindexes的直接链接

Lists all metadata field indexes for an index.

indexName:

string
Name of the index to list metadata indexes for

updateVector()
updatevector的直接链接

Updates the vector and optionally the metadata for a specific ID within an index. Vectorize has no partial-update operation, so update.vector is required. Metadata-only updates aren't supported and throw an error.

indexName:

string
Name of the index containing the ID to update

id:

string
Unique identifier of the vector or metadata to update

update:

{ vector: number[]; metadata?: Record<string, any>; }
Object containing the new vector values, and optionally the metadata to update. The vector values are required

deleteVector()
deletevector的直接链接

Deletes a vector and its associated metadata for a specific ID within an index.

indexName:

string
Name of the index containing the ID to delete

id:

string
Unique identifier of the vector and metadata to delete

deleteVectors()
deletevectors的直接链接

Deletes multiple vectors and their associated metadata by ID. Vectorize has no filtered-delete operation, so deleting by metadata filter isn't supported and throws an error.

indexName:

string
Name of the index containing the vectors to delete

ids:

string[]
Unique identifiers of the vectors to delete. Must contain at least one ID, and cannot be combined with filter

filter?:

Record<string, any>
Not supported by Vectorize. Passing a filter throws an error

Response types
Response types的直接链接

Query results are returned in this format:

interface QueryResult {
id: string
score: number
metadata: Record<string, any>
vector?: number[]
}

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

Environment variables
Environment variables的直接链接

Required environment variables:

  • CLOUDFLARE_ACCOUNT_ID: Your Cloudflare account ID
  • CLOUDFLARE_API_TOKEN: Your Cloudflare API token with Vectorize permissions