From 05917a04aaf1e968dc49ca6908511325fa2e35a2 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 27 Aug 2026 12:19:31 -0700 Subject: [PATCH] Fix: immediately re-add doc to index after trash restore (#13818) --- src/documents/tests/test_api_search.py | 30 ++++++++++++++++++++++++++ src/documents/views.py | 9 +++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/documents/tests/test_api_search.py b/src/documents/tests/test_api_search.py index e597904dd..f4ebf6d97 100644 --- a/src/documents/tests/test_api_search.py +++ b/src/documents/tests/test_api_search.py @@ -93,6 +93,36 @@ class TestDocumentSearchApi(DirectoriesMixin, APITestCase): self.assertEqual(response.data["count"], 0) self.assertEqual(len(results), 0) + def test_search_after_restore_from_trash(self) -> None: + """ + GIVEN: + - Indexed document that was moved to the trash + WHEN: + - The document is restored from the trash + THEN: + - The document is searchable again without a reindex + """ + doc = Document.objects.create( + title="invoice", + content="the thing i bought at a shop and paid with bank account", + checksum="A", + pk=1, + ) + get_backend().add_or_update(doc) + + self.assertEqual(self.client.get("/api/documents/?query=shop").data["count"], 1) + + self.client.delete(f"/api/documents/{doc.pk}/") + self.assertEqual(self.client.get("/api/documents/?query=shop").data["count"], 0) + + response = self.client.post( + "/api/trash/", + {"action": "restore", "documents": [doc.pk]}, + ) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + self.assertEqual(self.client.get("/api/documents/?query=shop").data["count"], 1) + def test_simple_text_search(self) -> None: tagged = Tag.objects.create(name="invoice") matching_doc = Document.objects.create( diff --git a/src/documents/views.py b/src/documents/views.py index bc77dc5eb..09f5d914a 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -5432,8 +5432,15 @@ class TrashView(ListModelMixin, PassUserMixin): return HttpResponseForbidden("Insufficient permissions") action = serializer.validated_data.get("action") if action == "restore": - for doc in Document.deleted_objects.filter(id__in=doc_ids).all(): + restored = list(Document.deleted_objects.filter(id__in=doc_ids)) + for doc in restored: doc.restore(strict=False) + if restored: + from documents.search import get_backend + + with get_backend().batch_update() as batch: + for doc in restored: + batch.add_or_update(doc) elif action == "empty": if doc_ids is None: doc_ids = [doc.id for doc in docs]