> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # Lance vector store LanceVectorStore クラスは、Lance カラム形式を基盤とする組み込みベクトルデータベース [LanceDB](https://lancedb.github.io/lancedb/) を使用したベクトル検索を提供します。ローカル開発と本番デプロイの両方で、効率的なストレージと高速な類似度検索を利用できます。 ## ファクトリーメソッド LanceVectorStore の作成にはファクトリーパターンを使用します。コンストラクターを直接呼び出さず、静的 `create()` メソッドを使用してください。 **uri** (`string`): LanceDB データベースのパス、またはクラウドデプロイ用 URI **options** (`ConnectionOptions`): LanceDB の追加接続オプション ## コンストラクターの例 静的 create メソッドを使用して `LanceVectorStore` インスタンスを作成できます。 ```ts import { LanceVectorStore } from '@mastra/lance' // Connect to a local database const vectorStore = await LanceVectorStore.create('/path/to/db') // Connect to a LanceDB cloud database const cloudStore = await LanceVectorStore.create('db://host:port') // Connect to a cloud database with options const s3Store = await LanceVectorStore.create('s3://bucket/db', { storageOptions: { timeout: '60s' }, }) ``` ## メソッド ### `createIndex()` **tableName** (`string`): インデックスを作成するテーブルの名前 **indexName** (`string`): 作成するインデックス(カラム)の名前 **dimension** (`number`): ベクトルの次元数(埋め込みモデルと一致させる必要があります) **metric** (`'cosine' | 'euclidean' | 'dotproduct'`): 類似度検索の距離指標 (Default: `cosine`) **indexConfig** (`LanceIndexConfig`): インデックス設定 (Default: `{ type: 'hnsw' }`) #### `LanceIndexConfig` **type** (`'ivfflat' | 'hnsw'`): インデックス種別 (Default: `hnsw`) **type.ivfflat** (`ivfflat`): 近似検索のためにベクトルをリストへクラスタリングします。 **type.hnsw** (`hnsw`): 高速な検索と高い再現率を実現するグラフベースのインデックスです。 **numPartitions** (`number`): IVF インデックスのパーティション数 (Default: `128`) **numSubVectors** (`number`): 直積量子化のサブベクトル数 (Default: `16`) **hnsw** (`HNSWConfig`): HNSW 設定 **hnsw\.m** (`number`): ノードあたりの最大接続数(デフォルト: 16) **hnsw\.efConstruction** (`number`): 構築時の複雑度(デフォルト: 100) ### `createTable()` **tableName** (`string`): 作成するテーブルの名前 **data** (`Record[] | TableLike`): テーブルの初期データ **options** (`Partial`): テーブル作成の追加オプション ### `upsert()` **tableName** (`string`): ベクトルを upsert するテーブルの名前 **vectors** (`number[][]`): 埋め込みベクトルの配列 **metadata** (`Record[]`): 各ベクトルのメタデータ **ids** (`string[]`): 省略可能なベクトル ID(未指定の場合は自動生成されます) ### `query()` **tableName** (`string`): クエリ対象のテーブル名 **queryVector** (`number[]`): クエリベクトル **topK** (`number`): 返す結果の数 (Default: `10`) **filter** (`Record`): メタデータフィルター **includeVector** (`boolean`): 結果にベクトルを含めるかどうか (Default: `false`) **columns** (`string[]`): 結果に含める特定のカラム (Default: `[]`) **includeAllColumns** (`boolean`): 結果にすべてのカラムを含めるかどうか (Default: `false`) ### `listTables()` テーブル名を文字列の配列として返します。 ```typescript const tables = await vectorStore.listTables() // ['my_vectors', 'embeddings', 'documents'] ``` ### `getTableSchema()` **tableName** (`string`): 詳細を取得するテーブルの名前 指定したテーブルのスキーマを返します。 ### `deleteTable()` **tableName** (`string`): 削除するテーブルの名前 ### `deleteAllTables()` データベース内のすべてのテーブルを削除します。 ### `listIndexes()` インデックス名を文字列の配列として返します。 ### `describeIndex()` **indexName** (`string`): 詳細を取得するインデックスの名前 インデックスの情報を返します。 ```typescript interface IndexStats { dimension: number count: number metric: 'cosine' | 'euclidean' | 'dotproduct' type: 'ivfflat' | 'hnsw' config: { m?: number efConstruction?: number numPartitions?: number numSubVectors?: number } } ``` ### `deleteIndex()` **indexName** (`string`): 削除するインデックスの名前 ### `updateVector()` ID またはメタデータフィルターで単一のベクトルを更新します。`id` と `filter` のいずれか一方だけを指定する必要があります。 **indexName** (`string`): ベクトルを含むインデックスの名前 **id** (`string`): 更新するベクトルの ID(filter とは同時に指定できません) **filter** (`Record`): 更新するベクトルを特定するメタデータフィルター(id とは同時に指定できません) **update** (`{ vector?: number[]; metadata?: Record; }`): 更新するベクトルやメタデータを含むオブジェクト ### `deleteVector()` **indexName** (`string`): ベクトルを含むインデックスの名前 **id** (`string`): 削除するベクトルの ID ### `deleteVectors()` ID またはメタデータフィルターで複数のベクトルを削除します。`ids` と `filter` のいずれか一方だけを指定する必要があります。 **indexName** (`string`): ベクトルを含むインデックスの名前s to delete **ids** (`string[]`): 削除するベクトル ID の配列(filter とは同時に指定できません) **filter** (`Record`): 削除するベクトルを特定するメタデータフィルター(ids とは同時に指定できません) ### `close()` データベース接続を閉じます。 ## レスポンス型 クエリ結果は次の形式で返されます。 ```typescript interface QueryResult { id: string score: number metadata: Record vector?: number[] // Only included if includeVector is true document?: string // Document text if available } ``` ## エラー処理 store は捕捉可能な型付きエラーをスローします。 ```typescript try { await store.query({ tableName: 'my_vectors', queryVector: queryVector, }) } catch (error) { if (error instanceof Error) { console.log(error.message) } } ``` ## ベストプラクティス - ユースケースに適したインデックス種別を使用します。 - メモリに制約がない場合は、再現率とパフォーマンスに優れる HNSW を使用します - 大規模なデータセットでメモリ効率を高めるには IVF を使用します - 大規模なデータセットでパフォーマンスを最適化するには、`numPartitions` と `numSubVectors` の値の調整を検討してください - データベースの使用後は `close()` メソッドで接続を適切に閉じてください - フィルタリング操作を簡素化するため、一貫したスキーマでメタデータを保存してください ## 関連項目 - [メタデータフィルター](https://mastra.zisheng.pro/ja/reference/rag/metadata-filters)