~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): rank collection search by index metric, not L2 only (#5062)
Ehsanbbd0dd2c96a3
diff --git a/changelog.d/4963.bugfix.md b/changelog.d/4963.bugfix.mdnew file mode 100644index 0000000000..22d16c5f92--- /dev/null+++ b/changelog.d/4963.bugfix.md@@ -0,0 +1 @@+Collection search now ranks results correctly for cosine and dot-product embedding indexes. Previously the relevance score was computed as if every index used L2 distance, which inverted the ranking (the nearest match scored lowest) and could error on an anti-correlated match.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.pyindex 600c2fbff2..482d5a1819 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@@ -7,6 +7,8 @@ Provides semantic search within a specific document collection using RAG.import osfrom typing import List, Dict, Any, Optional+import faiss+from ...security.secure_logging import loggerfrom .search_engine_library import LibraryRAGSearchEnginefrom ...constants import SNIPPET_LENGTH_LONG@@ -18,6 +20,37 @@ from ...config.thread_settings import get_setting_from_snapshotfrom ...config.paths import get_library_directory+def _relevance_from_faiss_score(vector_store: Any, score: float) -> float:+ """Map a raw FAISS score to a [0, 1] relevance that grows with similarity.++ ``similarity_search_with_score`` returns the score straight from the+ underlying FAISS index, and what it means depends on that index's metric+ (chosen in ``LibraryRAGService.load_or_create_faiss_index`` from the+ collection's ``distance_metric``):++ - ``METRIC_L2``: a (squared) distance, ``0`` for an exact match and larger+ the farther apart. ``1 / (1 + distance)`` maps it to ``(0, 1]``,+ decreasing with distance.+ - ``METRIC_INNER_PRODUCT`` (built for the default ``cosine`` metric and for+ ``dot_product``): a similarity, larger for nearer vectors, and in+ ``[-1, 1]`` for the normalized vectors this service indexes. Here+ ``1 / (1 + score)`` inverts the ranking (a perfect match -> 0.5, an+ unrelated vector -> 1.0) and divides by zero for an anti-correlated pair+ (``score == -1``). Map it with ``(score + 1) / 2`` instead, clamped to+ ``[0, 1]``.++ The invariant both branches preserve: relevance is monotonically increasing+ in true similarity and bounded to ``[0, 1]``, so the nearest neighbour+ always gets the highest ``relevance_score``.+ """+ index = getattr(vector_store, "index", None)+ if getattr(index, "metric_type", None) == faiss.METRIC_INNER_PRODUCT:+ return max(0.0, min(1.0, (float(score) + 1.0) / 2.0))+ # L2 distance, or an index we cannot introspect: keep the historical+ # distance -> similarity mapping.+ return float(1.0 / (1.0 + float(score)))++class CollectionSearchEngine(LibraryRAGSearchEngine):"""Search engine for a specific document collection using RAG.@@ -242,7 +275,9 @@ class CollectionSearchEngine(LibraryRAGSearchEngine):"link": document_url,"source": "library","source_type": "library",- "relevance_score": float(1 / (1 + score)),+ "relevance_score": _relevance_from_faiss_score(+ vector_store, score+ ),"metadata": metadata,}results.append(result)diff --git a/tests/web_search_engines/engines/test_search_engine_collection.py b/tests/web_search_engines/engines/test_search_engine_collection.pyindex adca744597..a2210c3413 100644--- a/tests/web_search_engines/engines/test_search_engine_collection.py+++ b/tests/web_search_engines/engines/test_search_engine_collection.py@@ -621,6 +621,125 @@ class TestGetDocumentUrl:assert url == "/library/document/doc123/pdf"+class TestRelevanceFromFaissScore:+ """Regression tests for #4963: relevance_score must increase with true+ similarity and stay in [0, 1] for every FAISS index metric.++ ``similarity_search_with_score`` returns the raw score of the underlying+ index. For the default ``cosine`` (and ``dot_product``) metric that index+ is ``IndexFlatIP``, whose score is an inner product where *larger is+ nearer*; the old ``1 / (1 + score)`` mapping inverted that ranking and+ divided by zero for an anti-correlated pair.+ """++ def _fixed_embeddings(self, mapping):+ """A deterministic Embeddings that returns a preset vector per text."""+ from langchain_core.embeddings import Embeddings++ class _FixedEmbeddings(Embeddings):+ def __init__(self, table):+ self._table = table++ def embed_documents(self, texts):+ return [list(self._table[t]) for t in texts]++ def embed_query(self, text):+ return list(self._table[text])++ return _FixedEmbeddings(mapping)++ def _build_store(self, metric_index, mapping, texts, normalize):+ from langchain_community.docstore.in_memory import InMemoryDocstore+ from langchain_community.vectorstores import FAISS++ store = FAISS(+ self._fixed_embeddings(mapping),+ index=metric_index,+ docstore=InMemoryDocstore(),+ index_to_docstore_id={},+ normalize_L2=normalize,+ )+ store.add_texts(texts)+ return store++ def test_inner_product_index_keeps_ranking_and_bounds(self):+ """Cosine/IP index: nearest gets the highest relevance, all in [0, 1]."""+ import faiss++ from local_deep_research.web_search_engines.engines.search_engine_collection import (+ _relevance_from_faiss_score,+ )++ # 2D unit-ish vectors; query == "near" (cosine 1.0), "mid" ~0.6,+ # "far" is anti-correlated (cosine -1.0 -> old code divides by zero).+ mapping = {+ "query": (1.0, 0.0),+ "near": (1.0, 0.0),+ "mid": (0.6, 0.8),+ "far": (-1.0, 0.0),+ }+ store = self._build_store(+ faiss.IndexFlatIP(2),+ mapping,+ ["near", "mid", "far"],+ normalize=True,+ )+ docs_with_scores = store.similarity_search_with_score("query", k=3)+ rel = {+ doc.page_content: _relevance_from_faiss_score(store, score)+ for doc, score in docs_with_scores+ }++ assert all(0.0 <= r <= 1.0 for r in rel.values())+ # Monotonic in true similarity: near > mid > far. On main the mapping+ # inverts this (near 0.5 < mid ~0.62) and "far" raises ZeroDivisionError.+ assert rel["near"] > rel["mid"] > rel["far"]++ def test_l2_index_preserves_distance_mapping(self):+ """L2 index: unchanged 1/(1+distance), nearest highest, in (0, 1]."""+ import faiss++ from local_deep_research.web_search_engines.engines.search_engine_collection import (+ _relevance_from_faiss_score,+ )++ mapping = {+ "query": (0.0, 0.0),+ "near": (0.1, 0.0),+ "far": (5.0, 0.0),+ }+ store = self._build_store(+ faiss.IndexFlatL2(2),+ mapping,+ ["near", "far"],+ normalize=False,+ )+ docs_with_scores = store.similarity_search_with_score("query", k=2)+ rel = {+ doc.page_content: _relevance_from_faiss_score(store, score)+ for doc, score in docs_with_scores+ }++ assert all(0.0 < r <= 1.0 for r in rel.values())+ assert rel["near"] > rel["far"]++ def test_anticorrelated_ip_score_does_not_divide_by_zero(self):+ """A raw IP score of -1 maps to 0.0, not a ZeroDivisionError."""+ from unittest.mock import Mock++ import faiss++ from local_deep_research.web_search_engines.engines.search_engine_collection import (+ _relevance_from_faiss_score,+ )++ store = Mock()+ store.index.metric_type = faiss.METRIC_INNER_PRODUCT+ assert _relevance_from_faiss_score(store, -1.0) == 0.0+ assert _relevance_from_faiss_score(store, 1.0) == 1.0+ assert _relevance_from_faiss_score(store, 0.0) == 0.5++class TestClassAttributes:"""Tests for class attributes."""