@mastra/opensearch 1.1.0 → 1.1.1-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/dist/index.cjs CHANGED
@@ -1,913 +1,710 @@
1
- 'use strict';
2
-
3
- var error = require('@mastra/core/error');
4
- var storage = require('@mastra/core/storage');
5
- var vector = require('@mastra/core/vector');
6
- var opensearch = require('@opensearch-project/opensearch');
7
- var filter = require('@mastra/core/vector/filter');
8
-
9
- // src/vector/index.ts
10
- var OpenSearchFilterTranslator = class extends filter.BaseFilterTranslator {
11
- getSupportedOperators() {
12
- return {
13
- ...filter.BaseFilterTranslator.DEFAULT_OPERATORS,
14
- logical: ["$and", "$or", "$not"],
15
- array: ["$in", "$nin", "$all"],
16
- regex: ["$regex"],
17
- custom: []
18
- };
19
- }
20
- translate(filter) {
21
- if (this.isEmpty(filter)) return void 0;
22
- this.validateFilter(filter);
23
- return this.translateNode(filter);
24
- }
25
- translateNode(node) {
26
- if (this.isPrimitive(node) || Array.isArray(node)) {
27
- return node;
28
- }
29
- const entries = Object.entries(node);
30
- const logicalOperators = [];
31
- const fieldConditions = [];
32
- entries.forEach(([key, value]) => {
33
- if (this.isLogicalOperator(key)) {
34
- logicalOperators.push([key, value]);
35
- } else {
36
- fieldConditions.push([key, value]);
37
- }
38
- });
39
- if (logicalOperators.length === 1 && fieldConditions.length === 0) {
40
- const [operator, value] = logicalOperators[0];
41
- if (!Array.isArray(value) && typeof value !== "object") {
42
- throw new Error(`Invalid logical operator structure: ${operator} must have an array or object value`);
43
- }
44
- return this.translateLogicalOperator(operator, value);
45
- }
46
- const fieldConditionQueries = fieldConditions.map(([key, value]) => {
47
- if (typeof value === "object" && value !== null && !Array.isArray(value)) {
48
- const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
49
- const nestedField = `metadata.${key}`;
50
- return hasOperators ? this.translateFieldConditions(nestedField, value) : this.translateNestedObject(nestedField, value);
51
- }
52
- if (Array.isArray(value)) {
53
- const fieldWithKeyword2 = this.addKeywordIfNeeded(`metadata.${key}`, value);
54
- return { terms: { [fieldWithKeyword2]: value } };
55
- }
56
- const fieldWithKeyword = this.addKeywordIfNeeded(`metadata.${key}`, value);
57
- return { term: { [fieldWithKeyword]: value } };
58
- });
59
- if (logicalOperators.length > 0) {
60
- const logicalConditions = logicalOperators.map(
61
- ([operator, value]) => this.translateOperator(operator, value)
62
- );
63
- return {
64
- bool: {
65
- must: [...logicalConditions, ...fieldConditionQueries]
66
- }
67
- };
68
- }
69
- if (fieldConditionQueries.length > 1) {
70
- return {
71
- bool: {
72
- must: fieldConditionQueries
73
- }
74
- };
75
- }
76
- if (fieldConditionQueries.length === 1) {
77
- return fieldConditionQueries[0];
78
- }
79
- return { match_all: {} };
80
- }
81
- /**
82
- * Handles translation of nested objects with dot notation fields
83
- */
84
- translateNestedObject(field, value) {
85
- const conditions = Object.entries(value).map(([subField, subValue]) => {
86
- const fullField = `${field}.${subField}`;
87
- if (this.isOperator(subField)) {
88
- return this.translateOperator(subField, subValue, field);
89
- }
90
- if (typeof subValue === "object" && subValue !== null && !Array.isArray(subValue)) {
91
- const hasOperators = Object.keys(subValue).some((k) => this.isOperator(k));
92
- if (hasOperators) {
93
- return this.translateFieldConditions(fullField, subValue);
94
- }
95
- return this.translateNestedObject(fullField, subValue);
96
- }
97
- const fieldWithKeyword = this.addKeywordIfNeeded(fullField, subValue);
98
- return { term: { [fieldWithKeyword]: subValue } };
99
- });
100
- return {
101
- bool: {
102
- must: conditions
103
- }
104
- };
105
- }
106
- translateLogicalOperator(operator, value) {
107
- const conditions = Array.isArray(value) ? value.map((item) => this.translateNode(item)) : [this.translateNode(value)];
108
- switch (operator) {
109
- case "$and":
110
- if (Array.isArray(value) && value.length === 0) {
111
- return { match_all: {} };
112
- }
113
- return {
114
- bool: {
115
- must: conditions
116
- }
117
- };
118
- case "$or":
119
- if (Array.isArray(value) && value.length === 0) {
120
- return {
121
- bool: {
122
- must_not: [{ match_all: {} }]
123
- }
124
- };
125
- }
126
- return {
127
- bool: {
128
- should: conditions
129
- }
130
- };
131
- case "$not":
132
- return {
133
- bool: {
134
- must_not: conditions
135
- }
136
- };
137
- default:
138
- return value;
139
- }
140
- }
141
- translateFieldOperator(field, operator, value) {
142
- if (this.isBasicOperator(operator)) {
143
- const normalizedValue = this.normalizeComparisonValue(value);
144
- const fieldWithKeyword2 = this.addKeywordIfNeeded(field, value);
145
- switch (operator) {
146
- case "$eq":
147
- if (value === null) {
148
- return {
149
- bool: {
150
- must_not: [{ exists: { field } }]
151
- }
152
- };
153
- }
154
- return { term: { [fieldWithKeyword2]: normalizedValue } };
155
- case "$ne":
156
- if (value === null) {
157
- return { exists: { field } };
158
- }
159
- return {
160
- bool: {
161
- must_not: [{ term: { [fieldWithKeyword2]: normalizedValue } }]
162
- }
163
- };
164
- default:
165
- return { term: { [fieldWithKeyword2]: normalizedValue } };
166
- }
167
- }
168
- if (this.isNumericOperator(operator)) {
169
- const normalizedValue = this.normalizeComparisonValue(value);
170
- const rangeOp = operator.replace("$", "");
171
- return { range: { [field]: { [rangeOp]: normalizedValue } } };
172
- }
173
- if (this.isArrayOperator(operator)) {
174
- if (!Array.isArray(value)) {
175
- throw new Error(`Invalid array operator value: ${operator} requires an array value`);
176
- }
177
- const normalizedValues = this.normalizeArrayValues(value);
178
- const fieldWithKeyword2 = this.addKeywordIfNeeded(field, value);
179
- switch (operator) {
180
- case "$in":
181
- return { terms: { [fieldWithKeyword2]: normalizedValues } };
182
- case "$nin":
183
- if (normalizedValues.length === 0) {
184
- return { match_all: {} };
185
- }
186
- return {
187
- bool: {
188
- must_not: [{ terms: { [fieldWithKeyword2]: normalizedValues } }]
189
- }
190
- };
191
- case "$all":
192
- if (normalizedValues.length === 0) {
193
- return {
194
- bool: {
195
- must_not: [{ match_all: {} }]
196
- }
197
- };
198
- }
199
- return {
200
- bool: {
201
- must: normalizedValues.map((v) => ({ term: { [fieldWithKeyword2]: v } }))
202
- }
203
- };
204
- default:
205
- return { terms: { [fieldWithKeyword2]: normalizedValues } };
206
- }
207
- }
208
- if (this.isElementOperator(operator)) {
209
- switch (operator) {
210
- case "$exists":
211
- return value ? { exists: { field } } : { bool: { must_not: [{ exists: { field } }] } };
212
- default:
213
- return { exists: { field } };
214
- }
215
- }
216
- if (this.isRegexOperator(operator)) {
217
- return this.translateRegexOperator(field, value);
218
- }
219
- const fieldWithKeyword = this.addKeywordIfNeeded(field, value);
220
- return { term: { [fieldWithKeyword]: value } };
221
- }
222
- /**
223
- * Translates regex patterns to OpenSearch query syntax
224
- */
225
- translateRegexOperator(field, value) {
226
- const regexValue = typeof value === "string" ? value : value.toString();
227
- if (regexValue.includes("\n") || regexValue.includes("\r")) {
228
- return { match: { [field]: value } };
229
- }
230
- let processedRegex = regexValue;
231
- const hasStartAnchor = regexValue.startsWith("^");
232
- const hasEndAnchor = regexValue.endsWith("$");
233
- if (hasStartAnchor || hasEndAnchor) {
234
- if (hasStartAnchor) {
235
- processedRegex = processedRegex.substring(1);
236
- }
237
- if (hasEndAnchor) {
238
- processedRegex = processedRegex.substring(0, processedRegex.length - 1);
239
- }
240
- let wildcardPattern = processedRegex;
241
- if (!hasStartAnchor) {
242
- wildcardPattern = "*" + wildcardPattern;
243
- }
244
- if (!hasEndAnchor) {
245
- wildcardPattern = wildcardPattern + "*";
246
- }
247
- return { wildcard: { [field]: wildcardPattern } };
248
- }
249
- const escapedRegex = regexValue.replace(/\\/g, "\\\\");
250
- return { regexp: { [field]: escapedRegex } };
251
- }
252
- addKeywordIfNeeded(field, value) {
253
- if (typeof value === "string") {
254
- return `${field}.keyword`;
255
- }
256
- if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
257
- return `${field}.keyword`;
258
- }
259
- return field;
260
- }
261
- /**
262
- * Helper method to handle special cases for the $not operator
263
- */
264
- handleNotOperatorSpecialCases(value, field) {
265
- if (value === null) {
266
- return { exists: { field } };
267
- }
268
- if (typeof value === "object" && value !== null) {
269
- if ("$eq" in value && value.$eq === null) {
270
- return { exists: { field } };
271
- }
272
- if ("$ne" in value && value.$ne === null) {
273
- return {
274
- bool: {
275
- must_not: [{ exists: { field } }]
276
- }
277
- };
278
- }
279
- }
280
- return null;
281
- }
282
- translateOperator(operator, value, field) {
283
- if (!this.isOperator(operator)) {
284
- throw new Error(`Unsupported operator: ${operator}`);
285
- }
286
- if (operator === "$not" && field) {
287
- const specialCaseResult = this.handleNotOperatorSpecialCases(value, field);
288
- if (specialCaseResult) {
289
- return specialCaseResult;
290
- }
291
- }
292
- if (this.isLogicalOperator(operator)) {
293
- if (operator === "$not" && field && typeof value === "object" && value !== null && !Array.isArray(value)) {
294
- const entries = Object.entries(value);
295
- if (entries.length > 0) {
296
- if (entries.every(([op]) => this.isOperator(op))) {
297
- const translatedCondition = this.translateFieldConditions(field, value);
298
- return {
299
- bool: {
300
- must_not: [translatedCondition]
301
- }
302
- };
303
- }
304
- if (entries.length === 1 && entries[0] && this.isOperator(entries[0][0])) {
305
- const [nestedOp, nestedVal] = entries[0];
306
- const translatedNested = this.translateFieldOperator(field, nestedOp, nestedVal);
307
- return {
308
- bool: {
309
- must_not: [translatedNested]
310
- }
311
- };
312
- }
313
- }
314
- }
315
- return this.translateLogicalOperator(operator, value);
316
- }
317
- if (field) {
318
- return this.translateFieldOperator(field, operator, value);
319
- }
320
- return value;
321
- }
322
- /**
323
- * Translates field conditions to OpenSearch query syntax
324
- * Handles special cases like range queries and multiple operators
325
- */
326
- translateFieldConditions(field, conditions) {
327
- if (this.canOptimizeToRangeQuery(conditions)) {
328
- return this.createRangeQuery(field, conditions);
329
- }
330
- const queryConditions = [];
331
- Object.entries(conditions).forEach(([operator, value]) => {
332
- if (this.isOperator(operator)) {
333
- queryConditions.push(this.translateOperator(operator, value, field));
334
- } else {
335
- const fieldWithKeyword = this.addKeywordIfNeeded(`${field}.${operator}`, value);
336
- queryConditions.push({ term: { [fieldWithKeyword]: value } });
337
- }
338
- });
339
- if (queryConditions.length === 1) {
340
- return queryConditions[0];
341
- }
342
- return {
343
- bool: {
344
- must: queryConditions
345
- }
346
- };
347
- }
348
- /**
349
- * Checks if conditions can be optimized to a range query
350
- */
351
- canOptimizeToRangeQuery(conditions) {
352
- return Object.keys(conditions).every((op) => this.isNumericOperator(op)) && Object.keys(conditions).length > 0;
353
- }
354
- /**
355
- * Creates a range query from numeric operators
356
- */
357
- createRangeQuery(field, conditions) {
358
- const rangeParams = Object.fromEntries(
359
- Object.entries(conditions).map(([op, val]) => [op.replace("$", ""), this.normalizeComparisonValue(val)])
360
- );
361
- return { range: { [field]: rangeParams } };
362
- }
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _mastra_core_error = require("@mastra/core/error");
3
+ let _mastra_core_storage = require("@mastra/core/storage");
4
+ let _mastra_core_vector = require("@mastra/core/vector");
5
+ let _opensearch_project_opensearch = require("@opensearch-project/opensearch");
6
+ let _mastra_core_vector_filter = require("@mastra/core/vector/filter");
7
+ //#region src/vector/filter.ts
8
+ /**
9
+ * Translator for OpenSearch filter queries.
10
+ * Maintains OpenSearch-compatible syntax while ensuring proper validation
11
+ * and normalization of values.
12
+ */
13
+ var OpenSearchFilterTranslator = class extends _mastra_core_vector_filter.BaseFilterTranslator {
14
+ getSupportedOperators() {
15
+ return {
16
+ ..._mastra_core_vector_filter.BaseFilterTranslator.DEFAULT_OPERATORS,
17
+ logical: [
18
+ "$and",
19
+ "$or",
20
+ "$not"
21
+ ],
22
+ array: [
23
+ "$in",
24
+ "$nin",
25
+ "$all"
26
+ ],
27
+ regex: ["$regex"],
28
+ custom: []
29
+ };
30
+ }
31
+ translate(filter) {
32
+ if (this.isEmpty(filter)) return void 0;
33
+ this.validateFilter(filter);
34
+ return this.translateNode(filter);
35
+ }
36
+ translateNode(node) {
37
+ if (this.isPrimitive(node) || Array.isArray(node)) return node;
38
+ const entries = Object.entries(node);
39
+ const logicalOperators = [];
40
+ const fieldConditions = [];
41
+ entries.forEach(([key, value]) => {
42
+ if (this.isLogicalOperator(key)) logicalOperators.push([key, value]);
43
+ else fieldConditions.push([key, value]);
44
+ });
45
+ if (logicalOperators.length === 1 && fieldConditions.length === 0) {
46
+ const [operator, value] = logicalOperators[0];
47
+ if (!Array.isArray(value) && typeof value !== "object") throw new Error(`Invalid logical operator structure: ${operator} must have an array or object value`);
48
+ return this.translateLogicalOperator(operator, value);
49
+ }
50
+ const fieldConditionQueries = fieldConditions.map(([key, value]) => {
51
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
52
+ const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
53
+ const nestedField = `metadata.${key}`;
54
+ return hasOperators ? this.translateFieldConditions(nestedField, value) : this.translateNestedObject(nestedField, value);
55
+ }
56
+ if (Array.isArray(value)) return { terms: { [this.addKeywordIfNeeded(`metadata.${key}`, value)]: value } };
57
+ return { term: { [this.addKeywordIfNeeded(`metadata.${key}`, value)]: value } };
58
+ });
59
+ if (logicalOperators.length > 0) return { bool: { must: [...logicalOperators.map(([operator, value]) => this.translateOperator(operator, value)), ...fieldConditionQueries] } };
60
+ if (fieldConditionQueries.length > 1) return { bool: { must: fieldConditionQueries } };
61
+ if (fieldConditionQueries.length === 1) return fieldConditionQueries[0];
62
+ return { match_all: {} };
63
+ }
64
+ /**
65
+ * Handles translation of nested objects with dot notation fields
66
+ */
67
+ translateNestedObject(field, value) {
68
+ return { bool: { must: Object.entries(value).map(([subField, subValue]) => {
69
+ const fullField = `${field}.${subField}`;
70
+ if (this.isOperator(subField)) return this.translateOperator(subField, subValue, field);
71
+ if (typeof subValue === "object" && subValue !== null && !Array.isArray(subValue)) {
72
+ if (Object.keys(subValue).some((k) => this.isOperator(k))) return this.translateFieldConditions(fullField, subValue);
73
+ return this.translateNestedObject(fullField, subValue);
74
+ }
75
+ return { term: { [this.addKeywordIfNeeded(fullField, subValue)]: subValue } };
76
+ }) } };
77
+ }
78
+ translateLogicalOperator(operator, value) {
79
+ const conditions = Array.isArray(value) ? value.map((item) => this.translateNode(item)) : [this.translateNode(value)];
80
+ switch (operator) {
81
+ case "$and":
82
+ if (Array.isArray(value) && value.length === 0) return { match_all: {} };
83
+ return { bool: { must: conditions } };
84
+ case "$or":
85
+ if (Array.isArray(value) && value.length === 0) return { bool: { must_not: [{ match_all: {} }] } };
86
+ return { bool: { should: conditions } };
87
+ case "$not": return { bool: { must_not: conditions } };
88
+ default: return value;
89
+ }
90
+ }
91
+ translateFieldOperator(field, operator, value) {
92
+ if (this.isBasicOperator(operator)) {
93
+ const normalizedValue = this.normalizeComparisonValue(value);
94
+ const fieldWithKeyword = this.addKeywordIfNeeded(field, value);
95
+ switch (operator) {
96
+ case "$eq":
97
+ if (value === null) return { bool: { must_not: [{ exists: { field } }] } };
98
+ return { term: { [fieldWithKeyword]: normalizedValue } };
99
+ case "$ne":
100
+ if (value === null) return { exists: { field } };
101
+ return { bool: { must_not: [{ term: { [fieldWithKeyword]: normalizedValue } }] } };
102
+ default: return { term: { [fieldWithKeyword]: normalizedValue } };
103
+ }
104
+ }
105
+ if (this.isNumericOperator(operator)) {
106
+ const normalizedValue = this.normalizeComparisonValue(value);
107
+ const rangeOp = operator.replace("$", "");
108
+ return { range: { [field]: { [rangeOp]: normalizedValue } } };
109
+ }
110
+ if (this.isArrayOperator(operator)) {
111
+ if (!Array.isArray(value)) throw new Error(`Invalid array operator value: ${operator} requires an array value`);
112
+ const normalizedValues = this.normalizeArrayValues(value);
113
+ const fieldWithKeyword = this.addKeywordIfNeeded(field, value);
114
+ switch (operator) {
115
+ case "$in": return { terms: { [fieldWithKeyword]: normalizedValues } };
116
+ case "$nin":
117
+ if (normalizedValues.length === 0) return { match_all: {} };
118
+ return { bool: { must_not: [{ terms: { [fieldWithKeyword]: normalizedValues } }] } };
119
+ case "$all":
120
+ if (normalizedValues.length === 0) return { bool: { must_not: [{ match_all: {} }] } };
121
+ return { bool: { must: normalizedValues.map((v) => ({ term: { [fieldWithKeyword]: v } })) } };
122
+ default: return { terms: { [fieldWithKeyword]: normalizedValues } };
123
+ }
124
+ }
125
+ if (this.isElementOperator(operator)) switch (operator) {
126
+ case "$exists": return value ? { exists: { field } } : { bool: { must_not: [{ exists: { field } }] } };
127
+ default: return { exists: { field } };
128
+ }
129
+ if (this.isRegexOperator(operator)) return this.translateRegexOperator(field, value);
130
+ return { term: { [this.addKeywordIfNeeded(field, value)]: value } };
131
+ }
132
+ /**
133
+ * Translates regex patterns to OpenSearch query syntax
134
+ */
135
+ translateRegexOperator(field, value) {
136
+ const regexValue = typeof value === "string" ? value : value.toString();
137
+ if (regexValue.includes("\n") || regexValue.includes("\r")) return { match: { [field]: value } };
138
+ let processedRegex = regexValue;
139
+ const hasStartAnchor = regexValue.startsWith("^");
140
+ const hasEndAnchor = regexValue.endsWith("$");
141
+ if (hasStartAnchor || hasEndAnchor) {
142
+ if (hasStartAnchor) processedRegex = processedRegex.substring(1);
143
+ if (hasEndAnchor) processedRegex = processedRegex.substring(0, processedRegex.length - 1);
144
+ let wildcardPattern = processedRegex;
145
+ if (!hasStartAnchor) wildcardPattern = "*" + wildcardPattern;
146
+ if (!hasEndAnchor) wildcardPattern = wildcardPattern + "*";
147
+ return { wildcard: { [field]: wildcardPattern } };
148
+ }
149
+ const escapedRegex = regexValue.replace(/\\/g, "\\\\");
150
+ return { regexp: { [field]: escapedRegex } };
151
+ }
152
+ addKeywordIfNeeded(field, value) {
153
+ if (typeof value === "string") return `${field}.keyword`;
154
+ if (Array.isArray(value) && value.every((item) => typeof item === "string")) return `${field}.keyword`;
155
+ return field;
156
+ }
157
+ /**
158
+ * Helper method to handle special cases for the $not operator
159
+ */
160
+ handleNotOperatorSpecialCases(value, field) {
161
+ if (value === null) return { exists: { field } };
162
+ if (typeof value === "object" && value !== null) {
163
+ if ("$eq" in value && value.$eq === null) return { exists: { field } };
164
+ if ("$ne" in value && value.$ne === null) return { bool: { must_not: [{ exists: { field } }] } };
165
+ }
166
+ return null;
167
+ }
168
+ translateOperator(operator, value, field) {
169
+ if (!this.isOperator(operator)) throw new Error(`Unsupported operator: ${operator}`);
170
+ if (operator === "$not" && field) {
171
+ const specialCaseResult = this.handleNotOperatorSpecialCases(value, field);
172
+ if (specialCaseResult) return specialCaseResult;
173
+ }
174
+ if (this.isLogicalOperator(operator)) {
175
+ if (operator === "$not" && field && typeof value === "object" && value !== null && !Array.isArray(value)) {
176
+ const entries = Object.entries(value);
177
+ if (entries.length > 0) {
178
+ if (entries.every(([op]) => this.isOperator(op))) return { bool: { must_not: [this.translateFieldConditions(field, value)] } };
179
+ if (entries.length === 1 && entries[0] && this.isOperator(entries[0][0])) {
180
+ const [nestedOp, nestedVal] = entries[0];
181
+ return { bool: { must_not: [this.translateFieldOperator(field, nestedOp, nestedVal)] } };
182
+ }
183
+ }
184
+ }
185
+ return this.translateLogicalOperator(operator, value);
186
+ }
187
+ if (field) return this.translateFieldOperator(field, operator, value);
188
+ return value;
189
+ }
190
+ /**
191
+ * Translates field conditions to OpenSearch query syntax
192
+ * Handles special cases like range queries and multiple operators
193
+ */
194
+ translateFieldConditions(field, conditions) {
195
+ if (this.canOptimizeToRangeQuery(conditions)) return this.createRangeQuery(field, conditions);
196
+ const queryConditions = [];
197
+ Object.entries(conditions).forEach(([operator, value]) => {
198
+ if (this.isOperator(operator)) queryConditions.push(this.translateOperator(operator, value, field));
199
+ else {
200
+ const fieldWithKeyword = this.addKeywordIfNeeded(`${field}.${operator}`, value);
201
+ queryConditions.push({ term: { [fieldWithKeyword]: value } });
202
+ }
203
+ });
204
+ if (queryConditions.length === 1) return queryConditions[0];
205
+ return { bool: { must: queryConditions } };
206
+ }
207
+ /**
208
+ * Checks if conditions can be optimized to a range query
209
+ */
210
+ canOptimizeToRangeQuery(conditions) {
211
+ return Object.keys(conditions).every((op) => this.isNumericOperator(op)) && Object.keys(conditions).length > 0;
212
+ }
213
+ /**
214
+ * Creates a range query from numeric operators
215
+ */
216
+ createRangeQuery(field, conditions) {
217
+ const rangeParams = Object.fromEntries(Object.entries(conditions).map(([op, val]) => [op.replace("$", ""), this.normalizeComparisonValue(val)]));
218
+ return { range: { [field]: rangeParams } };
219
+ }
363
220
  };
