~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(library-search): fall back to Document.filename in keyword leg title (resolves #5207) (#5208)
S15229b65a6f7
diff --git a/changelog.d/5207.bugfix.md b/changelog.d/5207.bugfix.mdnew file mode 100644index 0000000000..d43b3d480f--- /dev/null+++ b/changelog.d/5207.bugfix.md@@ -0,0 +1 @@+Fixed `/library/search/` keyword search showing "Untitled" for user-uploaded documents with no explicit title by falling back to `Document.filename`.diff --git a/src/local_deep_research/web/routes/unified_search_routes.py b/src/local_deep_research/web/routes/unified_search_routes.pyindex a9138386d0..ab7b8e058a 100644--- a/src/local_deep_research/web/routes/unified_search_routes.py+++ b/src/local_deep_research/web/routes/unified_search_routes.py@@ -172,7 +172,7 @@ def keyword_search():q: Search query (required, capped at MAX_SEARCH_LEN)limit: Maximum results (default 20, clamped 1..50)- Column-projected (id, title, 300-char preview, source-type name,+ Column-projected (id, title, filename, 300-char preview, source-type name,updated_at, research_id) — never hydrates full text_content."""username = session.get("username")@@ -208,6 +208,7 @@ def keyword_search():db.query(Document.id,Document.title,+ Document.filename,func.substr(Document.text_content, preview_start, PREVIEW_LEN),@@ -239,6 +240,7 @@ def keyword_search():for (doc_id,title,+ filename,preview,source_type_name,updated_at,@@ -253,10 +255,19 @@ def keyword_search():# preview isn't truncated and a leading "…" would be spurious.if pos and pos > PREVIEW_CONTEXT + 1:preview = "…" + preview+ # Fall back to filename (set by user uploads) when title is NULL,+ # so uploaded documents without an explicit title still surface a+ # useful name instead of the generic 'Untitled'. Filename is+ # already werkzeug-sanitized (spaces -> underscores) at upload+ # time, so what we return matches what the semantic leg already+ # shows. Final 'Untitled' fallback only fires when both title+ # AND filename are missing (defensive — every current code path+ # sets at least one of them).+ display_title = title or filename or "Untitled"results.append({"id": doc_id,- "title": title or "Untitled",+ "title": display_title,"content_preview": preview,"source_type": source_type_name,"updated_at": updated_at.isoformat()diff --git a/tests/web/routes/test_unified_search_routes.py b/tests/web/routes/test_unified_search_routes.pyindex 8040633344..9e902a0e76 100644--- a/tests/web/routes/test_unified_search_routes.py+++ b/tests/web/routes/test_unified_search_routes.py@@ -47,7 +47,7 @@ def _session_cm(sessions):class TestKeywordSearch:- def _call(self, rows, query_string="q=alpha"):+ def _call(self, rows, query_string="q=alpha", return_session=False):app = _app()session_mock = MagicMock()(@@ -63,25 +63,49 @@ class TestKeywordSearch:_session_cm([session_mock]),):response = _handler(unified_search_routes.keyword_search)()- return _unpack(response)+ payload, status = _unpack(response)+ if return_session:+ return payload, status, session_mock+ return payload, statusdef test_happy_path_payload_shape_and_url_computation(self):updated = datetime(2026, 7, 1, 12, 0, 0)- # Final element = instr() match position (0 = title-only match →+ # Row shape mirrors the SELECT: (id, title, filename, preview,+ # source_type, updated_at, research_id, instr_match_pos). Final+ # element = instr() match position (0 = title-only match →# head-anchored preview, no ellipsis).rows = [- ("n1", "My note", "note preview", "note", updated, None, 0),+ ("n1", "My note", None, "note preview", "note", updated, None, 0),("r1","Run report",+ None,"report preview","research_report",updated,"res-9",0,),- ("r2", "Orphan report", "p", "research_report", updated, None, 0),- ("u1", "Uploaded PDF", "p", "user_upload", updated, None, 0),+ (+ "r2",+ "Orphan report",+ None,+ "p",+ "research_report",+ updated,+ None,+ 0,+ ),+ (+ "u1",+ "Uploaded PDF",+ None,+ "p",+ "user_upload",+ updated,+ None,+ 0,+ ),]payload, status = self._call(rows)assert status == 200@@ -117,6 +141,7 @@ class TestKeywordSearch:("d1","Doc",+ None,"text around the match","note",updated,@@ -124,7 +149,7 @@ class TestKeywordSearch:deep_pos,),# Match inside the lead-in zone → head preview, no marker.- ("d2", "Doc2", "head text", "note", updated, None, 10),+ ("d2", "Doc2", None, "head text", "note", updated, None, 10),]payload, status = self._call(rows)assert status == 200@@ -152,6 +177,167 @@ class TestKeywordSearch:assert status == 400assert payload["success"] is False+ def test_title_null_falls_back_to_filename(self):+ # Uploaded documents have title=NULL and filename=<book name>.+ # The keyword leg must surface the filename (the same string the+ # semantic leg shows via DocumentChunk.document_title) instead of+ # the generic 'Untitled' so a user can identify the document.+ # Regression for the bug where the keyword leg did+ # title or 'Untitled'+ # ignoring filename entirely, producing "Untitled" for every+ # uploaded .txt / .pdf.+ updated = datetime(2026, 7, 1, 12, 0, 0)+ rows = [+ (+ "u1",+ None, # Document.title+ "10_Lessons_from_Hindu_History_in_10_Episodes.txt",+ "p",+ "user_upload",+ updated,+ None,+ 0,+ ),+ ]+ payload, status = self._call(rows)+ assert status == 200+ assert payload["results"][0]["title"] == (+ "10_Lessons_from_Hindu_History_in_10_Episodes.txt"+ )++ def test_title_empty_string_falls_back_to_filename(self):+ # An empty string is falsy in Python; the ``or`` chain must still+ # skip it and use filename.+ updated = datetime(2026, 7, 1, 12, 0, 0)+ rows = [+ (+ "u1",+ "",+ "book.txt",+ "p",+ "user_upload",+ updated,+ None,+ 0,+ ),+ ]+ payload, status = self._call(rows)+ assert status == 200+ assert payload["results"][0]["title"] == "book.txt"++ def test_title_set_wins_over_filename(self):+ # When Document.title is populated (e.g. research downloads,+ # notes), it must take precedence — filename is only a fallback.+ updated = datetime(2026, 7, 1, 12, 0, 0)+ rows = [+ (+ "d1",+ "Real Title",+ "should_not_appear.txt",+ "p",+ "research_report",+ updated,+ "res-1",+ 0,+ ),+ ]+ payload, status = self._call(rows)+ assert status == 200+ assert payload["results"][0]["title"] == "Real Title"++ def test_title_and_filename_both_null_falls_back_to_untitled(self):+ # Defensive: a document with no title and no filename (only+ # possible if a future upload path omits both) must still return+ # a string for the client. This is the ONLY case where+ # 'Untitled' should appear.+ updated = datetime(2026, 7, 1, 12, 0, 0)+ rows = [+ (+ "x1",+ None,+ None,+ "p",+ "user_upload",+ updated,+ None,+ 0,+ ),+ ]+ payload, status = self._call(rows)+ assert status == 200+ assert payload["results"][0]["title"] == "Untitled"++ def test_mixed_title_states_in_one_response(self):+ # End-to-end-ish: a single keyword response containing a mix of+ # title=set, title=NULL+filename=set, and title=NULL+filename=NULL+ # rows. The fallback must apply per-row, not blanket over the+ # response. Mirrors the live-API shape for the 'climate' query+ # before the fix (research reports with proper titles alongside+ # user uploads with no title).+ updated = datetime(2026, 7, 1, 12, 0, 0)+ rows = [+ (+ "r1",+ "Research Title",+ None,+ "p",+ "research_report",+ updated,+ "res-1",+ 0,+ ),+ (+ "u1",+ None,+ "book_one.txt",+ "p",+ "user_upload",+ updated,+ None,+ 0,+ ),+ (+ "u2",+ None,+ "book_two.txt",+ "p",+ "user_upload",+ updated,+ None,+ 0,+ ),+ (+ "n1",+ "Note title",+ None,+ "p",+ "note",+ updated,+ None,+ 0,+ ),+ ]+ payload, status = self._call(rows)+ assert status == 200+ titles = [r["title"] for r in payload["results"]]+ assert titles == [+ "Research Title",+ "book_one.txt",+ "book_two.txt",+ "Note title",+ ]++ def test_orm_query_includes_document_filename(self):+ # Assert that the ORM projection explicitly includes Document.filename,+ # verifying that the query fetches the column needed for fallback.+ from local_deep_research.database.models.library import Document++ payload, status, session_mock = self._call([], return_session=True)+ assert status == 200+ query_args = session_mock.query.call_args[0]+ assert Document.filename in query_args+ assert Document.title in query_args+def test_invalid_limit_maps_to_400(self):payload, status = self._call([], query_string="q=alpha&limit=zzz")assert status == 400