ElasticSearch 向量存储
🌐 ElasticSearch Vector Store
ElasticSearchVector 类使用 ElasticSearch 提供向量搜索,利用其 dense_vector 字段类型和 k-NN 搜索功能。它是 @mastra/elasticsearch 包的一部分。
🌐 The ElasticSearchVector class provides vector search using ElasticSearch with its dense_vector field type and k-NN search capabilities.
It's part of the @mastra/elasticsearch package.
安装Direct link to 安装
🌐 Installation
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/elasticsearch@latest
pnpm add @mastra/elasticsearch@latest
yarn add @mastra/elasticsearch@latest
bun add @mastra/elasticsearch@latest
用法Direct link to 用法
🌐 Usage
import { ElasticSearchVector } from "@mastra/elasticsearch";
const store = new ElasticSearchVector({
id: "elasticsearch-vector",
url: process.env.ELASTICSEARCH_URL,
});
// Create an index
await store.createIndex({
indexName: "my-collection",
dimension: 1536,
});
// Add vectors with metadata
const vectors = [[0.1, 0.2, ...], [0.3, 0.4, ...]];
const metadata = [
{ text: "first document", category: "A" },
{ text: "second document", category: "B" }
];
await store.upsert({
indexName: "my-collection",
vectors,
metadata,
});
// Query similar vectors
const results = await store.query({
indexName: "my-collection",
queryVector: [0.1, 0.2, ...],
topK: 10,
filter: { category: "A" },
});
构造函数选项Direct link to 构造函数选项
🌐 Constructor Options
id:
url:
方法Direct link to 方法
🌐 Methods
createIndex()Direct link to createIndex()
使用指定的配置创建一个新索引。
🌐 Creates a new index with the specified configuration.
indexName:
dimension:
metric?:
upsert()Direct link to upsert()
在索引中添加或更新向量及其元数据。
🌐 Adds or updates vectors and their metadata in the index.
indexName:
vectors:
metadata?:
ids?:
query()Direct link to query()
搜索具有可选元数据过滤的相似向量。
🌐 Searches for similar vectors with optional metadata filtering.
indexName:
queryVector:
topK?:
filter?:
includeVector?:
describeIndex()Direct link to describeIndex()
获取有关索引的信息。
🌐 Gets information about an index.
indexName:
返回:
🌐 Returns:
interface IndexStats {
dimension: number;
count: number;
metric: "cosine" | "euclidean" | "dotproduct";
}
deleteIndex()Direct link to deleteIndex()
删除索引及其所有数据。
🌐 Deletes an index and all its data.
indexName:
listIndexes()Direct link to listIndexes()
列出所有向量索引。
🌐 Lists all vector indexes.
返回:Promise<string[]>
🌐 Returns: Promise<string[]>
updateVector()Direct link to updateVector()
通过 ID 或元数据过滤器更新单个向量。必须提供 id 或 filter 中的一个,但不能同时提供两者。
🌐 Update a single vector by ID or by metadata filter. Either id or filter must be provided, but not both.
indexName:
id?:
filter?:
update:
update.vector?:
update.metadata?:
deleteVector()Direct link to deleteVector()
通过其 ID 删除单个向量。
🌐 Deletes a single vector by its ID.
indexName:
id:
deleteVectors()Direct link to deleteVectors()
通过 ID 或元数据过滤器删除多个向量。必须提供 ids 或 filter 中的一个,但不能同时提供两者。
🌐 Delete multiple vectors by IDs or by metadata filter. Either ids or filter must be provided, but not both.
indexName:
ids?:
filter?:
响应类型Direct link to 响应类型
🌐 Response Types
查询结果以此格式返回:
🌐 Query results are returned in this format:
interface QueryResult {
id: string;
score: number;
metadata: Record<string, any>;
vector?: number[]; // Only included if includeVector is true
}
相关Direct link to 相关
🌐 Related