Retrieval and reranking
Retrieval finds the passages an answer is built from. Scholar Lens combines vector search and full-text search, reranks the results with a cross-encoder, and picks a varied final set. This page explains each stage. The code is in app/retrieval/hybrid.py and app/ingestion/vector_store.py.
What is searched
A search runs over a scope:
- the Space's sources, each limited to its page ranges when it has a scope such as
9.1-9.6, - items of other users the Space shares, and items in the shared Library that the Space uses,
- files attached to this chat,
- the student's own notes on those documents.
With no Space sources, the scope is the student's whole Library. A shared item is only reachable through a Space that uses it.
All of these live in the chunks table, with a scope column of library, session or note. Only chunks of a document's live index_version are searched, so a re-index in progress is invisible.
The stages
Figure 1 shows the pipeline for one query. The answer graph runs it once per planned query.
flowchart LR Q[query] --> D[Dense<br/>pgvector cosine<br/>top 30] Q --> L[Lexical<br/>Postgres full text<br/>top 30] D --> F[Reciprocal rank fusion<br/>top 40] L --> F F --> R[Cross-encoder rerank] R --> M[MMR pick<br/>top k] M --> N[Add neighbouring chunks<br/>as context]
Figure 1. Hybrid search for one query.
- Dense search. The query is embedded with an instruction ("Given a student's question, retrieve passages from course materials that answer it"), because instruction-tuned embedders lose quality without it. pgvector finds the 30 nearest chunks by cosine distance, using an HNSW index on
halfvecembeddings. - Lexical search. Postgres full text finds the 30 best matches with
ts_rank_cd. Any query term may match, because requiring every term is too strict for a whole question. This catches exact terms, symbols and names that dense search misses. - Fusion. Reciprocal rank fusion merges the two lists: each passage scores
weight / (60 + rank)in each list it appears in. A lexical rank counts a quarter of a dense rank (RETRIEVAL_LEXICAL_WEIGHT), which measured best on the H&Z set. The top 40 go on, with scores scaled to 0 to 1. - Reranking. A cross-encoder (
bge-reranker-v2-m3in llama.cpp) scores each candidate against the query, from 0 to 1. It reads each passage with the same context header the embedder gets (title › section · page), because a passage often doesn't name its subject: without the header, a passage that answers "How does SIFT reject low-contrast keypoints?" word for word scored 0.04. Raw logits are mapped through a sigmoid. If the reranker fails, the fused order stands and the answer still runs. - MMR. Maximal marginal relevance picks the final passages one at a time, trading relevance against similarity to those already picked (
RETRIEVAL_MMR_LAMBDA, 0.75). This avoids eight copies of the same paragraph. - Neighbours. Each pick gets the chunks just before and after it in the same section as context (up to 3,000 characters), with the splitter's overlap removed. The model reads the context; the citation stays the chunk.
Every stage's score is kept on the passage (scores.dense, scores.lexical, scores.rrf, scores.rerank) and shown in the Sources tab.
Why the reranker matters
The reranker's scores are absolute: 0.8 means relevant, whatever the query. The fused scores are only relative. So three features need the reranker:
- coverage: a query is covered when its best passage scores at least 0.3,
- the evidence floor: passages under 0.1 are left out,
- High confidence: needs a best passage of at least 0.7.
Without a reranker, any hit counts as coverage and answers top out at Medium.
Web passages
Web pages are split into passages and scored by the same reranker against the question, so course and web passages compete on one scale. See Web research and sign-in.
Measuring retrieval
python -m evals.run --set <set> --owner <user> --retrieval-only measures recall@10 and MRR of the expected pages, with no model calls. The Retrieval test page in the Library shows the passages and scores for any query.