Elasticsearch vector store
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.
InstallationInstallation的直接链接
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/elasticsearch@latest
pnpm add @mastra/elasticsearch@latest
yarn add @mastra/elasticsearch@latest
bun add @mastra/elasticsearch@latest
UsageUsage的直接链接
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" },
});
Constructor optionsConstructor options的直接链接
id:
url:
MethodsMethods的直接链接
createIndex()createindex的直接链接
Creates a new index with the specified configuration.
indexName:
dimension:
metric?:
upsert()upsert的直接链接
Adds or updates vectors and their metadata in the index.
indexName:
vectors:
metadata?:
ids?:
query()query的直接链接
Searches for similar vectors with optional metadata filtering.
indexName:
queryVector:
topK?:
filter?:
includeVector?:
describeIndex()describeindex的直接链接
Gets information about an index.
indexName:
Returns:
interface IndexStats {
dimension: number
count: number
metric: 'cosine' | 'euclidean' | 'dotproduct'
}
deleteIndex()deleteindex的直接链接
Deletes an index and all its data.
indexName:
listIndexes()listindexes的直接链接
Lists all vector indexes.
Returns: Promise<string[]>
updateVector()updatevector的直接链接
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()deletevector的直接链接
Deletes a single vector by its ID.
indexName:
id:
deleteVectors()deletevectors的直接链接
Delete multiple vectors by IDs or by metadata filter. Either ids or filter must be provided, but not both.
indexName:
ids?:
filter?:
Response typesResponse 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
}