Qdrant Vector 存储
QdrantVector 类使用 Vector 相似度搜索引擎 Qdrant 提供 Vector 搜索。 它提供可用于生产环境的服务,并提供便捷的 API 来存储、搜索和管理 Vector,同时支持额外的 payload 和扩展筛选功能。
构造函数选项构造函数选项的直接链接
url:
string
Qdrant 实例的 REST URL。例如 https://xyz-example.eu-central.aws.cloud.qdrant.io:6333
apiKey:
string
可选的 Qdrant API 密钥
https:
boolean
建立连接时是否使用 TLS。建议启用。
方法方法的直接链接
createIndex()createindex的直接链接
indexName:
string
要创建的索引名称
dimension:
number
Vector 维度(必须与嵌入模型匹配)。单 Vector collection 必须提供此项。
metric?:
'cosine' | 'euclidean' | 'dotproduct'
= cosine
相似度搜索使用的距离度量
namedVectors?:
Record<string, { size: number; distance: 'cosine' | 'euclidean' | 'dotproduct' }>
命名 Vector 空间的配置。提供此项时,将创建包含多个命名 Vector 字段的 collection。
创建命名 Vector collection创建命名 Vector collection的直接链接
// Create a collection with multiple named vector spaces
await store.createIndex({
indexName: 'multi_modal',
dimension: 768, // fallback
namedVectors: {
text: { size: 768, distance: 'cosine' },
image: { size: 512, distance: 'euclidean' },
},
})
upsert()upsert的直接链接
indexName:
string
要 upsert 数据的索引名称
vectors:
number[][]
嵌入 Vector 数组
metadata?:
Record<string, any>[]
每个 Vector 的元数据
ids?:
string[]
可选的 Vector ID(未提供时自动生成)
vectorName?:
string
使用命名 Vector 时,要 upsert 数据的 Vector 空间名称。
Upsert 到命名 Vector 空间Upsert 到命名 Vector 空间的直接链接
// Upsert into the "text" vector space
await store.upsert({
indexName: 'multi_modal',
vectors: textEmbeddings,
metadata: textMetadata,
vectorName: 'text',
})
// Upsert into the "image" vector space
await store.upsert({
indexName: 'multi_modal',
vectors: imageEmbeddings,
metadata: imageMetadata,
vectorName: 'image',
})
query()query的直接链接
indexName:
string
要查询的索引名称
queryVector:
number[]
用于查找相似 Vector 的查询 Vector
topK?:
number
= 10
要返回的结果数量
filter?:
Record<string, any>
查询使用的元数据筛选条件
includeVector?:
boolean
= false
结果中是否包含 Vector
using?:
string
使用命名 Vector 时要查询的 Vector 字段名称。当 collection 包含多个命名 Vector 字段时,请使用此项。
命名 Vector命名 Vector的直接链接
Qdrant 支持每个 collection 包含多个 Vector,每个 Vector 字段都有一个名称。使用 using 参数选择要查询的 Vector 字段:
const results = await store.query({
indexName: 'my_index',
queryVector: embedding,
topK: 10,
using: 'title_embedding', // Query against a specific named vector
})
listIndexes()listindexes的直接链接
返回由索引名称字符串组成的数组。
describeIndex()describeindex的直接链接
indexName:
string
要描述的索引名称
返回:
interface IndexStats {
dimension: number
count: number
metric: 'cosine' | 'euclidean' | 'dotproduct'
}
deleteIndex()deleteindex的直接链接
indexName:
string
要删除的索引名称
updateVector()updatevector的直接链接
按 ID 或元数据筛选条件更新单个 Vector。必须提供 id 或 filter,但不能同时提供两者。
indexName:
string
要更新的索引名称
id?:
string
要更新的 Vector ID(不能与 filter 同时使用)
filter?:
Record<string, any>
用于标识待更新 Vector 的元数据筛选条件(不能与 id 同时使用)
update:
{ vector?: number[]; metadata?: Record<string, any>; }
包含待更新 Vector 和/或元数据的对象
更新指定索引中 Vector 和/或其元数据。如果同时提供 Vector 和元数据,两者都会更新。仅提供其中一个值时,则只更新该值。
deleteVector()deletevector的直接链接
indexName:
string
要从中删除 Vector 的索引名称
id:
string
要删除的 Vector ID
按 ID 从指定索引中删除 Vector。
deleteVectors()deletevectors的直接链接
按 ID 或元数据筛选条件删除多个 Vector。必须提供 ids 或 filter,但不能同时提供两者。
indexName:
string
包含待删除 Vector 的索引名称
ids?:
string[]
要删除的 Vector ID 数组(不能与 filter 同时使用)
filter?:
Record<string, any>
用于标识待删除 Vector 的元数据筛选条件(不能与 ids 同时使用)
createPayloadIndex()createpayloadindex的直接链接
在 collection 字段上创建 payload(元数据)索引,以实现高效筛选。对于 Qdrant Cloud 以及任何设置了 strict_mode_config = true 的 Qdrant 实例,此操作为必需。
indexName:
string
要在其上创建 payload 索引的 collection 名称
fieldName:
string
要建立索引的 payload 字段名称
fieldSchema:
'keyword' | 'integer' | 'float' | 'geo' | 'text' | 'bool' | 'datetime' | 'uuid'
payload 字段的 schema 类型
wait?:
boolean
= true
是否等待操作完成
// Create a keyword index for filtering by source
await store.createPayloadIndex({
indexName: 'my_index',
fieldName: 'source',
fieldSchema: 'keyword',
})
const results = await store.query({
indexName: 'my_index',
queryVector: queryVector,
filter: { source: 'document-a' },
})
deletePayloadIndex()deletepayloadindex的直接链接
从 collection 字段中移除 payload 索引。
indexName:
string
要从中删除 payload 索引的 collection 名称
fieldName:
string
要删除的 payload 字段索引名称
wait?:
boolean
= true
是否等待操作完成
响应类型响应类型的直接链接
查询结果以以下格式返回:
interface QueryResult {
id: string
score: number
metadata: Record<string, any>
vector?: number[] // Only included if includeVector is true
}
错误处理错误处理的直接链接
存储会抛出可捕获的类型化错误:
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
}
}