Fix: flag recreated search index for reindex on next startup (#14877)

This commit is contained in:
Trenton H authored and GitHub committed 2026-11-26 09:57:00 -08:00
1 parent b0dea5a07d
commit f775b94cd6
2 files changed
+24 -1

No files matched your search

+6 -1
View File
@@ -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
@@ -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)