Amazon S3 vector store
S3Vectors クラスは、Amazon S3 Vectors(プレビュー)を使用したベクトル検索を提供します。ベクトルを vector bucket に保存し、JSON ベースのメタデータフィルターを使用して vector index で類似度検索を実行します。
Amazon S3 Vectors はプレビューサービスです。プレビュー機能は予告なく変更または削除される可能性があり、AWS SLA の対象外です。動作、制限、利用可能なリージョンはいつでも変更される可能性があります。このライブラリでは、AWS との整合性を維持するために破壊的変更が加えられる場合があります。
インストールインストールへの直接リンク
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/s3vectors@latest
pnpm add @mastra/s3vectors@latest
yarn add @mastra/s3vectors@latest
bun add @mastra/s3vectors@latest
使用例使用例への直接リンク
import { S3Vectors } from '@mastra/s3vectors'
const store = new S3Vectors({
vectorBucketName: process.env.S3_VECTORS_BUCKET_NAME!, // e.g. "my-vector-bucket"
clientConfig: {
region: process.env.AWS_REGION!, // credentials use the default AWS provider chain
},
// Optional: mark large/long-text fields as non-filterable at index creation time
nonFilterableMetadataKeys: ['content'],
})
// Create an index (names are normalized: "_" → "-" and lowercased)
await store.createIndex({
indexName: 'my_index',
dimension: 1536,
metric: 'cosine', // "euclidean" also supported; "dotproduct" is NOT supported
})
// Upsert vectors (ids auto-generated if omitted). Date values in metadata are serialized to epoch ms.
const ids = await store.upsert({
indexName: 'my_index',
vectors: [
[0.1, 0.2 /* … */],
[0.3, 0.4 /* … */],
],
metadata: [
{
text: 'doc1',
genre: 'documentary',
year: 2023,
createdAt: new Date('2024-01-01'),
},
{ text: 'doc2', genre: 'comedy', year: 2021 },
],
})
// Query with metadata filters (implicit AND is canonicalized)
const results = await store.query({
indexName: 'my-index',
queryVector: [0.1, 0.2 /* … */],
topK: 10, // Service-side limits may apply (commonly 30)
filter: { genre: { $in: ['documentary', 'comedy'] }, year: { $gte: 2020 } },
includeVector: false, // set true to include raw vectors (may trigger a secondary fetch)
})
// Clean up resources (closes the underlying HTTP handler)
await store.disconnect()
コンストラクターオプションコンストラクターオプションへの直接リンク
vectorBucketName:
clientConfig?:
region、credentials など)。nonFilterableMetadataKeys?:
content などの大きなテキストフィールドに使用します。メソッドメソッドへの直接リンク
createIndex()createindexへの直接リンク
設定した vector bucket に新しい vector index を作成します。インデックスがすでに存在する場合はスキーマを検証し、何も変更しません(既存の距離指標と次元数は維持されます)。
indexName:
dimension:
metric?:
dotproduct をサポートしていません。upsert()upsertへの直接リンク
ベクトルを追加または置換します(レコード全体を保存)。ids を指定しない場合は UUID が生成されます。
indexName:
vectors:
metadata?:
ids?:
query()queryへの直接リンク
省略可能なメタデータフィルターを使用して最近傍を検索します。
indexName:
queryVector:
topK?:
filter?:
$and、$or、$eq、$ne、$gt、$gte、$lt、$lte、$in、$nin、$exists をサポートする JSON ベースのメタデータフィルター。includeVector?:
結果には score = 1/(1 + distance) が含まれます。基になる距離の順位を維持しながら、値が大きいほど類似度が高くなります。
describeIndex()describeindexへの直接リンク
インデックスの情報を返します。
indexName:
戻り値:
interface IndexStats {
dimension: number
count: number // computed via ListVectors pagination (O(n))
metric: 'cosine' | 'euclidean'
}
deleteIndex()deleteindexへの直接リンク
インデックスとそのデータを削除します。
indexName:
listIndexes()listindexesへの直接リンク
設定した vector bucket 内のすべてのインデックスを一覧表示します。
戻り値: Promise<string[]>
updateVector()updatevectorへの直接リンク
インデックス内の特定の ID に対応するベクトルまたはメタデータを更新します。
indexName:
id:
update:
update.vector?:
update.metadata?:
deleteVector()deletevectorへの直接リンク
指定した ID のベクトルを削除します。
indexName:
id:
disconnect()disconnectへの直接リンク
基盤となる AWS SDK HTTP ハンドラーを閉じ、ソケットを解放します。
レスポンス型レスポンス型への直接リンク
クエリ結果は次の形式で返されます。
interface QueryResult {
id: string
score: number // 1/(1 + distance)
metadata: Record<string, any>
vector?: number[] // Only included if includeVector is true
}
フィルター構文フィルター構文への直接リンク
S3 Vectors がサポートする演算子と値の型は、厳密に制限されています。Mastra のフィルタートランスレーターは次のように動作します。
- 暗黙の AND を正規化:
{a:1,b:2}→{ $and: [{a:1},{b:2}] }。 - Date 値を正規化: 数値比較と配列要素ではエポックミリ秒に変換します。
- 等値条件(
field: valueまたは$eq/$ne)では Date を使用できません。等値には string | number | boolean の値のみ使用できます。 - 等値条件の null/undefined は拒否されます。配列の等値比較はサポートされていません(
$in/$ninを使用してください)。 - トップレベルの論理演算子として使用できるのは
$and/$orのみです。 - 論理演算子には、直接の演算子ではなくフィールド条件を含める必要があります。
サポートされる演算子:
- 論理:
$and、$or(空でない配列) - 基本:
$eq、$ne(string | number | boolean) - 数値:
$gt、$gte、$lt、$lte(number またはDate→ エポックミリ秒) - 配列:
$in、$nin(string | number | boolean の空でない配列。Date→ エポックミリ秒) - 要素:
$exists(boolean)
未サポート / 使用不可(拒否されます): $not、$nor、$regex、$all、$elemMatch、$size、$text など。
例:
// Implicit AND
{ genre: { $in: ["documentary", "comedy"] }, year: { $gte: 2020 } }
// Explicit logicals and ranges
{
$and: [
{ price: { $gte: 100, $lte: 1000 } },
{ $or: [{ stock: { $gt: 0 } }, { preorder: true }] }
]
}
// Dates in range (converted to epoch ms)
{ timestamp: { $gt: new Date("2024-01-01T00:00:00Z") } }
インデックス作成時に nonFilterableMetadataKeys を設定すると、それらのキーは保存されますが、フィルターには使用できません。
エラー処理エラー処理への直接リンク
store は捕捉可能な型付きエラーをスローします。
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
}
}
環境変数環境変数への直接リンク
アプリの接続に使用する一般的な環境変数:
S3_VECTORS_BUCKET_NAME: S3 の vector bucket 名(vectorBucketNameの設定に使用)。AWS_REGION: S3 Vectors bucket の AWS リージョン。- AWS 認証情報: 標準の AWS SDK Provider Chain(
AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY、AWS_PROFILEなど)を介して指定します。
ベストプラクティスベストプラクティスへの直接リンク
- 埋め込みモデルに合わせて距離指標(
cosineまたはeuclidean)を選択してください。dotproductはサポートされていません。 - フィルター可能なメタデータは小さく構造化された値(string/number/boolean)にしてください。大きなテキスト(
contentなど)はフィルター対象外として保存します。 - ネストされたメタデータにはドット区切りのパスを使用し、複雑なロジックには明示的な
$and/$orを使用してください。 - 頻繁に実行される処理で
describeIndex()を呼び出さないでください。countはページ分割されたListVectorsを使用して算出されます(O(n))。 - 生のベクトルが必要な場合にのみ
includeVector: trueを使用してください。