You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add vector store delete API ref docs with examples
- Document delete APIs with ID lists and filter expressions
- Add versioning use case with metadata-based updates
Signed-off-by: Soby Chacko <soby.chacko@broadcom.com>
The Vector Store interface provides multiple methods for deleting documents, allowing you to remove data either by specific document IDs or using filter expressions.
398
+
399
+
=== Delete by Document IDs
400
+
401
+
The simplest way to delete documents is by providing a list of document IDs:
402
+
403
+
[source,java]
404
+
----
405
+
void delete(List<String> idList);
406
+
----
407
+
408
+
This method removes all documents whose IDs match those in the provided list.
409
+
If any ID in the list doesn't exist in the store, it will be ignored.
410
+
411
+
.Example usage
412
+
[source,java]
413
+
----
414
+
// Create and add document
415
+
Document document = new Document("The World is Big",
416
+
Map.of("country", "Netherlands"));
417
+
vectorStore.add(List.of(document));
418
+
419
+
// Delete document by ID
420
+
vectorStore.delete(List.of(document.getId()));
421
+
----
422
+
423
+
=== Delete by Filter Expression
424
+
425
+
For more complex deletion criteria, you can use filter expressions:
426
+
427
+
[source,java]
428
+
----
429
+
void delete(Filter.Expression filterExpression);
430
+
----
431
+
432
+
This method accepts a `Filter.Expression` object that defines the criteria for which documents should be deleted.
433
+
It's particularly useful when you need to delete documents based on their metadata properties.
434
+
435
+
.Example usage
436
+
[source,java]
437
+
----
438
+
// Create test documents with different metadata
439
+
Document bgDocument = new Document("The World is Big",
440
+
Map.of("country", "Bulgaria"));
441
+
Document nlDocument = new Document("The World is Big",
442
+
Map.of("country", "Netherlands"));
443
+
444
+
// Add documents to the store
445
+
vectorStore.add(List.of(bgDocument, nlDocument));
446
+
447
+
// Delete documents from Bulgaria using filter expression
448
+
Filter.Expression filterExpression = new Filter.Expression(
// results will only contain the Netherlands document
496
+
----
497
+
498
+
=== Error Handling When Calling the Delete API
499
+
500
+
All deletion methods may throw exceptions in case of errors:
501
+
502
+
The best practice is to wrap delete operations in try-catch blocks:
503
+
504
+
.Example usage
505
+
[source,java]
506
+
----
507
+
try {
508
+
vectorStore.delete("country == 'Bulgaria'");
509
+
}
510
+
catch (Exception e) {
511
+
logger.error("Invalid filter expression", e);
512
+
}
513
+
----
514
+
515
+
=== Document Versioning Use Case
516
+
517
+
A common scenario is managing document versions where you need to upload a new version of a document while removing the old version. Here's how to handle this using filter expressions:
518
+
519
+
.Example usage
520
+
[source,java]
521
+
----
522
+
// Create initial document (v1) with version metadata
523
+
Document documentV1 = new Document(
524
+
"AI and Machine Learning Best Practices",
525
+
Map.of(
526
+
"docId", "AIML-001",
527
+
"version", "1.0",
528
+
"lastUpdated", "2024-01-01"
529
+
)
530
+
);
531
+
532
+
// Add v1 to the vector store
533
+
vectorStore.add(List.of(documentV1));
534
+
535
+
// Create updated version (v2) of the same document
536
+
Document documentV2 = new Document(
537
+
"AI and Machine Learning Best Practices - Updated",
538
+
Map.of(
539
+
"docId", "AIML-001",
540
+
"version", "2.0",
541
+
"lastUpdated", "2024-02-01"
542
+
)
543
+
);
544
+
545
+
// First, delete the old version using filter expression
546
+
Filter.Expression deleteOldVersion = new Filter.Expression(
0 commit comments