@@ -832,3 +832,53 @@ def test_delete_all_documents_excessive_batch_size(self, document_store, caplog)
832832 document_store .delete_all_documents (batch_size = 20000 )
833833 assert document_store .count_documents () == 5
834834 assert "Not all documents have been deleted." in caplog .text
835+
836+ def test_delete_by_filter (self , document_store ):
837+ docs = [
838+ Document (content = "Doc 1" , meta = {"category" : "A" }),
839+ Document (content = "Doc 2" , meta = {"category" : "B" }),
840+ Document (content = "Doc 3" , meta = {"category" : "A" }),
841+ ]
842+ document_store .write_documents (docs )
843+ assert document_store .count_documents () == 3
844+
845+ # Delete documents with category="A"
846+ deleted_count = document_store .delete_by_filter (
847+ filters = {"field" : "meta.category" , "operator" : "==" , "value" : "A" }
848+ )
849+ assert deleted_count == 2
850+ assert document_store .count_documents () == 1
851+
852+ # Verify only category B remains
853+ remaining_docs = document_store .filter_documents ()
854+ assert len (remaining_docs ) == 1
855+ assert remaining_docs [0 ].meta ["category" ] == "B"
856+
857+ def test_update_by_filter (self , document_store ):
858+ docs = [
859+ Document (content = "Doc 1" , meta = {"category" : "A" , "status" : "draft" }),
860+ Document (content = "Doc 2" , meta = {"category" : "B" , "status" : "draft" }),
861+ Document (content = "Doc 3" , meta = {"category" : "A" , "status" : "draft" }),
862+ ]
863+ document_store .write_documents (docs )
864+ assert document_store .count_documents () == 3
865+
866+ # Update status for category="A" documents
867+ updated_count = document_store .update_by_filter (
868+ filters = {"field" : "meta.category" , "operator" : "==" , "value" : "A" }, meta = {"status" : "published" }
869+ )
870+ assert updated_count == 2
871+
872+ # Verify the updates
873+ published_docs = document_store .filter_documents (
874+ filters = {"field" : "meta.status" , "operator" : "==" , "value" : "published" }
875+ )
876+ assert len (published_docs ) == 2
877+ for doc in published_docs :
878+ assert doc .meta ["category" ] == "A"
879+ assert doc .meta ["status" ] == "published"
880+
881+ # Verify category B still has draft status
882+ draft_docs = document_store .filter_documents (filters = {"field" : "meta.status" , "operator" : "==" , "value" : "draft" })
883+ assert len (draft_docs ) == 1
884+ assert draft_docs [0 ].meta ["category" ] == "B"
0 commit comments