364
-
365
- // src/vector/index.ts
366
- var METRIC_MAPPING = {
367
- cosine: "cosinesimil",
368
- euclidean: "l2",
369
- dotproduct: "innerproduct"
221
+ //#endregion
222
+ //#region src/vector/index.ts
223
+ const METRIC_MAPPING = {
224
+ cosine: "cosinesimil",
225
+ euclidean: "l2",
226
+ dotproduct: "innerproduct"
370
227
  };
371
- var REVERSE_METRIC_MAPPING = {
372
- cosinesimil: "cosine",
373
- l2: "euclidean",
374
- innerproduct: "dotproduct"
228
+ const REVERSE_METRIC_MAPPING = {
229
+ cosinesimil: "cosine",
230
+ l2: "euclidean",
231
+ innerproduct: "dotproduct"
375
232
  };
376
- var OpenSearchVector = class extends vector.MastraVector {
377
- client;
378
- /**
379
- * Creates a new OpenSearchVector client.
380
- *
381
- * @param config - OpenSearch client configuration options plus a required id.
382
- * @see OpenSearchVectorConfig for all available options.
383
- */
384
- constructor({ id, ...clientOptions }) {
385
- super({ id });
386
- this.client = new opensearch.Client(clientOptions);
387
- }
388
- /**
389
- * Creates a new collection with the specified configuration.
390
- *
391
- * @param {string} indexName - The name of the collection to create.
392
- * @param {number} dimension - The dimension of the vectors to be stored in the collection.
393
- * @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.
394
- * @returns {Promise<void>} A promise that resolves when the collection is created.
395
- */
396
- async createIndex({ indexName, dimension, metric = "cosine" }) {
397
- if (!Number.isInteger(dimension) || dimension <= 0) {
398
- throw new error.MastraError({
399
- id: storage.createVectorErrorId("OPENSEARCH", "CREATE_INDEX", "INVALID_ARGS"),
400
- domain: error.ErrorDomain.STORAGE,
401
- category: error.ErrorCategory.USER,
402
- text: "Dimension must be a positive integer",
403
- details: { indexName, dimension }
404
- });
405
- }
406
- try {
407
- await this.client.indices.create({
408
- index: indexName,
409
- body: {
410
- settings: { index: { knn: true } },
411
- mappings: {
412
- properties: {
413
- metadata: { type: "object" },
414
- id: { type: "keyword" },
415
- embedding: {
416
- type: "knn_vector",
417
- dimension,
418
- method: {
419
- name: "hnsw",
420
- space_type: METRIC_MAPPING[metric],
421
- engine: "faiss",
422
- parameters: { ef_construction: 128, m: 16 }
423
- }
424
- }
425
- }
426
- }
427
- }
428
- });
429
- } catch (error$1) {
430
- const message = error$1?.message || error$1?.toString();
431
- if (message && message.toLowerCase().includes("already exists")) {
432
- await this.validateExistingIndex(indexName, dimension, metric);
433
- return;
434
- }
435
- throw new error.MastraError(
436
- {
437
- id: storage.createVectorErrorId("OPENSEARCH", "CREATE_INDEX", "FAILED"),
438
- domain: error.ErrorDomain.STORAGE,
439
- category: error.ErrorCategory.THIRD_PARTY,
440
- details: { indexName, dimension, metric }
441
- },
442
- error$1
443
- );
444
- }
445
- }
446
- /**
447
- * Lists all indexes.
448
- *
449
- * @returns {Promise<string[]>} A promise that resolves to an array of indexes.
450
- */
451
- async listIndexes() {
452
- try {
453
- const response = await this.client.cat.indices({ format: "json" });
454
- const indexes = response.body.map((record) => record.index).filter((index) => index !== void 0);
455
- return indexes;
456
- } catch (error$1) {
457
- throw new error.MastraError(
458
- {
459
- id: storage.createVectorErrorId("OPENSEARCH", "LIST_INDEXES", "FAILED"),
460
- domain: error.ErrorDomain.STORAGE,
461
- category: error.ErrorCategory.THIRD_PARTY
462
- },
463
- error$1
464
- );
465
- }
466
- }
467
- /**
468
- * Retrieves statistics about a vector index.
469
- *
470
- * @param {string} indexName - The name of the index to describe
471
- * @returns A promise that resolves to the index statistics including dimension, count and metric
472
- */
473
- async describeIndex({ indexName }) {
474
- const { body: indexInfo } = await this.client.indices.get({ index: indexName });
475
- const mappings = indexInfo[indexName]?.mappings;
476
- const embedding = mappings?.properties?.embedding;
477
- const spaceType = embedding.method.space_type;
478
- const { body: countInfo } = await this.client.count({ index: indexName });
479
- return {
480
- dimension: Number(embedding.dimension),
481
- count: Number(countInfo.count),
482
- metric: REVERSE_METRIC_MAPPING[spaceType]
483
- };
484
- }
485
- /**
486
- * Deletes the specified index.
487
- *
488
- * @param {string} indexName - The name of the index to delete.
489
- * @returns {Promise<void>} A promise that resolves when the index is deleted.
490
- */
491
- async deleteIndex({ indexName }) {
492
- try {
493
- await this.client.indices.delete({ index: indexName });
494
- } catch (error$1) {
495
- const mastraError = new error.MastraError(
496
- {
497
- id: storage.createVectorErrorId("OPENSEARCH", "DELETE_INDEX", "FAILED"),
498
- domain: error.ErrorDomain.STORAGE,
499
- category: error.ErrorCategory.THIRD_PARTY,
500
- details: { indexName }
501
- },
502
- error$1
503
- );
504
- this.logger?.error(mastraError.toString());
505
- this.logger?.trackException(mastraError);
506
- }
507
- }
508
- /**
509
- * Inserts or updates vectors in the specified collection.
510
- *
511
- * @param {string} indexName - The name of the collection to upsert into.
512
- * @param {number[][]} vectors - An array of vectors to upsert.
513
- * @param {Record<string, any>[]} [metadata] - An optional array of metadata objects corresponding to each vector.
514
- * @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.
515
- * @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
516
- */
517
- async upsert({ indexName, vectors, metadata = [], ids }) {
518
- vector.validateUpsert("OPENSEARCH", vectors, metadata, ids, true);
519
- const vectorIds = ids || vectors.map(() => crypto.randomUUID());
520
- const operations = [];
521
- try {
522
- const indexInfo = await this.describeIndex({ indexName });
523
- this.validateVectorDimensions(vectors, indexInfo.dimension);
524
- for (let i = 0; i < vectors.length; i++) {
525
- const operation = {
526
- index: {
527
- _index: indexName,
528
- _id: vectorIds[i]
529
- }
530
- };
531
- const document = {
532
- id: vectorIds[i],
533
- embedding: vectors[i],
534
- metadata: metadata[i] || {}
535
- };
536
- operations.push(operation);
537
- operations.push(document);
538
- }
539
- if (operations.length > 0) {
540
- await this.client.bulk({ body: operations, refresh: true });
541
- }
542
- return vectorIds;
543
- } catch (error$1) {
544
- throw new error.MastraError(
545
- {
546
- id: storage.createVectorErrorId("OPENSEARCH", "UPSERT", "FAILED"),
547
- domain: error.ErrorDomain.STORAGE,
548
- category: error.ErrorCategory.THIRD_PARTY,
549
- details: { indexName, vectorCount: vectors?.length || 0 }
550
- },
551
- error$1
552
- );
553
- }
554
- }
555
- /**
556
- * Queries the specified collection using a vector and optional filter.
557
- *
558
- * @param {string} indexName - The name of the collection to query.
559
- * @param {number[]} queryVector - The vector to query with.
560
- * @param {number} [topK] - The maximum number of results to return.
561
- * @param {Record<string, any>} [filter] - An optional filter to apply to the query. For more on filters in OpenSearch, see the filtering reference: https://opensearch.org/docs/latest/query-dsl/
562
- * @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
563
- * @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
564
- */
565
- async query({
566
- indexName,
567
- queryVector,
568
- filter,
569
- topK = 10,
570
- includeVector = false
571
- }) {
572
- if (!queryVector) {
573
- throw new error.MastraError({
574
- id: storage.createVectorErrorId("OPENSEARCH", "QUERY", "MISSING_VECTOR"),
575
- text: "queryVector is required for OpenSearch queries. Metadata-only queries are not supported by this vector store.",
576
- domain: error.ErrorDomain.STORAGE,
577
- category: error.ErrorCategory.USER,
578
- details: { indexName }
579
- });
580
- }
581
- vector.validateTopK("OPENSEARCH", topK);
582
- try {
583
- const translatedFilter = this.transformFilter(filter);
584
- const response = await this.client.search({
585
- index: indexName,
586
- body: {
587
- query: {
588
- bool: {
589
- must: { knn: { embedding: { vector: queryVector, k: topK } } },
590
- filter: translatedFilter ? [translatedFilter] : []
591
- }
592
- },
593
- _source: ["id", "metadata", "embedding"]
594
- }
595
- });
596
- const results = response.body.hits.hits.map((hit) => {
597
- const source = hit._source || {};
598
- return {
599
- id: String(source.id || ""),
600
- score: typeof hit._score === "number" ? hit._score : 0,
601
- metadata: source.metadata || {},
602
- ...includeVector && { vector: source.embedding }
603
- };
604
- });
605
- return results;
606
- } catch (error$1) {
607
- throw new error.MastraError(
608
- {
609
- id: storage.createVectorErrorId("OPENSEARCH", "QUERY", "FAILED"),
610
- domain: error.ErrorDomain.STORAGE,
611
- category: error.ErrorCategory.THIRD_PARTY,
612
- details: { indexName, topK }
613
- },
614
- error$1
615
- );
616
- }
617
- }
618
- /**
619
- * Validates the dimensions of the vectors.
620
- *
621
- * @param {number[][]} vectors - The vectors to validate.
622
- * @param {number} dimension - The dimension of the vectors.
623
- * @returns {void}
624
- */
625
- validateVectorDimensions(vectors, dimension) {
626
- if (vectors.some((vector) => vector.length !== dimension)) {
627
- throw new Error("Vector dimension does not match index dimension");
628
- }
629
- }
630
- /**
631
- * Transforms the filter to the OpenSearch DSL.
632
- *
633
- * @param {OpenSearchVectorFilter} filter - The filter to transform.
634
- * @returns {Record<string, any>} The transformed filter.
635
- */
636
- transformFilter(filter) {
637
- const translator = new OpenSearchFilterTranslator();
638
- return translator.translate(filter);
639
- }
640
- /**
641
- * Updates vectors by ID or filter with the provided vector and/or metadata.
642
- * @param params - Parameters containing either id or filter for targeting vectors to update
643
- * @param params.indexName - The name of the index containing the vector(s).
644
- * @param params.id - The ID of a single vector to update (mutually exclusive with filter).
645
- * @param params.filter - A filter to match multiple vectors to update (mutually exclusive with id).
646
- * @param params.update - An object containing the vector and/or metadata to update.
647
- * @returns A promise that resolves when the update is complete.
648
- * @throws Will throw an error if no updates are provided or if the update operation fails.
649
- */
650
- async updateVector(params) {
651
- const { indexName, update } = params;
652
- if ("id" in params && "filter" in params && params.id && params.filter) {
653
- throw new error.MastraError({
654
- id: storage.createVectorErrorId("OPENSEARCH", "UPDATE_VECTOR", "MUTUALLY_EXCLUSIVE"),
655
- domain: error.ErrorDomain.STORAGE,
656
- category: error.ErrorCategory.USER,
657
- text: "id and filter are mutually exclusive",
658
- details: { indexName }
659
- });
660
- }
661
- if (!update.vector && !update.metadata) {
662
- throw new error.MastraError({
663
- id: storage.createVectorErrorId("OPENSEARCH", "UPDATE_VECTOR", "NO_UPDATES"),
664
- domain: error.ErrorDomain.STORAGE,
665
- category: error.ErrorCategory.USER,
666
- text: "No updates provided",
667
- details: { indexName }
668
- });
669
- }
670
- if ("filter" in params && params.filter && Object.keys(params.filter).length === 0) {
671
- throw new error.MastraError({
672
- id: storage.createVectorErrorId("OPENSEARCH", "UPDATE_VECTOR", "EMPTY_FILTER"),
673
- domain: error.ErrorDomain.STORAGE,
674
- category: error.ErrorCategory.USER,
675
- text: "Cannot update with empty filter",
676
- details: { indexName }
677
- });
678
- }
679
- if ("id" in params && params.id) {
680
- await this.updateVectorById(indexName, params.id, update);
681
- } else if ("filter" in params && params.filter) {
682
- await this.updateVectorsByFilter(indexName, params.filter, update);
683
- } else {
684
- throw new error.MastraError({
685
- id: storage.createVectorErrorId("OPENSEARCH", "UPDATE_VECTOR", "NO_TARGET"),
686
- domain: error.ErrorDomain.STORAGE,
687
- category: error.ErrorCategory.USER,
688
- text: "Either id or filter must be provided",
689
- details: { indexName }
690
- });
691
- }
692
- }
693
- /**
694
- * Updates a single vector by its ID.
695
- */
696
- async updateVectorById(indexName, id, update) {
697
- let existingDoc;
698
- try {
699
- const { body } = await this.client.get({
700
- index: indexName,
701
- id
702
- }).catch(() => {
703
- throw new Error(`Document with ID ${id} not found in index ${indexName}`);
704
- });
705
- if (!body || !body._source) {
706
- throw new Error(`Document with ID ${id} has no source data in index ${indexName}`);
707
- }
708
- existingDoc = body;
709
- } catch (error$1) {
710
- throw new error.MastraError(
711
- {
712
- id: storage.createVectorErrorId("OPENSEARCH", "UPDATE_VECTOR", "FAILED"),
713
- domain: error.ErrorDomain.STORAGE,
714
- category: error.ErrorCategory.USER,
715
- details: {
716
- indexName,
717
- id
718
- }
719
- },
720
- error$1
721
- );
722
- }
723
- const source = existingDoc._source;
724
- const updatedDoc = {
725
- id: source?.id || id
726
- };
727
- try {
728
- if (update.vector) {
729
- const indexInfo = await this.describeIndex({ indexName });
730
- this.validateVectorDimensions([update.vector], indexInfo.dimension);
731
- updatedDoc.embedding = update.vector;
732
- } else if (source?.embedding) {
733
- updatedDoc.embedding = source.embedding;
734
- }
735
- if (update.metadata) {
736
- updatedDoc.metadata = update.metadata;
737
- } else {
738
- updatedDoc.metadata = source?.metadata || {};
739
- }
740
- await this.client.index({
741
- index: indexName,
742
- id,
743
- body: updatedDoc,
744
- refresh: true
745
- });
746
- } catch (error$1) {
747
- throw new error.MastraError(
748
- {
749
- id: storage.createVectorErrorId("OPENSEARCH", "UPDATE_VECTOR", "FAILED"),
750
- domain: error.ErrorDomain.STORAGE,
751
- category: error.ErrorCategory.THIRD_PARTY,
752
- details: {
753
- indexName,
754
- id
755
- }
756
- },
757
- error$1
758
- );
759
- }
760
- }
761
- /**
762
- * Updates multiple vectors matching a filter.
763
- */
764
- async updateVectorsByFilter(indexName, filter, update) {
765
- try {
766
- const translator = new OpenSearchFilterTranslator();
767
- const translatedFilter = translator.translate(filter);
768
- const scriptSource = [];
769
- const scriptParams = {};
770
- if (update.vector) {
771
- scriptSource.push("ctx._source.embedding = params.embedding");
772
- scriptParams.embedding = update.vector;
773
- }
774
- if (update.metadata) {
775
- scriptSource.push("ctx._source.metadata = params.metadata");
776
- scriptParams.metadata = update.metadata;
777
- }
778
- await this.client.updateByQuery({
779
- index: indexName,
780
- body: {
781
- query: translatedFilter || { match_all: {} },
782
- script: {
783
- source: scriptSource.join("; "),
784
- params: scriptParams,
785
- lang: "painless"
786
- }
787
- },
788
- refresh: true
789
- });
790
- } catch (error$1) {
791
- throw new error.MastraError(
792
- {
793
- id: storage.createVectorErrorId("OPENSEARCH", "UPDATE_VECTOR_BY_FILTER", "FAILED"),
794
- domain: error.ErrorDomain.STORAGE,
795
- category: error.ErrorCategory.THIRD_PARTY,
796
- details: {
797
- indexName,
798
- filter: JSON.stringify(filter)
799
- }
800
- },
801
- error$1
802
- );
803
- }
804
- }
805
- /**
806
- * Deletes a vector by its ID.
807
- * @param indexName - The name of the index containing the vector.
808
- * @param id - The ID of the vector to delete.
809
- * @returns A promise that resolves when the deletion is complete.
810
- * @throws Will throw an error if the deletion operation fails.
811
- */
812
- async deleteVector({ indexName, id }) {
813
- try {
814
- await this.client.delete({
815
- index: indexName,
816
- id,
817
- refresh: true
818
- });
819
- } catch (error$1) {
820
- if (error$1 && typeof error$1 === "object" && "statusCode" in error$1 && error$1.statusCode === 404) {
821
- return;
822
- }
823
- throw new error.MastraError(
824
- {
825
- id: storage.createVectorErrorId("OPENSEARCH", "DELETE_VECTOR", "FAILED"),
826
- domain: error.ErrorDomain.STORAGE,
827
- category: error.ErrorCategory.THIRD_PARTY,
828
- details: {
829
- indexName,
830
- ...id && { id }
831
- }
832
- },
833
- error$1
834
- );
835
- }
836
- }
837
- async deleteVectors({ indexName, filter, ids }) {
838
- if (ids && filter) {
839
- throw new error.MastraError({
840
- id: storage.createVectorErrorId("OPENSEARCH", "DELETE_VECTORS", "MUTUALLY_EXCLUSIVE"),
841
- domain: error.ErrorDomain.STORAGE,
842
- category: error.ErrorCategory.USER,
843
- text: "ids and filter are mutually exclusive",
844
- details: { indexName }
845
- });
846
- }
847
- if (!ids && !filter) {
848
- throw new error.MastraError({
849
- id: storage.createVectorErrorId("OPENSEARCH", "DELETE_VECTORS", "NO_TARGET"),
850
- domain: error.ErrorDomain.STORAGE,
851
- category: error.ErrorCategory.USER,
852
- text: "Either filter or ids must be provided",
853
- details: { indexName }
854
- });
855
- }
856
- if (ids && ids.length === 0) {
857
- throw new error.MastraError({
858
- id: storage.createVectorErrorId("OPENSEARCH", "DELETE_VECTORS", "EMPTY_IDS"),
859
- domain: error.ErrorDomain.STORAGE,
860
- category: error.ErrorCategory.USER,
861
- text: "Cannot delete with empty ids array",
862
- details: { indexName }
863
- });
864
- }
865
- if (filter && Object.keys(filter).length === 0) {
866
- throw new error.MastraError({
867
- id: storage.createVectorErrorId("OPENSEARCH", "DELETE_VECTORS", "EMPTY_FILTER"),
868
- domain: error.ErrorDomain.STORAGE,
869
- category: error.ErrorCategory.USER,
870
- text: "Cannot delete with empty filter",
871
- details: { indexName }
872
- });
873
- }
874
- try {
875
- if (ids) {
876
- const bulkBody = ids.flatMap((id) => [{ delete: { _index: indexName, _id: id } }]);
877
- await this.client.bulk({
878
- body: bulkBody,
879
- refresh: true
880
- });
881
- } else if (filter) {
882
- const translator = new OpenSearchFilterTranslator();
883
- const translatedFilter = translator.translate(filter);
884
- await this.client.deleteByQuery({
885
- index: indexName,
886
- body: {
887
- query: translatedFilter || { match_all: {} }
888
- },
889
- refresh: true
890
- });
891
- }
892
- } catch (error$1) {
893
- if (error$1 instanceof error.MastraError) throw error$1;
894
- throw new error.MastraError(
895
- {
896
- id: storage.createVectorErrorId("OPENSEARCH", "DELETE_VECTORS", "FAILED"),
897
- domain: error.ErrorDomain.STORAGE,
898
- category: error.ErrorCategory.THIRD_PARTY,
899
- details: {
900
- indexName,
901
- ...filter && { filter: JSON.stringify(filter) },
902
- ...ids && { idsCount: ids.length }
903
- }
904
- },
905
- error$1
906
- );
907
- }
908
- }
233
+ var OpenSearchVector = class extends _mastra_core_vector.MastraVector {
234
+ client;
235
+ /**
236
+ * Creates a new OpenSearchVector client.
237
+ *
238
+ * @param config - OpenSearch client configuration options plus a required id.
239
+ * @see OpenSearchVectorConfig for all available options.
240
+ */
241
+ constructor({ id, ...clientOptions }) {
242
+ super({ id });
243
+ this.client = new _opensearch_project_opensearch.Client(clientOptions);
244
+ }
245
+ /**
246
+ * Creates a new collection with the specified configuration.
247
+ *
248
+ * @param {string} indexName - The name of the collection to create.
249
+ * @param {number} dimension - The dimension of the vectors to be stored in the collection.
250
+ * @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.
251
+ * @returns {Promise<void>} A promise that resolves when the collection is created.
252
+ */
253
+ async createIndex({ indexName, dimension, metric = "cosine" }) {
254
+ if (!Number.isInteger(dimension) || dimension <= 0) throw new _mastra_core_error.MastraError({
255
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "CREATE_INDEX", "INVALID_ARGS"),
256
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
257
+ category: _mastra_core_error.ErrorCategory.USER,
258
+ text: "Dimension must be a positive integer",
259
+ details: {
260
+ indexName,
261
+ dimension
262
+ }
263
+ });
264
+ try {
265
+ await this.client.indices.create({
266
+ index: indexName,
267
+ body: {
268
+ settings: { index: { knn: true } },
269
+ mappings: { properties: {
270
+ metadata: { type: "object" },
271
+ id: { type: "keyword" },
272
+ embedding: {
273
+ type: "knn_vector",
274
+ dimension,
275
+ method: {
276
+ name: "hnsw",
277
+ space_type: METRIC_MAPPING[metric],
278
+ engine: "faiss",
279
+ parameters: {
280
+ ef_construction: 128,
281
+ m: 16
282
+ }
283
+ }
284
+ }
285
+ } }
286
+ }
287
+ });
288
+ } catch (error) {
289
+ const message = error?.message || error?.toString();
290
+ if (message && message.toLowerCase().includes("already exists")) {
291
+ await this.validateExistingIndex(indexName, dimension, metric);
292
+ return;
293
+ }
294
+ throw new _mastra_core_error.MastraError({
295
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "CREATE_INDEX", "FAILED"),
296
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
297
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
298
+ details: {
299
+ indexName,
300
+ dimension,
301
+ metric
302
+ }
303
+ }, error);
304
+ }
305
+ }
306
+ /**
307
+ * Lists all indexes.
308
+ *
309
+ * @returns {Promise<string[]>} A promise that resolves to an array of indexes.
310
+ */
311
+ async listIndexes() {
312
+ try {
313
+ return (await this.client.cat.indices({ format: "json" })).body.map((record) => record.index).filter((index) => index !== void 0);
314
+ } catch (error) {
315
+ throw new _mastra_core_error.MastraError({
316
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "LIST_INDEXES", "FAILED"),
317
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
318
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY
319
+ }, error);
320
+ }
321
+ }
322
+ /**
323
+ * Retrieves statistics about a vector index.
324
+ *
325
+ * @param {string} indexName - The name of the index to describe
326
+ * @returns A promise that resolves to the index statistics including dimension, count and metric
327
+ */
328
+ async describeIndex({ indexName }) {
329
+ const { body: indexInfo } = await this.client.indices.get({ index: indexName });
330
+ const embedding = (indexInfo[indexName]?.mappings)?.properties?.embedding;
331
+ const spaceType = embedding.method.space_type;
332
+ const { body: countInfo } = await this.client.count({ index: indexName });
333
+ return {
334
+ dimension: Number(embedding.dimension),
335
+ count: Number(countInfo.count),
336
+ metric: REVERSE_METRIC_MAPPING[spaceType]
337
+ };
338
+ }
339
+ /**
340
+ * Deletes the specified index.
341
+ *
342
+ * @param {string} indexName - The name of the index to delete.
343
+ * @returns {Promise<void>} A promise that resolves when the index is deleted.
344
+ */
345
+ async deleteIndex({ indexName }) {
346
+ try {
347
+ await this.client.indices.delete({ index: indexName });
348
+ } catch (error) {
349
+ const mastraError = new _mastra_core_error.MastraError({
350
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "DELETE_INDEX", "FAILED"),
351
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
352
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
353
+ details: { indexName }
354
+ }, error);
355
+ this.logger?.error(mastraError.toString());
356
+ this.logger?.trackException(mastraError);
357
+ }
358
+ }
359
+ /**
360
+ * Inserts or updates vectors in the specified collection.
361
+ *
362
+ * @param {string} indexName - The name of the collection to upsert into.
363
+ * @param {number[][]} vectors - An array of vectors to upsert.
364
+ * @param {Record<string, any>[]} [metadata] - An optional array of metadata objects corresponding to each vector.
365
+ * @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.
366
+ * @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
367
+ */
368
+ async upsert({ indexName, vectors, metadata = [], ids }) {
369
+ (0, _mastra_core_vector.validateUpsert)("OPENSEARCH", vectors, metadata, ids, true);
370
+ const vectorIds = ids || vectors.map(() => crypto.randomUUID());
371
+ const operations = [];
372
+ try {
373
+ const indexInfo = await this.describeIndex({ indexName });
374
+ this.validateVectorDimensions(vectors, indexInfo.dimension);
375
+ for (let i = 0; i < vectors.length; i++) {
376
+ const operation = { index: {
377
+ _index: indexName,
378
+ _id: vectorIds[i]
379
+ } };
380
+ const document = {
381
+ id: vectorIds[i],
382
+ embedding: vectors[i],
383
+ metadata: metadata[i] || {}
384
+ };
385
+ operations.push(operation);
386
+ operations.push(document);
387
+ }
388
+ if (operations.length > 0) await this.client.bulk({
389
+ body: operations,
390
+ refresh: true
391
+ });
392
+ return vectorIds;
393
+ } catch (error) {
394
+ throw new _mastra_core_error.MastraError({
395
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "UPSERT", "FAILED"),
396
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
397
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
398
+ details: {
399
+ indexName,
400
+ vectorCount: vectors?.length || 0
401
+ }
402
+ }, error);
403
+ }
404
+ }
405
+ /**
406
+ * Queries the specified collection using a vector and optional filter.
407
+ *
408
+ * @param {string} indexName - The name of the collection to query.
409
+ * @param {number[]} queryVector - The vector to query with.
410
+ * @param {number} [topK] - The maximum number of results to return.
411
+ * @param {Record<string, any>} [filter] - An optional filter to apply to the query. For more on filters in OpenSearch, see the filtering reference: https://opensearch.org/docs/latest/query-dsl/
412
+ * @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
413
+ * @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
414
+ */
415
+ async query({ indexName, queryVector, filter, topK = 10, includeVector = false }) {
416
+ if (!queryVector) throw new _mastra_core_error.MastraError({
417
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "QUERY", "MISSING_VECTOR"),
418
+ text: "queryVector is required for OpenSearch queries. Metadata-only queries are not supported by this vector store.",
419
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
420
+ category: _mastra_core_error.ErrorCategory.USER,
421
+ details: { indexName }
422
+ });
423
+ (0, _mastra_core_vector.validateTopK)("OPENSEARCH", topK);
424
+ try {
425
+ const translatedFilter = this.transformFilter(filter);
426
+ return (await this.client.search({
427
+ index: indexName,
428
+ body: {
429
+ query: { bool: {
430
+ must: { knn: { embedding: {
431
+ vector: queryVector,
432
+ k: topK
433
+ } } },
434
+ filter: translatedFilter ? [translatedFilter] : []
435
+ } },
436
+ _source: [
437
+ "id",
438
+ "metadata",
439
+ "embedding"
440
+ ]
441
+ }
442
+ })).body.hits.hits.map((hit) => {
443
+ const source = hit._source || {};
444
+ return {
445
+ id: String(source.id || ""),
446
+ score: typeof hit._score === "number" ? hit._score : 0,
447
+ metadata: source.metadata || {},
448
+ ...includeVector && { vector: source.embedding }
449
+ };
450
+ });
451
+ } catch (error) {
452
+ throw new _mastra_core_error.MastraError({
453
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "QUERY", "FAILED"),
454
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
455
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
456
+ details: {
457
+ indexName,
458
+ topK
459
+ }
460
+ }, error);
461
+ }
462
+ }
463
+ /**
464
+ * Validates the dimensions of the vectors.
465
+ *
466
+ * @param {number[][]} vectors - The vectors to validate.
467
+ * @param {number} dimension - The dimension of the vectors.
468
+ * @returns {void}
469
+ */
470
+ validateVectorDimensions(vectors, dimension) {
471
+ if (vectors.some((vector) => vector.length !== dimension)) throw new Error("Vector dimension does not match index dimension");
472
+ }
473
+ /**
474
+ * Transforms the filter to the OpenSearch DSL.
475
+ *
476
+ * @param {OpenSearchVectorFilter} filter - The filter to transform.
477
+ * @returns {Record<string, any>} The transformed filter.
478
+ */
479
+ transformFilter(filter) {
480
+ return new OpenSearchFilterTranslator().translate(filter);
481
+ }
482
+ /**
483
+ * Updates vectors by ID or filter with the provided vector and/or metadata.
484
+ * @param params - Parameters containing either id or filter for targeting vectors to update
485
+ * @param params.indexName - The name of the index containing the vector(s).
486
+ * @param params.id - The ID of a single vector to update (mutually exclusive with filter).
487
+ * @param params.filter - A filter to match multiple vectors to update (mutually exclusive with id).
488
+ * @param params.update - An object containing the vector and/or metadata to update.
489
+ * @returns A promise that resolves when the update is complete.
490
+ * @throws Will throw an error if no updates are provided or if the update operation fails.
491
+ */
492
+ async updateVector(params) {
493
+ const { indexName, update } = params;
494
+ if ("id" in params && "filter" in params && params.id && params.filter) throw new _mastra_core_error.MastraError({
495
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "UPDATE_VECTOR", "MUTUALLY_EXCLUSIVE"),
496
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
497
+ category: _mastra_core_error.ErrorCategory.USER,
498
+ text: "id and filter are mutually exclusive",
499
+ details: { indexName }
500
+ });
501
+ if (!update.vector && !update.metadata) throw new _mastra_core_error.MastraError({
502
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "UPDATE_VECTOR", "NO_UPDATES"),
503
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
504
+ category: _mastra_core_error.ErrorCategory.USER,
505
+ text: "No updates provided",
506
+ details: { indexName }
507
+ });
508
+ if ("filter" in params && params.filter && Object.keys(params.filter).length === 0) throw new _mastra_core_error.MastraError({
509
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "UPDATE_VECTOR", "EMPTY_FILTER"),
510
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
511
+ category: _mastra_core_error.ErrorCategory.USER,
512
+ text: "Cannot update with empty filter",
513
+ details: { indexName }
514
+ });
515
+ if ("id" in params && params.id) await this.updateVectorById(indexName, params.id, update);
516
+ else if ("filter" in params && params.filter) await this.updateVectorsByFilter(indexName, params.filter, update);
517
+ else throw new _mastra_core_error.MastraError({
518
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "UPDATE_VECTOR", "NO_TARGET"),
519
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
520
+ category: _mastra_core_error.ErrorCategory.USER,
521
+ text: "Either id or filter must be provided",
522
+ details: { indexName }
523
+ });
524
+ }
525
+ /**
526
+ * Updates a single vector by its ID.
527
+ */
528
+ async updateVectorById(indexName, id, update) {
529
+ let existingDoc;
530
+ try {
531
+ const { body } = await this.client.get({
532
+ index: indexName,
533
+ id
534
+ }).catch(() => {
535
+ throw new Error(`Document with ID ${id} not found in index ${indexName}`);
536
+ });
537
+ if (!body || !body._source) throw new Error(`Document with ID ${id} has no source data in index ${indexName}`);
538
+ existingDoc = body;
539
+ } catch (error) {
540
+ throw new _mastra_core_error.MastraError({
541
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "UPDATE_VECTOR", "FAILED"),
542
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
543
+ category: _mastra_core_error.ErrorCategory.USER,
544
+ details: {
545
+ indexName,
546
+ id
547
+ }
548
+ }, error);
549
+ }
550
+ const source = existingDoc._source;
551
+ const updatedDoc = { id: source?.id || id };
552
+ try {
553
+ if (update.vector) {
554
+ const indexInfo = await this.describeIndex({ indexName });
555
+ this.validateVectorDimensions([update.vector], indexInfo.dimension);
556
+ updatedDoc.embedding = update.vector;
557
+ } else if (source?.embedding) updatedDoc.embedding = source.embedding;
558
+ if (update.metadata) updatedDoc.metadata = update.metadata;
559
+ else updatedDoc.metadata = source?.metadata || {};
560
+ await this.client.index({
561
+ index: indexName,
562
+ id,
563
+ body: updatedDoc,
564
+ refresh: true
565
+ });
566
+ } catch (error) {
567
+ throw new _mastra_core_error.MastraError({
568
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "UPDATE_VECTOR", "FAILED"),
569
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
570
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
571
+ details: {
572
+ indexName,
573
+ id
574
+ }
575
+ }, error);
576
+ }
577
+ }
578
+ /**
579
+ * Updates multiple vectors matching a filter.
580
+ */
581
+ async updateVectorsByFilter(indexName, filter, update) {
582
+ try {
583
+ const translatedFilter = new OpenSearchFilterTranslator().translate(filter);
584
+ const scriptSource = [];
585
+ const scriptParams = {};
586
+ if (update.vector) {
587
+ scriptSource.push("ctx._source.embedding = params.embedding");
588
+ scriptParams.embedding = update.vector;
589
+ }
590
+ if (update.metadata) {
591
+ scriptSource.push("ctx._source.metadata = params.metadata");
592
+ scriptParams.metadata = update.metadata;
593
+ }
594
+ await this.client.updateByQuery({
595
+ index: indexName,
596
+ body: {
597
+ query: translatedFilter || { match_all: {} },
598
+ script: {
599
+ source: scriptSource.join("; "),
600
+ params: scriptParams,
601
+ lang: "painless"
602
+ }
603
+ },
604
+ refresh: true
605
+ });
606
+ } catch (error) {
607
+ throw new _mastra_core_error.MastraError({
608
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "UPDATE_VECTOR_BY_FILTER", "FAILED"),
609
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
610
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
611
+ details: {
612
+ indexName,
613
+ filter: JSON.stringify(filter)
614
+ }
615
+ }, error);
616
+ }
617
+ }
618
+ /**
619
+ * Deletes a vector by its ID.
620
+ * @param indexName - The name of the index containing the vector.
621
+ * @param id - The ID of the vector to delete.
622
+ * @returns A promise that resolves when the deletion is complete.
623
+ * @throws Will throw an error if the deletion operation fails.
624
+ */
625
+ async deleteVector({ indexName, id }) {
626
+ try {
627
+ await this.client.delete({
628
+ index: indexName,
629
+ id,
630
+ refresh: true
631
+ });
632
+ } catch (error) {
633
+ if (error && typeof error === "object" && "statusCode" in error && error.statusCode === 404) return;
634
+ throw new _mastra_core_error.MastraError({
635
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "DELETE_VECTOR", "FAILED"),
636
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
637
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
638
+ details: {
639
+ indexName,
640
+ ...id && { id }
641
+ }
642
+ }, error);
643
+ }
644
+ }
645
+ async deleteVectors({ indexName, filter, ids }) {
646
+ if (ids && filter) throw new _mastra_core_error.MastraError({
647
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "DELETE_VECTORS", "MUTUALLY_EXCLUSIVE"),
648
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
649
+ category: _mastra_core_error.ErrorCategory.USER,
650
+ text: "ids and filter are mutually exclusive",
651
+ details: { indexName }
652
+ });
653
+ if (!ids && !filter) throw new _mastra_core_error.MastraError({
654
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "DELETE_VECTORS", "NO_TARGET"),
655
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
656
+ category: _mastra_core_error.ErrorCategory.USER,
657
+ text: "Either filter or ids must be provided",
658
+ details: { indexName }
659
+ });
660
+ if (ids && ids.length === 0) throw new _mastra_core_error.MastraError({
661
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "DELETE_VECTORS", "EMPTY_IDS"),
662
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
663
+ category: _mastra_core_error.ErrorCategory.USER,
664
+ text: "Cannot delete with empty ids array",
665
+ details: { indexName }
666
+ });
667
+ if (filter && Object.keys(filter).length === 0) throw new _mastra_core_error.MastraError({
668
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "DELETE_VECTORS", "EMPTY_FILTER"),
669
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
670
+ category: _mastra_core_error.ErrorCategory.USER,
671
+ text: "Cannot delete with empty filter",
672
+ details: { indexName }
673
+ });
674
+ try {
675
+ if (ids) {
676
+ const bulkBody = ids.flatMap((id) => [{ delete: {
677
+ _index: indexName,
678
+ _id: id
679
+ } }]);
680
+ await this.client.bulk({
681
+ body: bulkBody,
682
+ refresh: true
683
+ });
684
+ } else if (filter) {
685
+ const translatedFilter = new OpenSearchFilterTranslator().translate(filter);
686
+ await this.client.deleteByQuery({
687
+ index: indexName,
688
+ body: { query: translatedFilter || { match_all: {} } },
689
+ refresh: true
690
+ });
691
+ }
692
+ } catch (error) {
693
+ if (error instanceof _mastra_core_error.MastraError) throw error;
694
+ throw new _mastra_core_error.MastraError({
695
+ id: (0, _mastra_core_storage.createVectorErrorId)("OPENSEARCH", "DELETE_VECTORS", "FAILED"),
696
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
697
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
698
+ details: {
699
+ indexName,
700
+ ...filter && { filter: JSON.stringify(filter) },
701
+ ...ids && { idsCount: ids.length }
702
+ }
703
+ }, error);
704
+ }
705
+ }
909
706
  };
910
-
707
+ //#endregion
911
708
  exports.OpenSearchVector = OpenSearchVector;
912
- //# sourceMappingURL=index.cjs.map
709
+
913
710
  //# sourceMappingURL=index.cjs.map