Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

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

Matthias Mastb992f9fc05be
diff --git a/src/documents/search/_backend.py b/src/documents/search/_backend.py
index d61e895a8..7464de79a 100644
--- a/src/documents/search/_backend.py
+++ b/src/documents/search/_backend.py
@@ -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))
diff --git a/src/documents/tests/search/test_backend.py b/src/documents/tests/search/test_backend.py
index f92f46700..e31708506 100644
--- a/src/documents/tests/search/test_backend.py
+++ b/src/documents/tests/search/test_backend.py
@@ -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."""