メインコンテンツへ移動

Lance vector store

LanceVectorStore クラスは、Lance カラム形式を基盤とする組み込みベクトルデータベース LanceDB を使用したベクトル検索を提供します。ローカル開発と本番デプロイの両方で、効率的なストレージと高速な類似度検索を利用できます。

ファクトリーメソッド
ファクトリーメソッドへの直接リンク

LanceVectorStore の作成にはファクトリーパターンを使用します。コンストラクターを直接呼び出さず、静的 create() メソッドを使用してください。

uri:

string
LanceDB データベースのパス、またはクラウドデプロイ用 URI

options?:

ConnectionOptions
LanceDB の追加接続オプション

コンストラクターの例
コンストラクターの例への直接リンク

静的 create メソッドを使用して LanceVectorStore インスタンスを作成できます。

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()
createindexへの直接リンク

tableName:

string
インデックスを作成するテーブルの名前

indexName:

string
作成するインデックス(カラム)の名前

dimension:

number
ベクトルの次元数(埋め込みモデルと一致させる必要があります)

metric?:

'cosine' | 'euclidean' | 'dotproduct'
= cosine
類似度検索の距離指標

indexConfig?:

LanceIndexConfig
= { type: 'hnsw' }
インデックス設定

LanceIndexConfig
lanceindexconfigへの直接リンク

type:

'ivfflat' | 'hnsw'
= hnsw
インデックス種別
string

ivfflat:

ivfflat
近似検索のためにベクトルをリストへクラスタリングします。

hnsw:

hnsw
高速な検索と高い再現率を実現するグラフベースのインデックスです。

numPartitions?:

number
= 128
IVF インデックスのパーティション数

numSubVectors?:

number
= 16
直積量子化のサブベクトル数

hnsw?:

HNSWConfig
HNSW 設定
object

m?:

number
ノードあたりの最大接続数(デフォルト: 16)

efConstruction?:

number
構築時の複雑度(デフォルト: 100)

createTable()
createtableへの直接リンク

tableName:

string
作成するテーブルの名前

data:

Record<string, unknown>[] | TableLike
テーブルの初期データ

options?:

Partial<CreateTableOptions>
テーブル作成の追加オプション

upsert()
upsertへの直接リンク

tableName:

string
ベクトルを upsert するテーブルの名前

vectors:

number[][]
埋め込みベクトルの配列

metadata?:

Record<string, any>[]
各ベクトルのメタデータ

ids?:

string[]
省略可能なベクトル ID(未指定の場合は自動生成されます)

query()
queryへの直接リンク

tableName:

string
クエリ対象のテーブル名

queryVector:

number[]
クエリベクトル

topK?:

number
= 10
返す結果の数

filter?:

Record<string, any>
メタデータフィルター

includeVector?:

boolean
= false
結果にベクトルを含めるかどうか

columns?:

string[]
= []
結果に含める特定のカラム

includeAllColumns?:

boolean
= false
結果にすべてのカラムを含めるかどうか

listTables()
listtablesへの直接リンク

テーブル名を文字列の配列として返します。

const tables = await vectorStore.listTables()
// ['my_vectors', 'embeddings', 'documents']

getTableSchema()
gettableschemaへの直接リンク

tableName:

string
詳細を取得するテーブルの名前

指定したテーブルのスキーマを返します。

deleteTable()
deletetableへの直接リンク

tableName:

string
削除するテーブルの名前

deleteAllTables()
deletealltablesへの直接リンク

データベース内のすべてのテーブルを削除します。

listIndexes()
listindexesへの直接リンク

インデックス名を文字列の配列として返します。

describeIndex()
describeindexへの直接リンク

indexName:

string
詳細を取得するインデックスの名前

インデックスの情報を返します。

interface IndexStats {
dimension: number
count: number
metric: 'cosine' | 'euclidean' | 'dotproduct'
type: 'ivfflat' | 'hnsw'
config: {
m?: number
efConstruction?: number
numPartitions?: number
numSubVectors?: number
}
}

deleteIndex()
deleteindexへの直接リンク

indexName:

string
削除するインデックスの名前

updateVector()
updatevectorへの直接リンク

ID またはメタデータフィルターで単一のベクトルを更新します。idfilter のいずれか一方だけを指定する必要があります。

indexName:

string
ベクトルを含むインデックスの名前

id?:

string
更新するベクトルの ID(filter とは同時に指定できません)

filter?:

Record<string, any>
更新するベクトルを特定するメタデータフィルター(id とは同時に指定できません)

update:

{ vector?: number[]; metadata?: Record<string, any>; }
更新するベクトルやメタデータを含むオブジェクト

deleteVector()
deletevectorへの直接リンク

indexName:

string
ベクトルを含むインデックスの名前

id:

string
削除するベクトルの ID

deleteVectors()
deletevectorsへの直接リンク

ID またはメタデータフィルターで複数のベクトルを削除します。idsfilter のいずれか一方だけを指定する必要があります。

indexName:

string
ベクトルを含むインデックスの名前s to delete

ids?:

string[]
削除するベクトル ID の配列(filter とは同時に指定できません)

filter?:

Record<string, any>
削除するベクトルを特定するメタデータフィルター(ids とは同時に指定できません)

close()
closeへの直接リンク

データベース接続を閉じます。

レスポンス型
レスポンス型への直接リンク

クエリ結果は次の形式で返されます。

interface QueryResult {
id: string
score: number
metadata: Record<string, any>
vector?: number[] // Only included if includeVector is true
document?: string // Document text if available
}

エラー処理
エラー処理への直接リンク

store は捕捉可能な型付きエラーをスローします。

try {
await store.query({
tableName: 'my_vectors',
queryVector: queryVector,
})
} catch (error) {
if (error instanceof Error) {
console.log(error.message)
}
}

ベストプラクティス
ベストプラクティスへの直接リンク

  • ユースケースに適したインデックス種別を使用します。
    • メモリに制約がない場合は、再現率とパフォーマンスに優れる HNSW を使用します
    • 大規模なデータセットでメモリ効率を高めるには IVF を使用します
  • 大規模なデータセットでパフォーマンスを最適化するには、numPartitionsnumSubVectors の値の調整を検討してください
  • データベースの使用後は close() メソッドで接続を適切に閉じてください
  • フィルタリング操作を簡素化するため、一貫したスキーマでメタデータを保存してください