@vectorstores/azure 0.1.3 → 0.1.5

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
@@ -2,8 +2,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
2
2
 
3
3
  var core = require('@vectorstores/core');
4
4
  var storage_cjs = require('./storage.cjs');
5
- var searchDocuments = require('@azure/search-documents');
6
5
  var identity = require('@azure/identity');
6
+ var searchDocuments = require('@azure/search-documents');
7
7
  var env = require('@vectorstores/env');
8
8
  var mongodb = require('mongodb');
9
9
  var cosmos = require('@azure/cosmos');
@@ -365,11 +365,10 @@ const vectorStore = new AzureAISearchVectorStore({
365
365
  const documents = await new SimpleDirectoryReader().loadData(
366
366
  "data/paul_graham/",
367
367
  );
368
- const storageContext = await storageContextFromDefaults({ vectorStore });
369
368
 
370
- // Create index from documents with the specified storage context
369
+ // Create index from documents with the specified vector store
371
370
  const index = await VectorStoreIndex.fromDocuments(documents, {
372
- storageContext,
371
+ vectorStore,
373
372
  docStoreStrategy: DocStoreStrategy.UPSERTS,
374
373
  });
375
374
 
@@ -789,7 +788,7 @@ console.log({ response });
789
788
  }
790
789
  }
791
790
  #buildCredentials(options) {
792
- let { credential: credential, key, endpoint, indexName } = options;
791
+ let { credential, key, endpoint, indexName } = options;
793
792
  // validate and use credential
794
793
  if (credential) {
795
794
  // if credential are provided, ensure they are an instance of valid credential instances
@@ -1121,9 +1120,19 @@ console.log({ response });
1121
1120
  // Execute the search and return the result
1122
1121
  return await azureQueryResultSearch.search();
1123
1122
  }
1123
+ async exists(refDocId) {
1124
+ const results = await this._searchClient.search("*", {
1125
+ filter: `ref_doc_id eq '${refDocId}'`,
1126
+ top: 1
1127
+ });
1128
+ for await (const _result of results.results){
1129
+ return true;
1130
+ }
1131
+ return false;
1132
+ }
1124
1133
  }
1125
1134
 
1126
- var version = "0.1.3";
1135
+ var version = "0.1.5";
1127
1136
  var pkg = {
1128
1137
  version: version};
1129
1138
 
@@ -1206,13 +1215,14 @@ var pkg = {
1206
1215
  return Object.values(insertResult.insertedIds).map((id)=>String(id));
1207
1216
  }
1208
1217
  /**
1209
- * Removes specified documents from the AzureCosmosDBMongoDBVectorStore.
1210
- * @param params Parameters for the delete operation.
1218
+ * Deletes all nodes from the collection that belong to the given document.
1219
+ * @param refDocId Reference document ID - all nodes with this ref_doc_id will be deleted.
1220
+ * @param deleteOptions Additional delete options.
1211
1221
  * @returns A promise that resolves when the documents have been removed.
1212
- */ async delete(id, deleteOptions) {
1222
+ */ async delete(refDocId, deleteOptions) {
1213
1223
  const collection = await this.ensureCollection();
1214
1224
  await collection.deleteMany({
1215
- id: id
1225
+ [`${this.metadataKey}.ref_doc_id`]: refDocId
1216
1226
  }, deleteOptions);
1217
1227
  }
1218
1228
  async query(query, options) {
@@ -1350,6 +1360,15 @@ var pkg = {
1350
1360
  await collection.dropIndex(indexName);
1351
1361
  }
1352
1362
  }
1363
+ async exists(refDocId) {
1364
+ const collection = await this.ensureCollection();
1365
+ const count = await collection.countDocuments({
1366
+ [`${this.metadataKey}.ref_doc_id`]: refDocId
1367
+ }, {
1368
+ limit: 1
1369
+ });
1370
+ return count > 0;
1371
+ }
1353
1372
  }
1354
1373
 
1355
1374
  const USER_AGENT_SUFFIX = "vectorstores-cdbnosql-vectorstore-javascript";
@@ -1532,14 +1551,28 @@ class AzureCosmosDBNoSqlVectorStore extends core.BaseVectorStore {
1532
1551
  return ids;
1533
1552
  }
