From f775b94cd6753deda2eaa8e6be413d485bc20b4d Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:57:00 -0800 Subject: [PATCH] Fix: flag recreated search index for reindex on next startup (#14877) --- src/documents/search/_schema.py | 7 ++++++- .../tests/search/test_schema_version.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/documents/search/_schema.py b/src/documents/search/_schema.py index ed8405e0a..66f7bed83 100644 --- a/src/documents/search/_schema.py +++ b/src/documents/search/_schema.py @@ -387,9 +387,14 @@ def open_or_rebuild_index(index_dir: Path | None = None) -> tantivy.Index: logger.exception( "Search index is corrupted or incomplete. A new, empty index was " "created, but existing documents are not indexed yet. " - "Run 'document_index reindex' to repopulate it.", + "They will be reindexed on the next startup, or run " + "'document_index reindex' to do so now.", ) wipe_index(index_dir) idx = tantivy.Index(build_schema(), path=str(index_dir)) _write_sentinels(index_dir) + # The new index is stamped as current but is empty. Flag it so the next + # `document_index reindex --if-needed` repopulates it. Reindexing here + # would block whichever process happened to open the index first. + (index_dir / REBUILD_MARKER).touch() return idx diff --git a/src/documents/tests/search/test_schema_version.py b/src/documents/tests/search/test_schema_version.py index 3826f69c0..ca3e82914 100644 --- a/src/documents/tests/search/test_schema_version.py +++ b/src/documents/tests/search/test_schema_version.py @@ -225,3 +225,21 @@ class TestCorruptedIndex: assert (current_index_missing_meta_json / "meta.json").exists() tantivy.Index.open(str(current_index_missing_meta_json)) + + def test_missing_meta_json_flags_index_for_reindex( + self, + current_index_missing_meta_json: Path, + ) -> None: + """ + GIVEN: + - An index directory with matching sentinels but a missing meta.json + WHEN: + - open_or_rebuild_index() recreates it as an empty index + THEN: + - needs_rebuild() is True, so the next + `document_index reindex --if-needed` repopulates the index + instead of reporting the empty one as up to date + """ + open_or_rebuild_index(current_index_missing_meta_json) + + assert needs_rebuild(current_index_missing_meta_json)