docs/src/content/en/reference/vectors/elasticsearch.mdx
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.
npm install @mastra/elasticsearch@latest
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" },
});
<PropertiesTable content={[ { name: 'id', type: 'string', description: 'Unique identifier for this vector store instance', }, { name: 'url', type: 'string', description: "Elasticsearch connection URL (e.g., 'http://localhost:9200')", }, ]} />
createIndex()Creates a new index with the specified configuration.
<PropertiesTable content={[ { name: 'indexName', type: 'string', description: 'Name of the index to create', }, { name: 'dimension', type: 'number', description: 'Vector dimension (must match your embedding model)', }, { name: 'metric', type: "'cosine' | 'euclidean' | 'dotproduct'", isOptional: true, defaultValue: 'cosine', description: 'Distance metric for similarity search', }, ]} />
upsert()Adds or updates vectors and their metadata in the index.
<PropertiesTable content={[ { name: 'indexName', type: 'string', description: 'Name of the index to insert into', }, { name: 'vectors', type: 'number[][]', description: 'Array of embedding vectors', }, { name: 'metadata', type: 'Record<string, any>[]', isOptional: true, description: 'Metadata for each vector', }, { name: 'ids', type: 'string[]', isOptional: true, description: 'Optional vector IDs (auto-generated if not provided)', }, ]} />
query()Searches for similar vectors with optional metadata filtering.
<PropertiesTable content={[ { name: 'indexName', type: 'string', description: 'Name of the index to search in', }, { name: 'queryVector', type: 'number[]', description: 'Query vector to find similar vectors for', }, { name: 'topK', type: 'number', isOptional: true, defaultValue: '10', description: 'Number of results to return', }, { name: 'filter', type: 'Record<string, any>', isOptional: true, description: 'Metadata filters', }, { name: 'includeVector', type: 'boolean', isOptional: true, defaultValue: 'false', description: 'Whether to include vector data in results', }, ]} />
describeIndex()Gets information about an index.
<PropertiesTable content={[ { name: 'indexName', type: 'string', description: 'Name of the index to describe', }, ]} />
Returns:
interface IndexStats {
dimension: number
count: number
metric: 'cosine' | 'euclidean' | 'dotproduct'
}
deleteIndex()Deletes an index and all its data.
<PropertiesTable content={[ { name: 'indexName', type: 'string', description: 'Name of the index to delete', }, ]} />
listIndexes()Lists all vector indexes.
Returns: Promise<string[]>
updateVector()Update a single vector by ID or by metadata filter. Either id or filter must be provided, but not both.
<PropertiesTable content={[ { name: 'indexName', type: 'string', description: 'Name of the index containing the vector', }, { name: 'id', type: 'string', isOptional: true, description: 'ID of the vector to update (mutually exclusive with filter)', }, { name: 'filter', type: 'Record<string, any>', isOptional: true, description: 'Metadata filter to identify vector(s) to update (mutually exclusive with id)', }, { name: 'update', type: 'object', description: 'Update data containing vector and/or metadata', }, { name: 'update.vector', type: 'number[]', isOptional: true, description: 'New vector data', }, { name: 'update.metadata', type: 'Record<string, any>', isOptional: true, description: 'New metadata', }, ]} />
deleteVector()Deletes a single vector by its ID.
<PropertiesTable content={[ { name: 'indexName', type: 'string', description: 'Name of the index containing the vector', }, { name: 'id', type: 'string', description: 'ID of the vector to delete', }, ]} />
deleteVectors()Delete multiple vectors by IDs or by metadata filter. Either ids or filter must be provided, but not both.
<PropertiesTable content={[ { name: 'indexName', type: 'string', description: 'Name of the index containing the vectors to delete', }, { name: 'ids', type: 'string[]', isOptional: true, description: 'Array of vector IDs to delete (mutually exclusive with filter)', }, { name: 'filter', type: 'Record<string, any>', isOptional: true, description: 'Metadata filter to identify vectors to delete (mutually exclusive with ids)', }, ]} />
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
}