1534
1553
  /**
1535
- * Delete a document from the CosmosDB container.
1554
+ * Delete all nodes from the CosmosDB container that belong to the given document.
1536
1555
  *
1537
- * @param refDocId - The id of the document to delete
1538
- * @param deleteOptions - Any options to pass to the container.item.delete function
1556
+ * @param refDocId - Reference document ID - all nodes with this ref_doc_id will be deleted.
1557
+ * @param deleteOptions - Any options to pass to the delete operations.
1539
1558
  * @returns Promise that resolves if the delete query did not throw an error.
1540
1559
  */ async delete(refDocId, deleteOptions) {
1541
1560
  await this.initialize();
1542
- await this.container.item(refDocId).delete(deleteOptions);
1561
+ // Query all items with matching ref_doc_id
1562
+ const querySpec = {
1563
+ query: `SELECT c.id FROM c WHERE c.metadata.ref_doc_id = @refDocId`,
1564
+ parameters: [
1565
+ {
1566
+ name: "@refDocId",
1567
+ value: refDocId
1568
+ }
1569
+ ]
1570
+ };
1571
+ const { resources: items } = await this.container.items.query(querySpec).fetchAll();
1572
+ // Delete each matching item
1573
+ for (const item of items){
1574
+ await this.container.item(item.id, item.id).delete(deleteOptions);
1575
+ }
1543
1576
  }
1544
1577
  /**
1545
1578
  * Performs a vector similarity search query in the CosmosDB container.
@@ -1633,6 +1666,19 @@ class AzureCosmosDBNoSqlVectorStore extends core.BaseVectorStore {
1633
1666
  });
1634
1667
  this.container = container;
1635
1668
  }
1669
+ async exists(refDocId) {
1670
+ await this.initialize();
1671
+ const { resources } = await this.container.items.query({
1672
+ query: `SELECT VALUE COUNT(1) FROM c WHERE c.metadata.ref_doc_id = @refDocId`,
1673
+ parameters: [
1674
+ {
1675
+ name: "@refDocId",
1676
+ value: refDocId
1677
+ }
1678
+ ]
1679
+ }).fetchAll();
1680
+ return (resources[0] ?? 0) > 0;
1681
+ }
1636
1682
  }
1637
1683
 
1638
1684
  exports.AzureAISearchVectorStore = AzureAISearchVectorStore;
package/dist/index.d.cts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { SqlQuerySpec, CosmosClient, DatabaseRequest, ContainerRequest, VectorEmbeddingPolicy, IndexingPolicy } from '@azure/cosmos';
2
2
  import { BaseReader, Document, BaseNode, BaseVectorStore, VectorStoreBaseParams, MetadataFilters, VectorStoreQuery, VectorStoreQueryResult } from '@vectorstores/core';
3
3
  export * from './storage.cjs';
4
- import { AzureKeyCredential, SearchIndexClient, SearchClient, LexicalAnalyzerName, KnownVectorSearchCompressionKind, KnownVectorSearchAlgorithmKind, VectorizedQuery } from '@azure/search-documents';
5
4
  import { DefaultAzureCredential, ManagedIdentityCredential, TokenCredential } from '@azure/identity';
5
+ import { AzureKeyCredential, SearchIndexClient, SearchClient, LexicalAnalyzerName, KnownVectorSearchCompressionKind, KnownVectorSearchAlgorithmKind, VectorizedQuery } from '@azure/search-documents';
6
6
  import { MongoClient, Collection } from 'mongodb';
7
7
 
8
8
  type SimpleCosmosDBReaderLoaderConfig = {
@@ -247,11 +247,10 @@ const vectorStore = new AzureAISearchVectorStore({
247
247
  const documents = await new SimpleDirectoryReader().loadData(
248
248
  "data/paul_graham/",
249
249
  );
250
- const storageContext = await storageContextFromDefaults({ vectorStore });
251
250
 
252
- // Create index from documents with the specified storage context
251
+ // Create index from documents with the specified vector store
253
252
  const index = await VectorStoreIndex.fromDocuments(documents, {
254
- storageContext,
253
+ vectorStore,
255
254
  docStoreStrategy: DocStoreStrategy.UPSERTS,
256
255
  });
257
256
 
@@ -303,6 +302,7 @@ declare class AzureAISearchVectorStore<T extends R> extends BaseVectorStore {
303
302
  query(query: VectorStoreQuery & {
304
303
  queryStr: string;
305
304
  }): Promise<VectorStoreQueryResult>;
305
+ exists(refDocId: string): Promise<boolean>;
306
306
  }
307
307
 
308
308
  /** Azure Cosmos DB for MongoDB vCore Similarity type. */
