stores/elasticsearch/README.md
ElasticSearch vector store implementation for Mastra, providing vector similarity search and index management using ElasticSearch 8.x+.
npm install @mastra/elasticsearch
import { ElasticSearchVector } from '@mastra/elasticsearch';
const vectorDB = new ElasticSearchVector({
url: 'http://localhost:9200',
id: 'my-vector-store',
auth: { apiKey: 'insert-api-key' }
});
// Create a new vector index
await vectorDB.createIndex({
indexName: 'my_vectors',
dimension: 1536,
metric: 'cosine', // or 'euclidean', 'dotproduct'
});
// Upsert vectors
const ids = await vectorDB.upsert({
indexName: 'my_vectors',
vectors: [[0.1, 0.2, ...], [0.3, 0.4, ...]],
metadata: [{ text: 'doc1' }, { text: 'doc2' }],
});
// Query vectors
const results = await vectorDB.query({
indexName: 'my_vectors',
queryVector: [0.1, 0.2, ...],
topK: 10,
filter: { text: 'doc1' },
includeVector: false,
});
// Update vectors
await vectorDB.updateVector({
indexName: 'my_vectors',
id: 'vector-id',
update: {
vector: [0.5, 0.6, ...],
metadata: { text: 'updated' },
},
});
// Delete vectors
await vectorDB.deleteVector({
indexName: 'my_vectors',
id: 'vector-id',
});
// Bulk delete by filter
await vectorDB.deleteVectors({
indexName: 'my_vectors',
filter: { source: 'old-document.pdf' },
});
The ElasticSearchVector store accepts either connection parameters or a pre-configured client:
id: A unique identifier for the vector store instance (required)url: The ElasticSearch node URL (required)auth: The authentication mechanism (optional)
{ auth: { username: 'insert-username', password: 'insert-password' } }{ auth: { apiKey: 'insert-api-key' } }{ auth: { bearer: 'insert-token' } }id: A unique identifier for the vector store instance (required)client: An existing @elastic/elasticsearch Client instance (required)import { Client } from '@elastic/elasticsearch';
import { ElasticSearchVector } from '@mastra/elasticsearch';
const client = new Client({ node: 'http://localhost:9200' });
const vectorDB = new ElasticSearchVector({ id: 'my-vector-store', client });
cosine: Cosine similarity (default)euclidean: L2 (Euclidean) distancedotproduct: Dot product similarityThe following filter operators are supported:
$eq, $ne, $gt, $gte, $lt, $lte$in, $nin, $all$and, $or, $not, $nor$exists$regex# Start ElasticSearch container
docker compose up -d
# Run tests
pnpm test
# Stop container
docker compose down -v
MIT