From 4af7d7c45f4ee1892b63174cdb009a0f55d90162 Mon Sep 17 00:00:00 2001 From: Stone <47559019+st1020@users.noreply.github.com> Date: Thu, 28 Sep 2023 05:42:38 +0800 Subject: [PATCH] Adding delete_all_documents method for IndexWriter (#133) --- src/index.rs | 6 ++++++ tests/tantivy_test.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/index.rs b/src/index.rs index 0102c1b..573e77d 100644 --- a/src/index.rs +++ b/src/index.rs @@ -117,6 +117,12 @@ impl IndexWriter { Ok(()) } + /// Deletes all documents from the index. + fn delete_all_documents(&mut self) -> PyResult<()> { + self.inner()?.delete_all_documents().map_err(to_pyerr)?; + Ok(()) + } + /// The opstamp of the last successful commit. /// /// This is the opstamp the index will rollback to if there is a failure diff --git a/tests/tantivy_test.py b/tests/tantivy_test.py index 6b1d7fd..4fb8869 100644 --- a/tests/tantivy_test.py +++ b/tests/tantivy_test.py @@ -488,6 +488,18 @@ class TestClass(object): assert orig == pickled + def test_delete_all_documents(self, ram_index): + index = ram_index + writer = index.writer() + writer.delete_all_documents() + writer.commit() + + index.reload() + query = index.parse_query("sea whale", ["title", "body"]) + result = index.searcher().search(query, 10) + + assert len(result.hits) == 0 + class TestUpdateClass(object): def test_delete_update(self, ram_index):