Fix: handle notes without a user when building the search index (#13260)

This commit is contained in:
Matthias Mast
2026-07-24 06:39:11 -07:00
committed by GitHub
parent 6d249b4932
commit b992f9fc05
2 changed files with 24 additions and 1 deletions
+7 -1
View File
@@ -475,7 +475,13 @@ class TantivyBackend:
note_texts: list[str] = []
for note in document.notes.all():
num_notes += 1
doc.add_json("notes", {"note": note.note, "user": note.user.username})
doc.add_json(
"notes",
{
"note": note.note,
"user": note.user.username if note.user else None,
},
)
note_texts.append(note.note)
if note_texts:
doc.add_text("notes_text", " ".join(note_texts))
@@ -844,6 +844,23 @@ class TestFieldHandling:
f"Expected 1, got {len(ids)}. Note content should be searchable via notes.note: prefix."
)
def test_notes_without_user_are_indexed(self, backend: TantivyBackend) -> None:
"""Notes whose user was deleted (SET_NULL) must not break indexing."""
doc = Document.objects.create(
title="Doc with orphaned note",
content="test",
checksum="NT2",
pk=81,
)
Note.objects.create(document=doc, note="Orphaned note", user=None)
backend.add_or_update(doc)
ids = backend.search_ids("notes.note:orphaned", user=None)
assert len(ids) == 1, (
f"Expected 1, got {len(ids)}. Notes without a user should still be indexed."
)
class TestHighlightHits:
"""Test highlight_hits returns proper HTML strings, not raw Snippet objects."""