@mastra/elasticsearch 1.0.0 → 1.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
1
  # @mastra/elasticsearch
2
2
 
3
+ ## 1.1.0-alpha.0
4
+
5
+ ### Minor Changes
6
+
7
+ - **Added** API key, basic, and bearer authentication options for Elasticsearch connections. ([#11298](https://github.com/mastra-ai/mastra/pull/11298))
8
+
9
+ **Changed** vector IDs now come from Elasticsearch `_id`; stored `id` fields are no longer written (breaking if you relied on `source.id`).
10
+
11
+ **Why** This aligns with Elasticsearch auth best practices and avoids duplicate IDs in stored documents.
12
+
13
+ **Before**
14
+
15
+ ```ts
16
+ const store = new ElasticSearchVector({ url, id: 'my-index' });
17
+ ```
18
+
19
+ **After**
20
+
21
+ ```ts
22
+ const store = new ElasticSearchVector({
23
+ url,
24
+ id: 'my-index',
25
+ auth: { apiKey: process.env.ELASTICSEARCH_API_KEY! },
26
+ });
27
+ ```
28
+
29
+ ### Patch Changes
30
+
31
+ - Updated dependencies [[`e6fc281`](https://github.com/mastra-ai/mastra/commit/e6fc281896a3584e9e06465b356a44fe7faade65), [`97be6c8`](https://github.com/mastra-ai/mastra/commit/97be6c8963130fca8a664fcf99d7b3a38e463595), [`5fe1fe0`](https://github.com/mastra-ai/mastra/commit/5fe1fe0109faf2c87db34b725d8a4571a594f80e), [`f6673b8`](https://github.com/mastra-ai/mastra/commit/f6673b893b65b7d273ad25ead42e990704cc1e17), [`cd6be8a`](https://github.com/mastra-ai/mastra/commit/cd6be8ad32741cd41cabf508355bb31b71e8a5bd), [`9eb4e8e`](https://github.com/mastra-ai/mastra/commit/9eb4e8e39efbdcfff7a40ff2ce07ce2714c65fa8), [`aa37c84`](https://github.com/mastra-ai/mastra/commit/aa37c84d29b7db68c72517337932ef486c316275), [`47eba72`](https://github.com/mastra-ai/mastra/commit/47eba72f0397d0d14fbe324b97940c3d55e5a525)]:
32
+ - @mastra/core@1.2.0-alpha.0
33
+
3
34
  ## 1.0.0
4
35
 
5
36
  ### Minor Changes
package/README.md CHANGED
@@ -23,6 +23,7 @@ import { ElasticSearchVector } from '@mastra/elasticsearch';
23
23
  const vectorDB = new ElasticSearchVector({
24
24
  url: 'http://localhost:9200',
25
25
  id: 'my-vector-store',
26
+ auth: { apiKey: 'insert-api-key' }
26
27
  });
27
28
 
28
29
  // Create a new vector index
@@ -77,6 +78,9 @@ The ElasticSearchVector store can be initialized with:
77
78
 
78
79
  - `url`: The ElasticSearch node URL (required)
79
80
  - `id`: A unique identifier for the vector store instance (required)
81
+ - `auth` : The authentication mechanism (HTTP basic or API key)
82
+ - HTTP basic: { auth: { username : 'insert-username', password : 'insert-password' }}
83
+ - API key: { auth: { apiKey: 'insert-api-key' }}
80
84
 
81
85
  ## Features
82
86
 
@@ -102,7 +106,7 @@ The following filter operators are supported:
102
106
 
103
107
  - **Comparison**: `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`
104
108
  - **Array**: `$in`, `$nin`, `$all`
105
- - **Logical**: `$and`, `$or`, `$not`
109
+ - **Logical**: `$and`, `$or`, `$not`, `$nor`
106
110
  - **Element**: `$exists`
107
111
  - **Regex**: `$regex`
108
112
 
@@ -29,4 +29,4 @@ docs/
29
29
  ## Version
30
30
 
31
31
  Package: @mastra/elasticsearch
32
- Version: 1.0.0
32
+ Version: 1.1.0-alpha.0
@@ -5,7 +5,7 @@ description: Documentation for @mastra/elasticsearch. Includes links to type def
5
5
 
6
6
  # @mastra/elasticsearch Documentation
7
7
 
8
- > **Version**: 1.0.0
8
+ > **Version**: 1.1.0-alpha.0
9
9
  > **Package**: @mastra/elasticsearch
10
10
 
11
11
  ## Quick Navigation
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.0",
2
+ "version": "1.1.0-alpha.0",
3
3
  "package": "@mastra/elasticsearch",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -262,7 +262,13 @@ await store.upsert({
262
262
  ```ts title="vector-store.ts"
263
263
  import { ElasticSearchVector } from "@mastra/elasticsearch";
264
264
 
265
- const store = new ElasticSearchVector({ id: 'elasticsearch-vector', url: process.env.ELASTICSEARCH_URL });
265
+ const store = new ElasticSearchVector({
266
+ id: 'elasticsearch-vector',
267
+ url: process.env.ELASTICSEARCH_URL,
268
+ auth: {
269
+ apiKey : process.env.ELASTICSEARCH_API_KEY
270
+ }
271
+ });
266
272
 
267
273
  await store.createIndex({
268
274
  indexName: "my-collection",
@@ -275,6 +281,9 @@ await store.upsert({
275
281
  metadata: chunks.map((chunk) => ({ text: chunk.text })),
276
282
  });
277
283
  ```
284
+ <h3>Using Elasticsearch</h3>
285
+
286
+ For detailed setup instructions and best practices, see the [official Elasticsearch documentation](https://www.elastic.co/docs/solutions/search/get-started).
278
287
 
279
288
 
280
289
  **couchbase:**
@@ -376,7 +385,7 @@ The dimension size must match the output dimension of your chosen embedding mode
376
385
 
377
386
  - OpenAI text-embedding-3-small: 1536 dimensions (or custom, e.g., 256)
378
387
  - Cohere embed-multilingual-v3: 1024 dimensions
379
- - Google text-embedding-004: 768 dimensions (or custom)
388
+ - Google gemini-embedding-001: 768 dimensions (or custom)
380
389
 
381
390
  > **Note:**
382
391
  Index dimensions cannot be changed after creation. To use a different model, delete and recreate the index with the new dimension size.
@@ -14,8 +14,8 @@ It's part of the `@mastra/elasticsearch` package.
14
14
 
15
15
  ## Installation
16
16
 
17
- ```bash
18
- npm install @mastra/elasticsearch@beta
17
+ ```bash npm2yarn
18
+ npm install @mastra/elasticsearch@latest
19
19
  ```
20
20
 
21
21
  ## Usage
package/dist/index.cjs CHANGED
@@ -11,7 +11,7 @@ var ElasticSearchFilterTranslator = class extends filter.BaseFilterTranslator {
11
11
  getSupportedOperators() {
12
12
  return {
13
13
  ...filter.BaseFilterTranslator.DEFAULT_OPERATORS,
14
- logical: ["$and", "$or", "$not"],
14
+ logical: ["$and", "$or", "$not", "$nor"],
15
15
  array: ["$in", "$nin", "$all"],
16
16
  regex: ["$regex"],
17
17
  custom: []
@@ -130,6 +130,7 @@ var ElasticSearchFilterTranslator = class extends filter.BaseFilterTranslator {
130
130
  }
131
131
  };
132
132
  case "$not":
133
+ case "$nor":
133
134
  return {
134
135
  bool: {
135
136
  must_not: conditions
@@ -252,9 +253,9 @@ var ElasticSearchFilterTranslator = class extends filter.BaseFilterTranslator {
252
253
  if (!hasEndAnchor) {
253
254
  wildcardPattern = wildcardPattern + "*";
254
255
  }
255
- return { wildcard: { [field]: wildcardPattern } };
256
+ return { wildcard: { [field]: { value: wildcardPattern } } };
256
257
  }
257
- return { regexp: { [field]: regexValue } };
258
+ return { regexp: { [field]: { value: regexValue } } };
258
259
  }
259
260
  addKeywordIfNeeded(field, value) {
260
261
  if (typeof value === "string") {
@@ -386,10 +387,11 @@ var ElasticSearchVector = class extends vector.MastraVector {
386
387
  * Creates a new ElasticSearchVector client.
387
388
  *
388
389
  * @param {string} url - The url of the ElasticSearch node.
390
+ * @param {ElasticSearchAuth} [auth] - The authentication credentials for ElasticSearch.
389
391
  */
390
- constructor({ url, id }) {
392
+ constructor({ url, id, auth }) {
391
393
  super({ id });
392
- this.client = new elasticsearch.Client({ node: url });
394
+ this.client = new elasticsearch.Client({ node: url, ...auth && { auth } });
393
395
  }
394
396
  /**
395
397
  * Creates a new collection with the specified configuration.
@@ -415,7 +417,6 @@ var ElasticSearchVector = class extends vector.MastraVector {
415
417
  mappings: {
416
418
  properties: {
417
419
  metadata: { type: "object" },
418
- id: { type: "keyword" },
419
420
  embedding: {
420
421
  type: "dense_vector",
421
422
  dims: dimension,
@@ -536,12 +537,8 @@ var ElasticSearchVector = class extends vector.MastraVector {
536
537
  */
537
538
  async deleteIndex({ indexName }) {
538
539
  try {
539
- await this.client.indices.delete({ index: indexName });
540
+ await this.client.indices.delete({ index: indexName }, { ignore: [404] });
540
541
  } catch (error$1) {
541
- const isIndexNotFound = error$1?.statusCode === 404 || error$1?.body?.error?.type === "index_not_found_exception" || error$1?.meta?.statusCode === 404;
542
- if (isIndexNotFound) {
543
- return;
544
- }
545
542
  const mastraError = new error.MastraError(
546
543
  {
547
544
  id: storage.createVectorErrorId("ELASTICSEARCH", "DELETE_INDEX", "FAILED"),
@@ -580,7 +577,6 @@ var ElasticSearchVector = class extends vector.MastraVector {
580
577
  }
581
578
  };
582
579
  const document = {
583
- id: vectorIds[i],
584
580
  embedding: vectors[i],
585
581
  metadata: metadata[i] || {}
586
582
  };
@@ -674,6 +670,7 @@ var ElasticSearchVector = class extends vector.MastraVector {
674
670
  vector.validateTopK("ELASTICSEARCH", topK);
675
671
  try {
676
672
  const translatedFilter = this.transformFilter(filter);
673
+ const sourceFields = includeVector ? ["metadata", "embedding"] : ["metadata"];
677
674
  const response = await this.client.search({
678
675
  index: indexName,
679
676
  knn: {
@@ -683,12 +680,12 @@ var ElasticSearchVector = class extends vector.MastraVector {
683
680
  num_candidates: topK * 2,
684
681
  ...translatedFilter ? { filter: translatedFilter } : {}
685
682
  },
686
- _source: ["id", "metadata", "embedding"]
683
+ _source: sourceFields
687
684
  });
688
685
  const results = response.hits.hits.map((hit) => {
689
686
  const source = hit._source || {};
690
687
  return {
691
- id: String(source.id || ""),
688
+ id: String(hit._id),
692
689
  score: typeof hit._score === "number" ? hit._score : 0,
693
690
  metadata: source.metadata || {},
694
691
  ...includeVector && { vector: source.embedding }
@@ -790,7 +787,8 @@ var ElasticSearchVector = class extends vector.MastraVector {
790
787
  try {
791
788
  const result = await this.client.get({
792
789
  index: indexName,
793
- id
790
+ id,
791
+ _source: ["embedding", "metadata"]
794
792
  }).catch(() => {
795
793
  throw new Error(`Document with ID ${id} not found in index ${indexName}`);
796
794
  });
@@ -813,9 +811,7 @@ var ElasticSearchVector = class extends vector.MastraVector {
813
811
  );
814
812
  }
815
813
  const source = existingDoc._source;
816
- const updatedDoc = {
817
- id: source?.id || id
818
- };
814
+ const updatedDoc = {};
819
815
  try {
820
816
  if (update.vector) {
821
817
  const indexInfo = await this.describeIndex({ indexName });