@@ -388,11 +388,12 @@ declare class AzureCosmosDBMongoDBVectorStore extends BaseVectorStore {
388
388
  ensureCollection(): Promise<Collection>;
389
389
  add(nodes: BaseNode[]): Promise<string[]>;
390
390
  /**
391
- * Removes specified documents from the AzureCosmosDBMongoDBVectorStore.
392
- * @param params Parameters for the delete operation.
391
+ * Deletes all nodes from the collection that belong to the given document.
392
+ * @param refDocId Reference document ID - all nodes with this ref_doc_id will be deleted.
393
+ * @param deleteOptions Additional delete options.
393
394
  * @returns A promise that resolves when the documents have been removed.
394
395
  */
395
- delete(id: string, deleteOptions?: object): Promise<void>;
396
+ delete(refDocId: string, deleteOptions?: object): Promise<void>;
396
397
  query(query: VectorStoreQuery, options: AzureCosmosDBMongoDBQueryOptions): Promise<VectorStoreQueryResult>;
397
398
  /**
398
399
  * Creates an index on the collection with the specified index name during
@@ -435,6 +436,7 @@ declare class AzureCosmosDBMongoDBVectorStore extends BaseVectorStore {
435
436
  * @returns A promise that resolves when the index has been deleted.
436
437
  */
437
438
  deleteIndex(indexName: string): Promise<void>;
439
+ exists(refDocId: string): Promise<boolean>;
438
440
  }
439
441
 
440
442
  /** Azure Cosmos DB for NoSQL database creation options. */
