@vectorstores/mongodb 0.1.5 → 0.1.7

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
@@ -71,7 +71,7 @@ class MongoKVStore extends core.BaseKVStore {
71
71
  }
72
72
  }
73
73
 
74
- var version = "0.1.5";
74
+ var version = "0.1.7";
75
75
  var pkg = {
76
76
  version: version};
77
77
 
@@ -123,7 +123,7 @@ function toMongoDBFilter(filters) {
123
123
  * This store uses the $vectorSearch aggregation stage to perform vector similarity search.
124
124
  */ class MongoDBAtlasVectorSearch extends core.BaseVectorStore {
125
125
  constructor(init){
126
- super(init), this.storesText = true, this.flatMetadata = true;
126
+ super(), this.storesText = true, this.flatMetadata = true;
127
127
  if (init.mongodbClient) {
128
128
  this.mongodbClient = init.mongodbClient;
129
129
  } else {
@@ -232,6 +232,26 @@ function toMongoDBFilter(filters) {
232
232
  * @param query The query to run
233
233
  * @returns List of nodes and their similarities
234
234
  */ async query(query, options) {
235
+ switch(query.mode){
236
+ case "bm25":
237
+ return this.bm25Search(query);
238
+ case "hybrid":
239
+ {
240
+ // Calculate prefetch limit for sub-searches
241
+ const prefetchK = query.hybridPrefetch ?? query.similarityTopK * core.DEFAULT_HYBRID_PREFETCH_MULTIPLIER;
242
+ const prefetchQuery = {
243
+ ...query,
244
+ similarityTopK: prefetchK
245
+ };
246
+ const vectorResult = await this.vectorSearch(prefetchQuery);
247
+ const bm25Result = await this.bm25Search(prefetchQuery);
248
+ return core.combineResults(vectorResult, bm25Result, query.alpha ?? 0.5, query.similarityTopK);
249
+ }
250
+ default:
251
+ return this.vectorSearch(query);
252
+ }
253
+ }
254
+ async vectorSearch(query) {
235
255
  const params = {
236
256
  queryVector: query.queryEmbedding,
237
257
  path: this.embeddingKey,
@@ -272,12 +292,59 @@ function toMongoDBFilter(filters) {
272
292
  nodes.push(node);
273
293
  similarities.push(score);
274
294
  }
275
- const result = {
295
+ return {
296
+ nodes,
297
+ similarities,
298
+ ids
299
+ };
300
+ }
301
+ async bm25Search(query) {
302
+ if (!query.queryStr) {
303
+ throw new Error("queryStr is required for BM25 mode");
304
+ }
305
+ const pipeline = [
306
+ {
307
+ $search: {
308
+ index: this.indexName,
309
+ text: {
310
+ query: query.queryStr,
311
+ path: this.textKey
312
+ }
313
+ }
314
+ },
315
+ {
316
+ $project: {
317
+ score: {
318
+ $meta: "searchScore"
319
+ },
320
+ [this.embeddingKey]: 0
321
+ }
322
+ },
323
+ {
324
+ $limit: query.similarityTopK
325
+ }
326
+ ];
327
+ const collection = await this.ensureCollection();
328
+ const cursor = await collection.aggregate(pipeline);
329
+ const nodes = [];
330
+ const ids = [];
331
+ const similarities = [];
332
+ for await (const res of (await cursor)){
333
+ const text = res[this.textKey];
334
+ const score = res.score;
335
+ const id = res[this.idKey];
336
+ const metadata = res[this.metadataKey];
337
+ const node = core.metadataDictToNode(metadata);
338
+ node.setContent(text);
339
+ ids.push(id);
340
+ nodes.push(node);
341
+ similarities.push(score);
342
+ }
343
+ return {
276
344
  nodes,
277
345
  similarities,
278
346
  ids
279
347
  };
280
- return result;
281
348
  }
282
349
  async exists(refDocId) {
283
350
  const collection = await this.ensureCollection();
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { BaseKVStore, StoredValue, BaseVectorStore, VectorStoreQuery, BaseEmbedding, VectorStoreBaseParams, BaseNode, VectorStoreQueryResult, BaseReader, Document } from '@vectorstores/core';
1
+ import { BaseKVStore, StoredValue, BaseVectorStore, VectorStoreQuery, BaseNode, VectorStoreQueryResult, BaseReader, Document } from '@vectorstores/core';
2
2
  import { MongoClient, BulkWriteOptions, Collection, Document as Document$1, Filter } from 'mongodb';
3
3
 
4
4
  interface MongoKVStoreConfig {
@@ -83,11 +83,10 @@ declare class MongoDBAtlasVectorSearch extends BaseVectorStore {
83
83
  constructor(init: Partial<MongoDBAtlasVectorSearch> & {
84
84
  dbName: string;
85
85
  collectionName: string;
86
- embedModel?: BaseEmbedding;
87
86
  autoCreateIndex?: boolean;
88
87
  indexedMetadataFields?: string[];
89
88
  embeddingDefinition?: Record<string, unknown>;
90
- } & VectorStoreBaseParams);
89
+ });
91
90
  ensureCollection(): Promise<Collection>;
92
91
  /**
93
92
  * Add nodes to the vector store.
@@ -111,6 +110,8 @@ declare class MongoDBAtlasVectorSearch extends BaseVectorStore {
111
110
  * @returns List of nodes and their similarities
112
111
  */
113
112
  query(query: VectorStoreQuery, options?: object): Promise<VectorStoreQueryResult>;
113
+ private vectorSearch;
114
+ private bm25Search;
114
115
  exists(refDocId: string): Promise<boolean>;
115
116
  }
116
117
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { BaseKVStore, StoredValue, BaseVectorStore, VectorStoreQuery, BaseEmbedding, VectorStoreBaseParams, BaseNode, VectorStoreQueryResult, BaseReader, Document } from '@vectorstores/core';
1
+ import { BaseKVStore, StoredValue, BaseVectorStore, VectorStoreQuery, BaseNode, VectorStoreQueryResult, BaseReader, Document } from '@vectorstores/core';
2
2
  import { MongoClient, BulkWriteOptions, Collection, Document as Document$1, Filter } from 'mongodb';
3
3
 
4
4
  interface MongoKVStoreConfig {
@@ -83,11 +83,10 @@ declare class MongoDBAtlasVectorSearch extends BaseVectorStore {
83
83
  constructor(init: Partial<MongoDBAtlasVectorSearch> & {
84
84
  dbName: string;
85
85
  collectionName: string;
86
- embedModel?: BaseEmbedding;
87
86
  autoCreateIndex?: boolean;
88
87
  indexedMetadataFields?: string[];
89
88
  embeddingDefinition?: Record<string, unknown>;
90
- } & VectorStoreBaseParams);
89
+ });
91
90
  ensureCollection(): Promise<Collection>;
92
91
  /**
93
92
  * Add nodes to the vector store.
@@ -111,6 +110,8 @@ declare class MongoDBAtlasVectorSearch extends BaseVectorStore {
111
110
  * @returns List of nodes and their similarities
112
111
  */
113
112
  query(query: VectorStoreQuery, options?: object): Promise<VectorStoreQueryResult>;
113
+ private vectorSearch;
114
+ private bm25Search;
114
115
  exists(refDocId: string): Promise<boolean>;
115
116
  }
116
117
 
@@ -1,4 +1,4 @@
1
- import { BaseKVStore, StoredValue, BaseVectorStore, VectorStoreQuery, BaseEmbedding, VectorStoreBaseParams, BaseNode, VectorStoreQueryResult, BaseReader, Document } from '@vectorstores/core';
1
+ import { BaseKVStore, StoredValue, BaseVectorStore, VectorStoreQuery, BaseNode, VectorStoreQueryResult, BaseReader, Document } from '@vectorstores/core';
2
2
  import { MongoClient, BulkWriteOptions, Collection, Document as Document$1, Filter } from 'mongodb';
3
3
 
4
4
  interface MongoKVStoreConfig {
@@ -83,11 +83,10 @@ declare class MongoDBAtlasVectorSearch extends BaseVectorStore {
83
83
  constructor(init: Partial<MongoDBAtlasVectorSearch> & {
84
84
  dbName: string;
85
85
  collectionName: string;
86
- embedModel?: BaseEmbedding;
87
86
  autoCreateIndex?: boolean;
88
87
  indexedMetadataFields?: string[];
89
88
  embeddingDefinition?: Record<string, unknown>;
90
- } & VectorStoreBaseParams);
89
+ });
91
90
  ensureCollection(): Promise<Collection>;
92
91
  /**
93
92
  * Add nodes to the vector store.
@@ -111,6 +110,8 @@ declare class MongoDBAtlasVectorSearch extends BaseVectorStore {
111
110
  * @returns List of nodes and their similarities
112
111
  */
113
112
  query(query: VectorStoreQuery, options?: object): Promise<VectorStoreQueryResult>;
113
+ private vectorSearch;
114
+ private bm25Search;
114
115
  exists(refDocId: string): Promise<boolean>;
115
116
  }
116
117
 
@@ -1,4 +1,4 @@
1
- import { BaseKVStore, BaseVectorStore, nodeToMetadata, MetadataMode, metadataDictToNode, FilterCondition, Document } from '@vectorstores/core';
1
+ import { BaseKVStore, BaseVectorStore, nodeToMetadata, MetadataMode, DEFAULT_HYBRID_PREFETCH_MULTIPLIER, combineResults, metadataDictToNode, FilterCondition, Document } from '@vectorstores/core';
2
2
  import { getEnv } from '@vectorstores/env';
3
3
  import { MongoClient } from 'mongodb';
4
4
 
@@ -69,7 +69,7 @@ class MongoKVStore extends BaseKVStore {
69
69
  }
70
70
  }
71
71
 
72
- var version = "0.1.5";
72
+ var version = "0.1.7";
73
73
  var pkg = {
74
74
  version: version};
75
75
 
@@ -121,7 +121,7 @@ function toMongoDBFilter(filters) {
121
121
  * This store uses the $vectorSearch aggregation stage to perform vector similarity search.
122
122
  */ class MongoDBAtlasVectorSearch extends BaseVectorStore {
123
123
  constructor(init){
124
- super(init), this.storesText = true, this.flatMetadata = true;
124
+ super(), this.storesText = true, this.flatMetadata = true;
125
125
  if (init.mongodbClient) {
126
126
  this.mongodbClient = init.mongodbClient;
127
127
  } else {
@@ -230,6 +230,26 @@ function toMongoDBFilter(filters) {
230
230
  * @param query The query to run
231
231
  * @returns List of nodes and their similarities
232
232
  */ async query(query, options) {
233
+ switch(query.mode){
234
+ case "bm25":
235
+ return this.bm25Search(query);
236
+ case "hybrid":
237
+ {
238
+ // Calculate prefetch limit for sub-searches
239
+ const prefetchK = query.hybridPrefetch ?? query.similarityTopK * DEFAULT_HYBRID_PREFETCH_MULTIPLIER;
240
+ const prefetchQuery = {
241
+ ...query,
242
+ similarityTopK: prefetchK
243
+ };
244
+ const vectorResult = await this.vectorSearch(prefetchQuery);
245
+ const bm25Result = await this.bm25Search(prefetchQuery);
246
+ return combineResults(vectorResult, bm25Result, query.alpha ?? 0.5, query.similarityTopK);
247
+ }
248
+ default:
249
+ return this.vectorSearch(query);
250
+ }
251
+ }
252
+ async vectorSearch(query) {
233
253
  const params = {
234
254
  queryVector: query.queryEmbedding,
235
255
  path: this.embeddingKey,
@@ -270,12 +290,59 @@ function toMongoDBFilter(filters) {
270
290
  nodes.push(node);
271
291
  similarities.push(score);
272
292
  }
273
- const result = {
293
+ return {
294
+ nodes,
295
+ similarities,
296
+ ids
297
+ };
298
+ }
299
+ async bm25Search(query) {
300
+ if (!query.queryStr) {
301
+ throw new Error("queryStr is required for BM25 mode");
302
+ }
303
+ const pipeline = [
304
+ {
305
+ $search: {
306
+ index: this.indexName,
307
+ text: {
308
+ query: query.queryStr,
309
+ path: this.textKey
310
+ }
311
+ }
312
+ },
313
+ {
314
+ $project: {
315
+ score: {
316
+ $meta: "searchScore"
317
+ },
318
+ [this.embeddingKey]: 0
319
+ }
320
+ },
321
+ {
322
+ $limit: query.similarityTopK
323
+ }
324
+ ];
325
+ const collection = await this.ensureCollection();
326
+ const cursor = await collection.aggregate(pipeline);
327
+ const nodes = [];
328
+ const ids = [];
329
+ const similarities = [];
330
+ for await (const res of (await cursor)){
331
+ const text = res[this.textKey];
332
+ const score = res.score;
333
+ const id = res[this.idKey];
334
+ const metadata = res[this.metadataKey];
335
+ const node = metadataDictToNode(metadata);
336
+ node.setContent(text);
337
+ ids.push(id);
338
+ nodes.push(node);
339
+ similarities.push(score);
340
+ }
341
+ return {
274
342
  nodes,
275
343
  similarities,
276
344
  ids
277
345
  };
278
- return result;
279
346
  }
280
347
  async exists(refDocId) {
281
348
  const collection = await this.ensureCollection();
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { BaseKVStore, BaseVectorStore, nodeToMetadata, MetadataMode, metadataDictToNode, FilterCondition, Document } from '@vectorstores/core';
1
+ import { BaseKVStore, BaseVectorStore, nodeToMetadata, MetadataMode, DEFAULT_HYBRID_PREFETCH_MULTIPLIER, combineResults, metadataDictToNode, FilterCondition, Document } from '@vectorstores/core';
2
2
  import { getEnv } from '@vectorstores/env';
3
3
  import { MongoClient } from 'mongodb';
4
4
 
@@ -69,7 +69,7 @@ class MongoKVStore extends BaseKVStore {
69
69
  }
70
70
  }
71
71
 
72
- var version = "0.1.5";
72
+ var version = "0.1.7";
73
73
  var pkg = {
74
74
  version: version};
75
75
 
@@ -121,7 +121,7 @@ function toMongoDBFilter(filters) {
121
121
  * This store uses the $vectorSearch aggregation stage to perform vector similarity search.
122
122
  */ class MongoDBAtlasVectorSearch extends BaseVectorStore {
123
123
  constructor(init){
124
- super(init), this.storesText = true, this.flatMetadata = true;
124
+ super(), this.storesText = true, this.flatMetadata = true;
125
125
  if (init.mongodbClient) {
126
126
  this.mongodbClient = init.mongodbClient;
127
127
  } else {
@@ -230,6 +230,26 @@ function toMongoDBFilter(filters) {
230
230
  * @param query The query to run
231
231
  * @returns List of nodes and their similarities
232
232
  */ async query(query, options) {
233
+ switch(query.mode){
234
+ case "bm25":
235
+ return this.bm25Search(query);
236
+ case "hybrid":
237
+ {
238
+ // Calculate prefetch limit for sub-searches
239
+ const prefetchK = query.hybridPrefetch ?? query.similarityTopK * DEFAULT_HYBRID_PREFETCH_MULTIPLIER;
240
+ const prefetchQuery = {
241
+ ...query,
242
+ similarityTopK: prefetchK
243
+ };
244
+ const vectorResult = await this.vectorSearch(prefetchQuery);
245
+ const bm25Result = await this.bm25Search(prefetchQuery);
246
+ return combineResults(vectorResult, bm25Result, query.alpha ?? 0.5, query.similarityTopK);
247
+ }
248
+ default:
249
+ return this.vectorSearch(query);
250
+ }
251
+ }
252
+ async vectorSearch(query) {
233
253
  const params = {
234
254
  queryVector: query.queryEmbedding,
235
255
  path: this.embeddingKey,
@@ -270,12 +290,59 @@ function toMongoDBFilter(filters) {
270
290
  nodes.push(node);
271
291
  similarities.push(score);
272
292
  }
273
- const result = {
293
+ return {
294
+ nodes,
295
+ similarities,
296
+ ids
297
+ };
298
+ }
299
+ async bm25Search(query) {
300
+ if (!query.queryStr) {
301
+ throw new Error("queryStr is required for BM25 mode");
302
+ }
303
+ const pipeline = [
304
+ {
305
+ $search: {
306
+ index: this.indexName,
307
+ text: {
308
+ query: query.queryStr,
309
+ path: this.textKey
310
+ }
311
+ }
312
+ },
313
+ {
314
+ $project: {
315
+ score: {
316
+ $meta: "searchScore"
317
+ },
318
+ [this.embeddingKey]: 0
319
+ }
320
+ },
321
+ {
322
+ $limit: query.similarityTopK
323
+ }
324
+ ];
325
+ const collection = await this.ensureCollection();
326
+ const cursor = await collection.aggregate(pipeline);
327
+ const nodes = [];
328
+ const ids = [];
329
+ const similarities = [];
330
+ for await (const res of (await cursor)){
331
+ const text = res[this.textKey];
332
+ const score = res.score;
333
+ const id = res[this.idKey];
334
+ const metadata = res[this.metadataKey];
335
+ const node = metadataDictToNode(metadata);
336
+ node.setContent(text);
337
+ ids.push(id);
338
+ nodes.push(node);
339
+ similarities.push(score);
340
+ }
341
+ return {
274
342
  nodes,
275
343
  similarities,
276
344
  ids
277
345
  };
278
- return result;
279
346
  }
280
347
  async exists(refDocId) {
281
348
  const collection = await this.ensureCollection();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vectorstores/mongodb",
3
3
  "description": "MongoDB Storage for vectorstores",
4
- "version": "0.1.5",
4
+ "version": "0.1.7",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",
@@ -36,11 +36,11 @@
36
36
  "devDependencies": {
37
37
  "vitest": "2.1.0",
38
38
  "mongodb-memory-server": "^10.1.4",
39
- "@vectorstores/core": "0.1.5",
39
+ "@vectorstores/core": "0.1.7",
40
40
  "@vectorstores/env": "0.1.0"
41
41
  },
42
42
  "peerDependencies": {
43
- "@vectorstores/core": "0.1.5",
43
+ "@vectorstores/core": "0.1.7",
44
44
  "@vectorstores/env": "0.1.0"
45
45
  },
46
46
  "dependencies": {