~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
perf(library): aggregate collection document counts (#5378)
Ziyang Guo92c80718ba94
diff --git a/changelog.d/4856.misc.md b/changelog.d/4856.misc.mdnew file mode 100644index 0000000000..6e4a4aa478--- /dev/null+++ b/changelog.d/4856.misc.md@@ -0,0 +1 @@+Compute collection document totals and indexed totals in one grouped query instead of lazy-loading document links per collection.diff --git a/src/local_deep_research/research_library/routes/rag_routes.py b/src/local_deep_research/research_library/routes/rag_routes.pyindex 7ac64efc85..b88f5bf378 100644--- a/src/local_deep_research/research_library/routes/rag_routes.py+++ b/src/local_deep_research/research_library/routes/rag_routes.py@@ -1305,32 +1305,35 @@ def get_collections():# actual FAISS membership; the legacy DocumentCollection.indexed flag# is retained only for compatibility and must not drive user-visible# truth.- indexed_counts = dict(- db_session.query(- DocumentCollection.collection_id,- func.count(- case(- (- DocumentCollection.indexed == True, # noqa: E712- DocumentCollection.document_id,- ),- else_=None,- )- ),+ collection_counts = {+ collection_id: (+ int(document_count or 0),+ int(indexed_count or 0),)- .group_by(DocumentCollection.collection_id)- .all()- )+ for collection_id, document_count, indexed_count in (+ db_session.query(+ DocumentCollection.collection_id,+ func.count(DocumentCollection.document_id),+ func.count(+ case(+ (+ DocumentCollection.indexed.is_(True),+ DocumentCollection.document_id,+ ),+ else_=None,+ )+ ),+ )+ .group_by(DocumentCollection.collection_id)+ .all()+ )+ }result = []for coll in collections:- document_count = (- len(coll.document_links)- if hasattr(coll, "document_links")- else 0- )- indexed_document_count = int(- indexed_counts.get(coll.id, 0) or 0+ document_count, indexed_document_count = collection_counts.get(+ coll.id,+ (0, 0),)collection_data = {"id": coll.id,diff --git a/tests/research_library/routes/test_rag_routes.py b/tests/research_library/routes/test_rag_routes.pyindex 518d92cd33..4f88dd1b8b 100644--- a/tests/research_library/routes/test_rag_routes.py+++ b/tests/research_library/routes/test_rag_routes.py@@ -2944,6 +2944,40 @@ class TestGetCollectionsIndexedCounts:)assert coll_a["document_count"] != coll_b["document_count"]+ def test_document_link_counts_use_one_grouped_query(self, request):+ """Collection totals must not lazy-load document_links per collection."""+ from sqlalchemy import event++ session, _collection_a_id, _collection_b_id = self._seed_session(+ request+ )+ document_collection_queries = []++ def capture_statement(+ _conn,+ _cursor,+ statement,+ _parameters,+ _context,+ _executemany,+ ):+ normalized = " ".join(statement.lower().split())+ if "from document_collections" in normalized:+ document_collection_queries.append(normalized)++ engine = session.get_bind()+ event.listen(engine, "before_cursor_execute", capture_statement)+ try:+ response = self._call_route(session)+ finally:+ event.remove(engine, "before_cursor_execute", capture_statement)+ session.close()++ assert response.status_code == 200, response.status_code+ assert len(document_collection_queries) == 1+ group_by_clause = "group by document_collections.collection_id"+ assert group_by_clause in document_collection_queries[0]+def test_indexed_count_zero_when_nothing_indexed(self, request):"""A collection with only unindexed links reports indexed_document_count == 0."""import uuid