@@ -534,10 +536,10 @@ declare class AzureCosmosDBNoSqlVectorStore extends BaseVectorStore {
534
536
  */
535
537
  add(nodes: BaseNode[]): Promise<string[]>;
536
538
  /**
537
- * Delete a document from the CosmosDB container.
539
+ * Delete all nodes from the CosmosDB container that belong to the given document.
538
540
  *
539
- * @param refDocId - The id of the document to delete
540
- * @param deleteOptions - Any options to pass to the container.item.delete function
541
+ * @param refDocId - Reference document ID - all nodes with this ref_doc_id will be deleted.
542
+ * @param deleteOptions - Any options to pass to the delete operations.
541
543
  * @returns Promise that resolves if the delete query did not throw an error.
542
544
  */
543
545
  delete(refDocId: string, deleteOptions?: object): Promise<void>;
@@ -552,6 +554,7 @@ declare class AzureCosmosDBNoSqlVectorStore extends BaseVectorStore {
552
554
  * Initialize the CosmosDB container.
553
555
  */
554
556
  private init;
557
+ exists(refDocId: string): Promise<boolean>;
555
558
  }
556
559
 
557
560
  declare class AzureQueryResultSearchBase<T extends R> {
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { SqlQuerySpec, CosmosClient, DatabaseRequest, ContainerRequest, VectorEmbeddingPolicy, IndexingPolicy } from '@azure/cosmos';
2
2
  import { BaseReader, Document, BaseNode, BaseVectorStore, VectorStoreBaseParams, MetadataFilters, VectorStoreQuery, VectorStoreQueryResult } from '@vectorstores/core';
3
3
  export * from './storage.js';
4
- import { AzureKeyCredential, SearchIndexClient, SearchClient, LexicalAnalyzerName, KnownVectorSearchCompressionKind, KnownVectorSearchAlgorithmKind, VectorizedQuery } from '@azure/search-documents';
5
4
  import { DefaultAzureCredential, ManagedIdentityCredential, TokenCredential } from '@azure/identity';
5
+ import { AzureKeyCredential, SearchIndexClient, SearchClient, LexicalAnalyzerName, KnownVectorSearchCompressionKind, KnownVectorSearchAlgorithmKind, VectorizedQuery } from '@azure/search-documents';
6
6
  import { MongoClient, Collection } from 'mongodb';
7
7
 
8
8
  type SimpleCosmosDBReaderLoaderConfig = {
@@ -247,11 +247,10 @@ const vectorStore = new AzureAISearchVectorStore({
247
247
  const documents = await new SimpleDirectoryReader().loadData(
248
248
  "data/paul_graham/",
249
249
  );
250
- const storageContext = await storageContextFromDefaults({ vectorStore });
251
250
 
252
- // Create index from documents with the specified storage context
251
+ // Create index from documents with the specified vector store
253
252
  const index = await VectorStoreIndex.fromDocuments(documents, {
254
- storageContext,
253
+ vectorStore,
255
254
  docStoreStrategy: DocStoreStrategy.UPSERTS,
256
255
  });
257
256
 
@@ -303,6 +302,7 @@ declare class AzureAISearchVectorStore<T extends R> extends BaseVectorStore {
303
302
  query(query: VectorStoreQuery & {
304
303
  queryStr: string;
305
304
  }): Promise<VectorStoreQueryResult>;
305
+ exists(refDocId: string): Promise<boolean>;
306
306
  }
307
307
 
308
308
  /** Azure Cosmos DB for MongoDB vCore Similarity type. */
@@ -388,11 +388,12 @@ declare class AzureCosmosDBMongoDBVectorStore extends BaseVectorStore {
388
388
  ensureCollection(): Promise<Collection>;
389
389
  add(nodes: BaseNode[]): Promise<string[]>;
390
390
  /**
391
- * Removes specified documents from the AzureCosmosDBMongoDBVectorStore.
392
- * @param params Parameters for the delete operation.
391
+ * Deletes all nodes from the collection that belong to the given document.
392
+ * @param refDocId Reference document ID - all nodes with this ref_doc_id will be deleted.
393
+ * @param deleteOptions Additional delete options.
393
394
  * @returns A promise that resolves when the documents have been removed.
394
395
  */
395
- delete(id: string, deleteOptions?: object): Promise<void>;
396
+ delete(refDocId: string, deleteOptions?: object): Promise<void>;
396
397
  query(query: VectorStoreQuery, options: AzureCosmosDBMongoDBQueryOptions): Promise<VectorStoreQueryResult>;
397
398
  /**
398
399
  * Creates an index on the collection with the specified index name during
@@ -435,6 +436,7 @@ declare class AzureCosmosDBMongoDBVectorStore extends BaseVectorStore {
435
436
  * @returns A promise that resolves when the index has been deleted.
436
437
  */
437
438
  deleteIndex(indexName: string): Promise<void>;
439
+ exists(refDocId: string): Promise<boolean>;
438
440
  }
439
441
 
440
442
  /** Azure Cosmos DB for NoSQL database creation options. */
@@ -534,10 +536,10 @@ declare class AzureCosmosDBNoSqlVectorStore extends BaseVectorStore {
534
536
  */
535
537
  add(nodes: BaseNode[]): Promise<string[]>;
536
538
  /**
537
- * Delete a document from the CosmosDB container.
539
+ * Delete all nodes from the CosmosDB container that belong to the given document.
538
540
  *
539
- * @param refDocId - The id of the document to delete
540
- * @param deleteOptions - Any options to pass to the container.item.delete function
541
+ * @param refDocId - Reference document ID - all nodes with this ref_doc_id will be deleted.
542
+ * @param deleteOptions - Any options to pass to the delete operations.
541
543
  * @returns Promise that resolves if the delete query did not throw an error.
542
544
  */
543
545
  delete(refDocId: string, deleteOptions?: object): Promise<void>;
@@ -552,6 +554,7 @@ declare class AzureCosmosDBNoSqlVectorStore extends BaseVectorStore {
552
554
  * Initialize the CosmosDB container.
553
555
  */
554
556
  private init;
557
+ exists(refDocId: string): Promise<boolean>;
555
558
  }
556
559
 
557
560
  declare class AzureQueryResultSearchBase<T extends R> {
@@ -1,8 +1,8 @@
1
1
  import { SqlQuerySpec, CosmosClient, DatabaseRequest, ContainerRequest, VectorEmbeddingPolicy, IndexingPolicy } from '@azure/cosmos';
2
2
  import { BaseReader, Document, BaseNode, BaseVectorStore, VectorStoreBaseParams, MetadataFilters, VectorStoreQuery, VectorStoreQueryResult } from '@vectorstores/core';
3
3
  export * from './storage.js';
4
- import { AzureKeyCredential, SearchIndexClient, SearchClient, LexicalAnalyzerName, KnownVectorSearchCompressionKind, KnownVectorSearchAlgorithmKind, VectorizedQuery } from '@azure/search-documents';
5
4
  import { DefaultAzureCredential, ManagedIdentityCredential, TokenCredential } from '@azure/identity';
5
+ import { AzureKeyCredential, SearchIndexClient, SearchClient, LexicalAnalyzerName, KnownVectorSearchCompressionKind, KnownVectorSearchAlgorithmKind, VectorizedQuery } from '@azure/search-documents';
6
6
  import { MongoClient, Collection } from 'mongodb';
7
7
 
8
8
  type SimpleCosmosDBReaderLoaderConfig = {
@@ -247,11 +247,10 @@ const vectorStore = new AzureAISearchVectorStore({
247
247
  const documents = await new SimpleDirectoryReader().loadData(
248
248
  "data/paul_graham/",
249
249
  );
250
- const storageContext = await storageContextFromDefaults({ vectorStore });
251
250
 
252
- // Create index from documents with the specified storage context
251
+ // Create index from documents with the specified vector store
253
252
  const index = await VectorStoreIndex.fromDocuments(documents, {
254
- storageContext,
253
+ vectorStore,
255
254
  docStoreStrategy: DocStoreStrategy.UPSERTS,
256
255
  });
257
256
 
@@ -303,6 +302,7 @@ declare class AzureAISearchVectorStore<T extends R> extends BaseVectorStore {
303
302
  query(query: VectorStoreQuery & {
304
303
  queryStr: string;
305
304
  }): Promise<VectorStoreQueryResult>;
305
+ exists(refDocId: string): Promise<boolean>;
306
306
  }
307
307
 
308
308
  /** Azure Cosmos DB for MongoDB vCore Similarity type. */
@@ -388,11 +388,12 @@ declare class AzureCosmosDBMongoDBVectorStore extends BaseVectorStore {
388
388
  ensureCollection(): Promise<Collection>;
389
389
  add(nodes: BaseNode[]): Promise<string[]>;
390
390
  /**
391
- * Removes specified documents from the AzureCosmosDBMongoDBVectorStore.
392
- * @param params Parameters for the delete operation.
391
+ * Deletes all nodes from the collection that belong to the given document.
392
+ * @param refDocId Reference document ID - all nodes with this ref_doc_id will be deleted.
393
+ * @param deleteOptions Additional delete options.
393
394
  * @returns A promise that resolves when the documents have been removed.
394
395
  */
395
- delete(id: string, deleteOptions?: object): Promise<void>;
396
+ delete(refDocId: string, deleteOptions?: object): Promise<void>;
396
397
  query(query: VectorStoreQuery, options: AzureCosmosDBMongoDBQueryOptions): Promise<VectorStoreQueryResult>;
397
398
  /**
398
399
  * Creates an index on the collection with the specified index name during
@@ -435,6 +436,7 @@ declare class AzureCosmosDBMongoDBVectorStore extends BaseVectorStore {
435
436
  * @returns A promise that resolves when the index has been deleted.
436
437
  */
437
438
  deleteIndex(indexName: string): Promise<void>;
439
+ exists(refDocId: string): Promise<boolean>;
438
440
  }
439
441
 
440
442
  /** Azure Cosmos DB for NoSQL database creation options. */
@@ -534,10 +536,10 @@ declare class AzureCosmosDBNoSqlVectorStore extends BaseVectorStore {
534
536
  */
535
537
  add(nodes: BaseNode[]): Promise<string[]>;
536
538
  /**
537
- * Delete a document from the CosmosDB container.
539
+ * Delete all nodes from the CosmosDB container that belong to the given document.
538
540
  *
539
- * @param refDocId - The id of the document to delete
540
- * @param deleteOptions - Any options to pass to the container.item.delete function
541
+ * @param refDocId - Reference document ID - all nodes with this ref_doc_id will be deleted.
542
+ * @param deleteOptions - Any options to pass to the delete operations.
541
543
  * @returns Promise that resolves if the delete query did not throw an error.
542
544
  */
543
545
  delete(refDocId: string, deleteOptions?: object): Promise<void>;
@@ -552,6 +554,7 @@ declare class AzureCosmosDBNoSqlVectorStore extends BaseVectorStore {
552
554
  * Initialize the CosmosDB container.
553
555
  */
554
556
  private init;
557
+ exists(refDocId: string): Promise<boolean>;
555
558
  }
556
559
 
557
560
  declare class AzureQueryResultSearchBase<T extends R> {
@@ -1,7 +1,7 @@
1
1
  import { Document, metadataDictToNode, BaseVectorStore, MetadataMode, nodeToMetadata, FilterOperator, FilterCondition, VectorStoreQueryMode } from '@vectorstores/core';
2
2
  export * from './storage.edge-light.js';
3
- import { KnownVectorSearchAlgorithmKind, KnownAnalyzerNames, KnownSearchFieldDataType, KnownVectorSearchAlgorithmMetric, KnownVectorSearchCompressionKind, SearchClient, SearchIndexClient, AzureKeyCredential, IndexDocumentsBatch } from '@azure/search-documents';
4
3
  import { DefaultAzureCredential, ManagedIdentityCredential } from '@azure/identity';
4
+ import { KnownVectorSearchAlgorithmKind, KnownAnalyzerNames, KnownSearchFieldDataType, KnownVectorSearchAlgorithmMetric, KnownVectorSearchCompressionKind, SearchClient, SearchIndexClient, AzureKeyCredential, IndexDocumentsBatch } from '@azure/search-documents';
5
5
  import { consoleLogger, getEnv } from '@vectorstores/env';
6
6
  import { MongoClient } from 'mongodb';
7
7
  import { VectorEmbeddingDistanceFunction, VectorEmbeddingDataType, VectorIndexType, CosmosClient } from '@azure/cosmos';
@@ -363,11 +363,10 @@ const vectorStore = new AzureAISearchVectorStore({
363
363
  const documents = await new SimpleDirectoryReader().loadData(
364
364
  "data/paul_graham/",
365
365
  );
366
- const storageContext = await storageContextFromDefaults({ vectorStore });
367
366
 
368
- // Create index from documents with the specified storage context
367
+ // Create index from documents with the specified vector store
369
368
  const index = await VectorStoreIndex.fromDocuments(documents, {
370
- storageContext,
369
+ vectorStore,
371
370
  docStoreStrategy: DocStoreStrategy.UPSERTS,
372
371
  });
373
372
 
@@ -787,7 +786,7 @@ console.log({ response });
787
786
  }
788
787
  }
789
788
  #buildCredentials(options) {
790
- let { credential: credential, key, endpoint, indexName } = options;
789
+ let { credential, key, endpoint, indexName } = options;
791
790
  // validate and use credential
792
791
  if (credential) {
793
792
  // if credential are provided, ensure they are an instance of valid credential instances
@@ -1119,9 +1118,19 @@ console.log({ response });
1119
1118
  // Execute the search and return the result
1120
1119
  return await azureQueryResultSearch.search();
1121
1120
  }
1121
+ async exists(refDocId) {
1122
+ const results = await this._searchClient.search("*", {
1123
+ filter: `ref_doc_id eq '${refDocId}'`,
1124
+ top: 1
1125
+ });
1126
+ for await (const _result of results.results){
1127
+ return true;
1128
+ }
1129
+ return false;
1130
+ }
1122
1131
  }
1123
1132
 
1124
- var version = "0.1.3";
1133
+ var version = "0.1.5";
1125
1134
  var pkg = {
1126
1135
  version: version};
1127
1136
 
@@ -1204,13 +1213,14 @@ var pkg = {
1204
1213
  return Object.values(insertResult.insertedIds).map((id)=>String(id));
1205
1214
  }
1206
1215
  /**
1207
- * Removes specified documents from the AzureCosmosDBMongoDBVectorStore.
1208
- * @param params Parameters for the delete operation.
1216
+ * Deletes all nodes from the collection that belong to the given document.
1217
+ * @param refDocId Reference document ID - all nodes with this ref_doc_id will be deleted.
1218
+ * @param deleteOptions Additional delete options.
1209
1219
  * @returns A promise that resolves when the documents have been removed.
1210
- */ async delete(id, deleteOptions) {
1220
+ */ async delete(refDocId, deleteOptions) {
1211
1221
  const collection = await this.ensureCollection();
1212
1222
  await collection.deleteMany({
1213
- id: id
1223
+ [`${this.metadataKey}.ref_doc_id`]: refDocId
1214
1224
  }, deleteOptions);
1215
1225
  }
1216
1226
  async query(query, options) {
@@ -1348,6 +1358,15 @@ var pkg = {
1348
1358
  await collection.dropIndex(indexName);
1349
1359
  }
1350
1360
  }
1361
+ async exists(refDocId) {
1362
+ const collection = await this.ensureCollection();
1363
+ const count = await collection.countDocuments({
1364
+ [`${this.metadataKey}.ref_doc_id`]: refDocId
1365
+ }, {
1366
+ limit: 1
1367
+ });
1368
+ return count > 0;
1369
+ }
1351
1370
  }
1352
1371
 
1353
1372
  const USER_AGENT_SUFFIX = "vectorstores-cdbnosql-vectorstore-javascript";
@@ -1530,14 +1549,28 @@ class AzureCosmosDBNoSqlVectorStore extends BaseVectorStore {
1530
1549
  return ids;
1531
1550
  }
1532
1551
  /**
1533
- * Delete a document from the CosmosDB container.
1552
+ * Delete all nodes from the CosmosDB container that belong to the given document.
1534
1553
  *
1535
- * @param refDocId - The id of the document to delete
1536
- * @param deleteOptions - Any options to pass to the container.item.delete function
1554
+ * @param refDocId - Reference document ID - all nodes with this ref_doc_id will be deleted.
1555
+ * @param deleteOptions - Any options to pass to the delete operations.
1537
1556
  * @returns Promise that resolves if the delete query did not throw an error.
1538
1557
  */ async delete(refDocId, deleteOptions) {
1539
1558
  await this.initialize();
1540
- await this.container.item(refDocId).delete(deleteOptions);
1559
+ // Query all items with matching ref_doc_id
1560
+ const querySpec = {
1561
+ query: `SELECT c.id FROM c WHERE c.metadata.ref_doc_id = @refDocId`,
1562
+ parameters: [
1563
+ {
1564
+ name: "@refDocId",
1565
+ value: refDocId
1566
+ }
1567
+ ]
1568
+ };
1569
+ const { resources: items } = await this.container.items.query(querySpec).fetchAll();
1570
+ // Delete each matching item
1571
+ for (const item of items){
1572
+ await this.container.item(item.id, item.id).delete(deleteOptions);
1573
+ }
1541
1574
  }
1542
1575
  /**
1543
1576
  * Performs a vector similarity search query in the CosmosDB container.
@@ -1631,6 +1664,19 @@ class AzureCosmosDBNoSqlVectorStore extends BaseVectorStore {
1631
1664
  });
1632
1665
  this.container = container;
1633
1666
  }
1667
+ async exists(refDocId) {
1668
+ await this.initialize();
1669
+ const { resources } = await this.container.items.query({
1670
+ query: `SELECT VALUE COUNT(1) FROM c WHERE c.metadata.ref_doc_id = @refDocId`,
1671
+ parameters: [
1672
+ {
1673
+ name: "@refDocId",
1674
+ value: refDocId
1675
+ }
1676
+ ]
1677
+ }).fetchAll();
1678
+ return (resources[0] ?? 0) > 0;
1679
+ }
1634
1680
  }
1635
1681
 
1636
1682
  export { AzureAISearchVectorStore, AzureAISearchVectorStoreConfig, AzureCosmosDBMongoDBSimilarityType, AzureCosmosDBMongoDBVectorStore, AzureCosmosDBNoSqlVectorStore, AzureQueryResultSearchBase, AzureQueryResultSearchDefault, AzureQueryResultSearchHybrid, AzureQueryResultSearchSemanticHybrid, AzureQueryResultSearchSparse, IndexManagement, MetadataIndexFieldType, SimpleCosmosDBReader };
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Document, metadataDictToNode, BaseVectorStore, MetadataMode, nodeToMetadata, FilterOperator, FilterCondition, VectorStoreQueryMode } from '@vectorstores/core';
2
2
  export * from './storage.js';
3
- import { KnownVectorSearchAlgorithmKind, KnownAnalyzerNames, KnownSearchFieldDataType, KnownVectorSearchAlgorithmMetric, KnownVectorSearchCompressionKind, SearchClient, SearchIndexClient, AzureKeyCredential, IndexDocumentsBatch } from '@azure/search-documents';
4
3
  import { DefaultAzureCredential, ManagedIdentityCredential } from '@azure/identity';
4
+ import { KnownVectorSearchAlgorithmKind, KnownAnalyzerNames, KnownSearchFieldDataType, KnownVectorSearchAlgorithmMetric, KnownVectorSearchCompressionKind, SearchClient, SearchIndexClient, AzureKeyCredential, IndexDocumentsBatch } from '@azure/search-documents';
5
5
  import { consoleLogger, getEnv } from '@vectorstores/env';
6
6
  import { MongoClient } from 'mongodb';
7
7
  import { VectorEmbeddingDistanceFunction, VectorEmbeddingDataType, VectorIndexType, CosmosClient } from '@azure/cosmos';
@@ -363,11 +363,10 @@ const vectorStore = new AzureAISearchVectorStore({
363
363
  const documents = await new SimpleDirectoryReader().loadData(
364
364
  "data/paul_graham/",
365
365
  );
366
- const storageContext = await storageContextFromDefaults({ vectorStore });
367
366
 
368
- // Create index from documents with the specified storage context
367
+ // Create index from documents with the specified vector store
369
368
  const index = await VectorStoreIndex.fromDocuments(documents, {
370
- storageContext,
369
+ vectorStore,
371
370
  docStoreStrategy: DocStoreStrategy.UPSERTS,
372
371
  });
373
372
 
@@ -787,7 +786,7 @@ console.log({ response });
787
786
  }
788
787
  }
789
788
  #buildCredentials(options) {
790
- let { credential: credential, key, endpoint, indexName } = options;
789
+ let { credential, key, endpoint, indexName } = options;
791
790
  // validate and use credential
792
791
  if (credential) {
793
792
  // if credential are provided, ensure they are an instance of valid credential instances
@@ -1119,9 +1118,19 @@ console.log({ response });
1119
1118
  // Execute the search and return the result
1120
1119
  return await azureQueryResultSearch.search();
1121
1120
  }
1121
+ async exists(refDocId) {
1122
+ const results = await this._searchClient.search("*", {
1123
+ filter: `ref_doc_id eq '${refDocId}'`,
1124
+ top: 1
1125
+ });
1126
+ for await (const _result of results.results){
1127
+ return true;
1128
+ }
1129
+ return false;
1130
+ }
1122
1131
  }
1123
1132
 
1124
- var version = "0.1.3";
1133
+ var version = "0.1.5";
1125
1134
  var pkg = {
1126
1135
  version: version};
1127
1136
 
@@ -1204,13 +1213,14 @@ var pkg = {
1204
1213
  return Object.values(insertResult.insertedIds).map((id)=>String(id));
1205
1214
  }
1206
1215
  /**
1207
- * Removes specified documents from the AzureCosmosDBMongoDBVectorStore.
1208
- * @param params Parameters for the delete operation.
1216
+ * Deletes all nodes from the collection that belong to the given document.
1217
+ * @param refDocId Reference document ID - all nodes with this ref_doc_id will be deleted.
1218
+ * @param deleteOptions Additional delete options.
1209
1219
  * @returns A promise that resolves when the documents have been removed.
1210
- */ async delete(id, deleteOptions) {
1220
+ */ async delete(refDocId, deleteOptions) {
1211
1221
  const collection = await this.ensureCollection();
1212
1222
  await collection.deleteMany({
1213
- id: id
1223
+ [`${this.metadataKey}.ref_doc_id`]: refDocId
1214
1224
  }, deleteOptions);
1215
1225
  }
1216
1226
  async query(query, options) {
@@ -1348,6 +1358,15 @@ var pkg = {
1348
1358
  await collection.dropIndex(indexName);
1349
1359
  }
1350
1360
  }
1361
+ async exists(refDocId) {
1362
+ const collection = await this.ensureCollection();
1363
+ const count = await collection.countDocuments({
1364
+ [`${this.metadataKey}.ref_doc_id`]: refDocId
1365
+ }, {
1366
+ limit: 1
1367
+ });
1368
+ return count > 0;
1369
+ }
1351
1370
  }
1352
1371
 
1353
1372
  const USER_AGENT_SUFFIX = "vectorstores-cdbnosql-vectorstore-javascript";
@@ -1530,14 +1549,28 @@ class AzureCosmosDBNoSqlVectorStore extends BaseVectorStore {
1530
1549
  return ids;
1531
1550
  }
1532
1551
  /**
1533
- * Delete a document from the CosmosDB container.
1552
+ * Delete all nodes from the CosmosDB container that belong to the given document.
1534
1553
  *
1535
- * @param refDocId - The id of the document to delete
1536
- * @param deleteOptions - Any options to pass to the container.item.delete function
1554
+ * @param refDocId - Reference document ID - all nodes with this ref_doc_id will be deleted.
1555
+ * @param deleteOptions - Any options to pass to the delete operations.
1537
1556
  * @returns Promise that resolves if the delete query did not throw an error.
1538
1557
  */ async delete(refDocId, deleteOptions) {
1539
1558
  await this.initialize();
1540
- await this.container.item(refDocId).delete(deleteOptions);
1559
+ // Query all items with matching ref_doc_id
1560
+ const querySpec = {
1561
+ query: `SELECT c.id FROM c WHERE c.metadata.ref_doc_id = @refDocId`,
1562
+ parameters: [
1563
+ {
1564
+ name: "@refDocId",
1565
+ value: refDocId
1566
+ }
1567
+ ]
1568
+ };
1569
+ const { resources: items } = await this.container.items.query(querySpec).fetchAll();
1570
+ // Delete each matching item
1571
+ for (const item of items){
1572
+ await this.container.item(item.id, item.id).delete(deleteOptions);
1573
+ }
1541
1574
  }
1542
1575
  /**
1543
1576
  * Performs a vector similarity search query in the CosmosDB container.
@@ -1631,6 +1664,19 @@ class AzureCosmosDBNoSqlVectorStore extends BaseVectorStore {
1631
1664
  });
1632
1665
  this.container = container;
1633
1666
  }
1667
+ async exists(refDocId) {
1668
+ await this.initialize();
1669
+ const { resources } = await this.container.items.query({
1670
+ query: `SELECT VALUE COUNT(1) FROM c WHERE c.metadata.ref_doc_id = @refDocId`,
1671
+ parameters: [
1672
+ {
1673
+ name: "@refDocId",
1674
+ value: refDocId
1675
+ }
1676
+ ]
1677
+ }).fetchAll();
1678
+ return (resources[0] ?? 0) > 0;
1679
+ }
1634
1680
  }
1635
1681
 
1636
1682
  export { AzureAISearchVectorStore, AzureAISearchVectorStoreConfig, AzureCosmosDBMongoDBSimilarityType, AzureCosmosDBMongoDBVectorStore, AzureCosmosDBNoSqlVectorStore, AzureQueryResultSearchBase, AzureQueryResultSearchDefault, AzureQueryResultSearchHybrid, AzureQueryResultSearchSemanticHybrid, AzureQueryResultSearchSparse, IndexManagement, MetadataIndexFieldType, SimpleCosmosDBReader };