Skip to content

~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.

refactor: CollectionSearchEngine searches only its own index

LearningCircuite93f228801f5
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_collection.py b/src/local_deep_research/web_search_engines/engines/search_engine_collection.py
index d8b1121ce5..72322aafc6 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_collection.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_collection.py
@@ -8,12 +8,15 @@ from typing import List, Dict, Any, Optional
from loguru import logger
from .search_engine_library import LibraryRAGSearchEngine
+from ...research_library.services.library_rag_service import LibraryRAGService
+from ...database.models.library import RAGIndex, Document
+from ...database.session_context import get_user_db_session
class CollectionSearchEngine(LibraryRAGSearchEngine):
"""
Search engine for a specific document collection using RAG.
- Extends LibraryRAGSearchEngine to filter results by collection ID.
+ Directly searches only the specified collection's FAISS index.
Each collection uses its own embedding model that was used during indexing.
"""
@@ -53,50 +56,48 @@ class CollectionSearchEngine(LibraryRAGSearchEngine):
self.collection_name = collection_name
self.collection_key = f"collection_{collection_id}"
- # Override embedding settings with collection-specific settings if available
+ # Load collection-specific embedding settings
self._load_collection_embedding_settings()
def _load_collection_embedding_settings(self):
"""
- Load embedding settings from the collection's metadata.
- If the collection has stored embedding settings, use those instead of global settings.
+ Load embedding settings from the collection's RAG index.
+ Uses the same embedding model that was used during indexing.
"""
if not self.username:
logger.warning("Cannot load collection settings without username")
return
try:
- from ...database.models.library import Collection
- from ...database.session_context import get_user_db_session
-
with get_user_db_session(self.username) as db_session:
- collection = (
- db_session.query(Collection)
- .filter_by(id=self.collection_id)
+ # Get RAG index for this collection
+ rag_index = (
+ db_session.query(RAGIndex)
+ .filter_by(
+ collection_name=self.collection_key,
+ is_current=True,
+ )
.first()
)
- if not collection:
- logger.warning(f"Collection {self.collection_id} not found")
- return
-
- # If collection has embedding metadata, use it
- if collection.embedding_model:
- logger.info(
- f"Using collection-specific embedding settings: "
- f"{collection.embedding_model_type}/{collection.embedding_model}"
- )
- self.embedding_model = collection.embedding_model
- self.embedding_provider = collection.embedding_model_type
- self.chunk_size = collection.chunk_size or self.chunk_size
- self.chunk_overlap = (
- collection.chunk_overlap or self.chunk_overlap
- )
- else:
+ if not rag_index:
logger.warning(
- f"Collection {self.collection_id} has no embedding metadata stored. "
- f"Using global settings: {self.embedding_provider}/{self.embedding_model}"
+ f"No RAG index found for collection {self.collection_id}"
)
+ return
+
+ # Use embedding settings from the RAG index
+ self.embedding_model = rag_index.embedding_model
+ self.embedding_provider = rag_index.embedding_model_type.value
+ self.chunk_size = rag_index.chunk_size or self.chunk_size
+ self.chunk_overlap = (
+ rag_index.chunk_overlap or self.chunk_overlap
+ )
+
+ logger.info(
+ f"Collection '{self.collection_name}' using embedding: "
+ f"{self.embedding_provider}/{self.embedding_model}"
+ )
except Exception:
logger.exception(
@@ -113,6 +114,9 @@ class CollectionSearchEngine(LibraryRAGSearchEngine):
"""
Search within the specific collection using semantic search.
+ Directly searches only this collection's FAISS index instead of
+ searching all collections and filtering.
+
Args:
query: Search query
limit: Maximum number of results to return
@@ -120,44 +124,136 @@ class CollectionSearchEngine(LibraryRAGSearchEngine):
extra_params: Additional search parameters
Returns:
- List of search results filtered to this collection
+ List of search results from this collection
"""
- # Get results from parent (all library documents)
- # Request 3x the limit to ensure we have enough after filtering
- all_results = super().search(
- query,
- limit=limit * 3,
- llm_callback=llm_callback,
- extra_params=extra_params,
- )
+ if not self.username:
+ logger.error("Cannot search collection without username")
+ return []
- # Filter to only results from this collection
- # Convert both to strings for comparison since FAISS may serialize UUIDs as strings
- target_collection_id = str(self.collection_id)
- filtered_results = [
- result
- for result in all_results
- if str(result.get("metadata", {}).get("collection_id", ""))
- == target_collection_id
- ]
-
- # Limit to requested number
- filtered_results = filtered_results[:limit]
-
- logger.info(
- f"Collection '{self.collection_name}' (id={target_collection_id}) search returned "
- f"{len(filtered_results)} results (filtered from {len(all_results)} total library results)"
- )
+ try:
+ # Get RAG index info for this collection
+ with get_user_db_session(self.username) as db_session:
+ rag_index = (
+ db_session.query(RAGIndex)
+ .filter_by(
+ collection_name=self.collection_key,
+ is_current=True,
+ )
+ .first()
+ )
- if len(all_results) > 0 and len(filtered_results) == 0:
- # Debug: log sample of collection_ids from results for troubleshooting
- sample_ids = [
- str(r.get("metadata", {}).get("collection_id", "N/A"))
- for r in all_results[:3]
- ]
- logger.debug(
- f"No matches found. Target: {target_collection_id}, "
- f"Sample result collection_ids: {sample_ids}"
+ if not rag_index:
+ logger.info(
+ f"No RAG index for collection '{self.collection_name}'"
+ )
+ return []
+
+ # Get embedding settings from RAG index
+ embedding_model = rag_index.embedding_model
+ embedding_provider = rag_index.embedding_model_type.value
+ chunk_size = rag_index.chunk_size or self.chunk_size
+ chunk_overlap = rag_index.chunk_overlap or self.chunk_overlap
+
+ # Create RAG service with collection's embedding settings
+ rag_service = LibraryRAGService(
+ username=self.username,
+ embedding_model=embedding_model,
+ embedding_provider=embedding_provider,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
)
- return filtered_results
+ # Check if there are indexed documents
+ stats = rag_service.get_rag_stats(self.collection_id)
+ if stats.get("indexed_documents", 0) == 0:
+ logger.info(
+ f"No documents indexed in collection '{self.collection_name}'"
+ )
+ return []
+
+ # Load and search the FAISS index for this collection
+ vector_store = rag_service.load_or_create_faiss_index(
+ self.collection_id
+ )
+
+ docs_with_scores = vector_store.similarity_search_with_score(
+ query, k=limit
+ )
+
+ if not docs_with_scores:
+ logger.info(
+ f"No results found in collection '{self.collection_name}'"
+ )
+ return []
+
+ # Convert to search result format
+ results = []
+ for doc, score in docs_with_scores:
+ metadata = doc.metadata or {}
+
+ # Get document ID
+ doc_id = metadata.get("source_id") or metadata.get(
+ "document_id"
+ )
+
+ # Get title
+ title = (
+ metadata.get("document_title")
+ or metadata.get("title")
+ or (f"Document {doc_id}" if doc_id else "Untitled")
+ )
+
+ # Create snippet from content
+ snippet = (
+ doc.page_content[:500] + "..."
+ if len(doc.page_content) > 500
+ else doc.page_content
+ )
+
+ # Generate document URL
+ document_url = self._get_document_url(doc_id)
+
+ # Add collection info to metadata
+ metadata["collection_id"] = self.collection_id
+ metadata["collection_name"] = self.collection_name
+
+ result = {
+ "title": title,
+ "snippet": snippet,
+ "url": document_url,
+ "link": document_url,
+ "source": "library",
+ "relevance_score": float(1 / (1 + score)),
+ "metadata": metadata,
+ }
+ results.append(result)
+
+ logger.info(
+ f"Collection '{self.collection_name}' search returned "
+ f"{len(results)} results for query: {query[:50]}..."
+ )
+
+ return results
+
+ except Exception:
+ logger.exception(
+ f"Error searching collection '{self.collection_name}'"
+ )
+ return []
+
+ def _get_document_url(self, doc_id: Optional[str]) -> str:
+ """Get the URL for viewing a document."""
+ if not doc_id:
+ return "#"
+
+ try:
+ with get_user_db_session(self.username) as session:
+ document = session.query(Document).filter_by(id=doc_id).first()
+ if document:
+ if document.file_type == "pdf" and document.file_path:
+ return f"/library/document/{doc_id}/pdf"
+ return f"/library/document/{doc_id}/txt"
+ except Exception as e:
+ logger.warning(f"Error getting document URL for {doc_id}: {e}")
+
+ return f"/library/document/{doc_id}/txt"