From 97034e8ff669cf97077d1193a44715a8572d40ae Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Tue, 31 Mar 2026 07:50:44 -0700 Subject: [PATCH] Covers the reindex --recreate path and documents the flag --- docs/administration.md | 12 +++++++++--- src/documents/tests/test_management.py | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/docs/administration.md b/docs/administration.md index 4a4a2f925..013ac9fdd 100644 --- a/docs/administration.md +++ b/docs/administration.md @@ -463,11 +463,17 @@ the search yields non-existing documents or won't find anything, you may need to recreate the index manually. ``` -document_index {reindex,optimize} +document_index {reindex,optimize} [--recreate] [--if-needed] ``` -Specify `reindex` to have the index created from scratch. This may take -some time. +Specify `reindex` to rebuild the index from all documents in the database. This +may take some time. + +Pass `--recreate` to wipe the existing index before rebuilding. Use this when the +index is corrupted or you want a fully clean rebuild. + +Pass `--if-needed` to skip the rebuild if the index is already up to date (schema +version and search language match). Safe to run on every startup or upgrade. Specify `optimize` to optimize the index. This command is regularly invoked by the task scheduler. diff --git a/src/documents/tests/test_management.py b/src/documents/tests/test_management.py index 3e61b74f8..6ea4431fd 100644 --- a/src/documents/tests/test_management.py +++ b/src/documents/tests/test_management.py @@ -117,6 +117,32 @@ class TestMakeIndex: """Optimize command must execute without error (Tantivy handles optimization automatically).""" call_command("document_index", "optimize", skip_checks=True) + def test_reindex_recreate_wipes_index(self, mocker: MockerFixture) -> None: + """Reindex with --recreate must wipe the index before rebuilding.""" + mock_wipe = mocker.patch( + "documents.management.commands.document_index.wipe_index", + ) + mock_get_backend = mocker.patch( + "documents.management.commands.document_index.get_backend", + ) + call_command("document_index", "reindex", recreate=True, skip_checks=True) + mock_wipe.assert_called_once() + mock_get_backend.return_value.rebuild.assert_called_once() + + def test_reindex_without_recreate_does_not_wipe_index( + self, + mocker: MockerFixture, + ) -> None: + """Reindex without --recreate must not wipe the index.""" + mock_wipe = mocker.patch( + "documents.management.commands.document_index.wipe_index", + ) + mocker.patch( + "documents.management.commands.document_index.get_backend", + ) + call_command("document_index", "reindex", skip_checks=True) + mock_wipe.assert_not_called() + def test_reindex_if_needed_skips_when_up_to_date( self, mocker: MockerFixture,