@vectorstores/elastic-search 0.1.5 → 0.1.6

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
@@ -209,26 +209,78 @@ function getElasticSearchClient({ esUrl, esCloudId, esApiKey, esUsername, esPass
209
209
  * @returns Query results containing matching nodes, similarities, and IDs
210
210
  * @throws Error if query embedding is not provided
211
211
  */ async query(query, options) {
212
- if (!query.queryEmbedding) {
213
- throw new Error("query embedding is not provided");
214
- }
215
212
  let elasticSearchFilter = [];
216
213
  if (query.filters) {
217
214
  elasticSearchFilter = [
218
215
  this.toElasticSearchFilter(query.filters)
219
216
  ];
220
217
  }
221
- const searchResponse = await this.elasticSearchClient.search({
218
+ const searchRequest = {
222
219
  index: this.indexName,
223
- size: query.similarityTopK,
224
- knn: {
225
- field: this.vectorField,
226
- query_vector: query.queryEmbedding,
227
- k: query.similarityTopK,
228
- num_candidates: query.similarityTopK * 10,
229
- filter: elasticSearchFilter
230
- }
231
- });
220
+ size: query.similarityTopK
221
+ };
222
+ switch(query.mode){
223
+ case "bm25":
224
+ if (!query.queryStr) {
225
+ throw new Error("queryStr is required for BM25 mode");
226
+ }
227
+ searchRequest.query = {
228
+ bool: {
229
+ must: [
230
+ {
231
+ match: {
232
+ [this.textField]: query.queryStr
233
+ }
234
+ },
235
+ ...elasticSearchFilter
236
+ ]
237
+ }
238
+ };
239
+ break;
240
+ case "hybrid":
241
+ if (!query.queryEmbedding) {
242
+ throw new Error("queryEmbedding is required for HYBRID mode");
243
+ }
244
+ if (!query.queryStr) {
245
+ throw new Error("queryStr is required for HYBRID mode");
246
+ }
247
+ searchRequest.knn = {
248
+ field: this.vectorField,
249
+ query_vector: query.queryEmbedding,
250
+ k: query.similarityTopK,
251
+ num_candidates: query.similarityTopK * 10,
252
+ filter: elasticSearchFilter,
253
+ boost: query.alpha ?? 0.5
254
+ };
255
+ searchRequest.query = {
256
+ bool: {
257
+ must: [
258
+ {
259
+ match: {
260
+ [this.textField]: {
261
+ query: query.queryStr,
262
+ boost: 1 - (query.alpha ?? 0.5)
263
+ }
264
+ }
265
+ },
266
+ ...elasticSearchFilter
267
+ ]
268
+ }
269
+ };
270
+ break;
271
+ default:
272
+ if (!query.queryEmbedding) {
273
+ throw new Error("query embedding is not provided");
274
+ }
275
+ searchRequest.knn = {
276
+ field: this.vectorField,
277
+ query_vector: query.queryEmbedding,
278
+ k: query.similarityTopK,
279
+ num_candidates: query.similarityTopK * 10,
280
+ filter: elasticSearchFilter
281
+ };
282
+ }
283
+ const searchResponse = await this.elasticSearchClient.search(searchRequest);
232
284
  return this.getVectorSearchQueryResultFromResponse(searchResponse);
233
285
  }
234
286
  /**
@@ -207,26 +207,78 @@ function getElasticSearchClient({ esUrl, esCloudId, esApiKey, esUsername, esPass
207
207
  * @returns Query results containing matching nodes, similarities, and IDs
208
208
  * @throws Error if query embedding is not provided
209
209
  */ async query(query, options) {
210
- if (!query.queryEmbedding) {
211
- throw new Error("query embedding is not provided");
212
- }
213
210
  let elasticSearchFilter = [];
214
211
  if (query.filters) {
215
212
  elasticSearchFilter = [
216
213
  this.toElasticSearchFilter(query.filters)
217
214
  ];
218
215
  }
219
- const searchResponse = await this.elasticSearchClient.search({
216
+ const searchRequest = {
220
217
  index: this.indexName,
221
- size: query.similarityTopK,
222
- knn: {
223
- field: this.vectorField,
224
- query_vector: query.queryEmbedding,
225
- k: query.similarityTopK,
226
- num_candidates: query.similarityTopK * 10,
227
- filter: elasticSearchFilter
228
- }
229
- });
218
+ size: query.similarityTopK
219
+ };
220
+ switch(query.mode){
221
+ case "bm25":
222
+ if (!query.queryStr) {
223
+ throw new Error("queryStr is required for BM25 mode");
224
+ }
225
+ searchRequest.query = {
226
+ bool: {
227
+ must: [
228
+ {
229
+ match: {
230
+ [this.textField]: query.queryStr
231
+ }
232
+ },
233
+ ...elasticSearchFilter
234
+ ]
235
+ }
236
+ };
237
+ break;
238
+ case "hybrid":
239
+ if (!query.queryEmbedding) {
240
+ throw new Error("queryEmbedding is required for HYBRID mode");
241
+ }
242
+ if (!query.queryStr) {
243
+ throw new Error("queryStr is required for HYBRID mode");
244
+ }
245
+ searchRequest.knn = {
246
+ field: this.vectorField,
247
+ query_vector: query.queryEmbedding,
248
+ k: query.similarityTopK,
249
+ num_candidates: query.similarityTopK * 10,
250
+ filter: elasticSearchFilter,
251
+ boost: query.alpha ?? 0.5
252
+ };
253
+ searchRequest.query = {
254
+ bool: {
255
+ must: [
256
+ {
257
+ match: {
258
+ [this.textField]: {
259
+ query: query.queryStr,
260
+ boost: 1 - (query.alpha ?? 0.5)
261
+ }
262
+ }
263
+ },
264
+ ...elasticSearchFilter
265
+ ]
266
+ }
267
+ };
268
+ break;
269
+ default:
270
+ if (!query.queryEmbedding) {
271
+ throw new Error("query embedding is not provided");
272
+ }
273
+ searchRequest.knn = {
274
+ field: this.vectorField,
275
+ query_vector: query.queryEmbedding,
276
+ k: query.similarityTopK,
277
+ num_candidates: query.similarityTopK * 10,
278
+ filter: elasticSearchFilter
279
+ };
280
+ }
281
+ const searchResponse = await this.elasticSearchClient.search(searchRequest);
230
282
  return this.getVectorSearchQueryResultFromResponse(searchResponse);
231
283
  }
232
284
  /**
package/dist/index.js CHANGED
@@ -207,26 +207,78 @@ function getElasticSearchClient({ esUrl, esCloudId, esApiKey, esUsername, esPass
207
207
  * @returns Query results containing matching nodes, similarities, and IDs
208
208
  * @throws Error if query embedding is not provided
209
209
  */ async query(query, options) {
210
- if (!query.queryEmbedding) {
211
- throw new Error("query embedding is not provided");
212
- }
213
210
  let elasticSearchFilter = [];
214
211
  if (query.filters) {
215
212
  elasticSearchFilter = [
216
213
  this.toElasticSearchFilter(query.filters)
217
214
  ];
218
215
  }
219
- const searchResponse = await this.elasticSearchClient.search({
216
+ const searchRequest = {
220
217
  index: this.indexName,
221
- size: query.similarityTopK,
222
- knn: {
223
- field: this.vectorField,
224
- query_vector: query.queryEmbedding,
225
- k: query.similarityTopK,
226
- num_candidates: query.similarityTopK * 10,
227
- filter: elasticSearchFilter
228
- }
229
- });
218
+ size: query.similarityTopK
219
+ };
220
+ switch(query.mode){
221
+ case "bm25":
222
+ if (!query.queryStr) {
223
+ throw new Error("queryStr is required for BM25 mode");
224
+ }
225
+ searchRequest.query = {
226
+ bool: {
227
+ must: [
228
+ {
229
+ match: {
230
+ [this.textField]: query.queryStr
231
+ }
232
+ },
233
+ ...elasticSearchFilter
234
+ ]
235
+ }
236
+ };
237
+ break;
238
+ case "hybrid":
239
+ if (!query.queryEmbedding) {
240
+ throw new Error("queryEmbedding is required for HYBRID mode");
241
+ }
242
+ if (!query.queryStr) {
243
+ throw new Error("queryStr is required for HYBRID mode");
244
+ }
245
+ searchRequest.knn = {
246
+ field: this.vectorField,
247
+ query_vector: query.queryEmbedding,
248
+ k: query.similarityTopK,
249
+ num_candidates: query.similarityTopK * 10,
250
+ filter: elasticSearchFilter,
251
+ boost: query.alpha ?? 0.5
252
+ };
253
+ searchRequest.query = {
254
+ bool: {
255
+ must: [
256
+ {
257
+ match: {
258
+ [this.textField]: {
259
+ query: query.queryStr,
260
+ boost: 1 - (query.alpha ?? 0.5)
261
+ }
262
+ }
263
+ },
264
+ ...elasticSearchFilter
265
+ ]
266
+ }
267
+ };
268
+ break;
269
+ default:
270
+ if (!query.queryEmbedding) {
271
+ throw new Error("query embedding is not provided");
272
+ }
273
+ searchRequest.knn = {
274
+ field: this.vectorField,
275
+ query_vector: query.queryEmbedding,
276
+ k: query.similarityTopK,
277
+ num_candidates: query.similarityTopK * 10,
278
+ filter: elasticSearchFilter
279
+ };
280
+ }
281
+ const searchResponse = await this.elasticSearchClient.search(searchRequest);
230
282
  return this.getVectorSearchQueryResultFromResponse(searchResponse);
231
283
  }
232
284
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vectorstores/elastic-search",
3
3
  "description": "Elastic Search Storage for vectorstores",
4
- "version": "0.1.5",
4
+ "version": "0.1.6",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",
@@ -35,11 +35,11 @@
35
35
  },
36
36
  "devDependencies": {
37
37
  "vitest": "^3.0.9",
38
- "@vectorstores/core": "0.1.5",
38
+ "@vectorstores/core": "0.1.6",
39
39
  "@vectorstores/env": "0.1.0"
40
40
  },
41
41
  "peerDependencies": {
42
- "@vectorstores/core": "0.1.5",
42
+ "@vectorstores/core": "0.1.6",
43
43
  "@vectorstores/env": "0.1.0"
44
44
  },
45
45
  "dependencies": {