Skip to content

04 Retrieval and Ranking Math

Anurag Prasad edited this page Nov 29, 2026 · 1 revision

4. Retrieval & Ranking Math

The formulas that decide which documents an agent ever gets to read. If retrieval misses a document, no reasoning model can recover it.

4.1 Term weighting: TF-IDF

For term $t$, document $d$, corpus of $N$ documents with $n_t$ containing $t$:

$$\mathrm{tfidf}(t,d)=\mathrm{tf}(t,d)\cdot \log\frac{N}{n_t}$$

Intuition: frequent in the document, rare in the corpus ⇒ informative. Reference: Manning et al., IIR ch. 6.

4.2 BM25

The workhorse lexical ranker (Robertson & Zaragoza, 2009):

$$\mathrm{BM25}(q,d)=\sum_{t\in q}\mathrm{IDF}(t)\cdot\frac{f(t,d),(k_1+1)}{f(t,d)+k_1\left(1-b+b,\frac{|d|}{\mathrm{avgdl}}\right)}$$

$k_1$ controls term-frequency saturation; $b$ controls length normalisation; $\mathrm{avgdl}$ is mean document length.

4.3 PageRank

Link-based authority (Page, Brin, Motwani & Winograd, 1999). With damping $d$ (commonly 0.85), $N$ pages, in-links $B_u$ and out-degree $L(v)$:

$$PR(u)=\frac{1-d}{N}+d\sum_{v\in B_u}\frac{PR(v)}{L(v)}$$

It is the stationary distribution of a random surfer who follows a link with probability $d$ and teleports with probability $1-d$.

4.4 Dense retrieval

Encode query and passage into vectors; score by dot product (DPR, Karpukhin et al. 2020):

$$s(q,p)=E_Q(q)^\top E_P(p),\qquad P(p^+\mid q)=\frac{e^{s(q,p^+)}}{e^{s(q,p^+)}+\sum_{j}e^{s(q,p_j^-)}}$$

Late-interaction alternative: ColBERT, Khattab & Zaharia 2020.

Cosine similarity: $\cos(\mathbf{a},\mathbf{b})=\dfrac{\mathbf a\cdot\mathbf b}{\lVert\mathbf a\rVert,\lVert\mathbf b\rVert}$.

4.5 Hybrid fusion: Reciprocal Rank Fusion

Combine rankers without calibrating scores (Cormack, Clarke & Büttcher, 2009):

$$\mathrm{RRF}(d)=\sum_{r\in R}\frac{1}{k+\mathrm{rank}_r(d)},\qquad k\approx 60$$

This is exactly the problem a metasearch engine like SearXNG faces when merging engines (5).

4.6 Evaluation

$$\mathrm{Precision@}k=\frac{|\text{relevant}\cap\text{top-}k|}{k},\quad \mathrm{Recall@}k=\frac{|\text{relevant}\cap\text{top-}k|}{|\text{relevant}|}$$

$$\mathrm{DCG@}k=\sum_{i=1}^{k}\frac{2^{rel_i}-1}{\log_2(i+1)},\qquad \mathrm{nDCG@}k=\frac{\mathrm{DCG@}k}{\mathrm{IDCG@}k}$$

4.7 Agent-specific metrics (used in this project)

Unique yield per page: $y_p=|U_p\setminus\bigcup_{i<p}U_i|$. Target completion: $C=\dfrac{|U|}{T}$ with $T$ the target. In the deep run $C=216/500=43.2%$. Repeatability (Jaccard): for two runs with URL sets $A,B$

$$J(A,B)=\frac{|A\cap B|}{|A\cup B|}$$

Planned for Stage 2 (21); not yet measured.

Worked example from measured data — mean page yield (pages 1–15): $216/15=14.4$ unique URLs/page.


❓ Check your understanding

Q1. Why does BM25 saturate term frequency?

Because the fraction $\frac{f(k_1+1)}{f+k_1(\cdot)}$ approaches $k_1+1$ as $f\to\infty$: the 50th occurrence of a word adds almost nothing.

Q2. Two runs return 200 and 180 URLs with 150 in common. What is $J$?

$|A\cup B|=200+180-150=230$, so $J=150/230\approx0.652$.

Q3. Why use RRF instead of averaging scores?

Scores from different engines are on incompatible scales; ranks are comparable. RRF uses only ranks.

Q4. What is $PR(u)$ when $d=0$?

$PR(u)=1/N$ — pure teleportation, every page equal.

← Prev 3. Anatomy · Next → 5. Metasearch & SearXNG

Clone this wiki locally