You're viewing the readable version of this site. The interactive extras (search, diagrams, read-aloud) need JavaScript and a current browser. Enable JavaScript; if it is already enabled, update your browser.

Learn · Search, Never Touch What Cannot Win

budding

The Score to Beat

Top-k search does not need every score. Once the answer set has a floor, a valid upper bound can prove that whole candidates are unable to enter it.

search, information-retrieval, top-k, wand, maxscore, algorithms, proofs, learn

“program testing can be used very efficiently to show the presence of bugs, but never to show their absence.”

— Edsger W. Dijkstra, Concern for Correctness as a Guiding Principle for Program Composition (EWD 288, July 1970)

This is the tenth chapter in a book about search from first principles. You will derive the score-to-beat invariant, trace the WAND algorithm over sorted postings, prove that each cursor advance is safe, and distinguish an upper bound from an estimate. You will also see why tighter bounds are not automatically faster. The next chapter will turn that warning into an economic question: when does the saved scoring work repay the cost of proving a skip?

A race with two medals

Suppose a query needs the best two documents. The straightforward plan is:

  1. find every matching document;
  2. compute every exact score;
  3. sort the scores;
  4. retain the first two.

That plan is correct, but it answers a larger question than the user asked. To identify the best two, the engine does not need the exact ordering of everyone below second place.

Keep the two best scores seen so far in a min-heap. Once the heap is full, let

θ=mindHS(d) \theta = \min_{d \in H} S(d)

be its smallest score. This is the score to beat. A new candidate can enter the heap only by scoring at least θ\theta; after a deterministic tie-break, one can make the comparison strict, but the conservative version is enough here.

Top-k — the kk highest-ranked items under a stated ordering. A min-heap of size kk exposes the current kth score at its root, so a better candidate can replace it in O(logk)O(\log k) time. Learn more.

The threshold alone saves nothing. We still need a cheap way to prove that a candidate cannot reach it.

A ceiling, not a guess

Assume the score is a sum of nonnegative query-term contributions:

S(d,q)=tqst(d),st(d)0. S(d,q) = \sum_{t \in q} s_t(d), \qquad s_t(d) \ge 0.

For each query term tt, precompute a value UtU_t such that

d,st(d)Ut. \forall d, \quad s_t(d) \le U_t.

Then any candidate that can match only a subset AA of the query terms obeys

S(d,q)tAUt. S(d,q) \le \sum_{t \in A} U_t.

If that sum is below θ\theta, exact scoring cannot change the top kk.

Upper bound — a value known to be at least as large as every value in its scope. It is a correctness contract, not a likely value. An overestimate loses pruning; an underestimate can lose the true winner. Broder and colleagues' WAND paper uses term-score upper bounds to guide evaluation.

Before continuing, decide which quantity may authorize a skip.

Prediction — separate a proof from a forecast. Only one statement is strong enough to discard a candidate without computing its exact score.

The license to skip is a proof obligation: the candidate's maximum possible score, under bounds that cover every omitted contribution, is below the current kth score.

Put the cursors on the corpus

Return to the eight documents introduced in The Back of the Book. Consider the disjunctive query

fox field index pages evidence

and suppose its scorer produces these nonzero contributions:

Term Postings as (document, contribution) Bound UtU_t
fox (1, 2.0), (2, 1.5) 2.0
field (1, 1.0), (2, 1.0) 1.0
index (3, 1.5), (6, 1.2) 1.5
pages (3, 1.4), (6, 1.1) 1.4
evidence (5, 1.2), (7, 1.0) 1.2

The exact nonzero document scores are therefore:

Document Score
1 3.0
2 2.5
3 2.9
5 1.2
6 2.3
7 1.0

A full evaluation scores all six candidates. WAND keeps one cursor per postings list and repeatedly orders the cursors by their current document ID. It accumulates term bounds from left to right until their sum reaches θ\theta. The cursor that crosses the threshold is the pivot.

WAND — “Weak AND,” a dynamic-pruning method introduced by Andrei Broder, David Carmel, Michael Herscovici, Aya Soffer, and Jason Zien. Its cursor and upper-bound test selects candidates for full evaluation without changing the exact result when the bounds are valid. Read the 2003 paper.

Here is the whole trace for k=2k=2. Before two results exist, take θ=0\theta=0.

Step Ordered current documents θ\theta Decision
1 fox 1, field 1, index 3, pages 3, evidence 5 0 all cursors before the pivot agree on 1; score document 1 as 3.0
2 fox 2, field 2, index 3, pages 3, evidence 5 0 score document 2 as 2.5; heap fills, so θ=2.5\theta=2.5
3 index 3, pages 3, evidence 5 2.5 1.5+1.4=2.91.5+1.4=2.9 reaches the threshold at document 3; score 3 as 2.9
4 evidence 5, index 6, pages 6 2.9 evidence alone is bounded by 1.2; advance it to pivot 6, proving document 5 cannot win
5 index 6, pages 6, evidence 7 2.9 1.5+1.4=2.91.5+1.4=2.9 reaches the threshold; score document 6 as 2.3
6 evidence 7 2.9 the remaining total bound is 1.2; stop, proving document 7 cannot win

The answer is documents 1 and 3, exactly as full evaluation would report. Four candidates were scored and two were rejected by proof.

The pivot algorithm

The following form assumes increasing document IDs, additive nonnegative scores, valid per-term upper bounds, and a heap whose threshold is zero until it contains kk results.

WAND evaluation with term-level upper bounds

WAND-TOP-K(I, U, k)
Input:  postings iterators I[1:m], valid bounds U[1:m], result count k
Output: the k highest-scoring documents under the exact additive scorer

H  empty min-heap
while at least one iterator is not exhausted
    order live iterators by their current document ID
    θ  0 if H has fewer than k items, otherwise H.MIN-SCORE
    bound  0
    pivot  NIL
    for each iterator i in order
        bound  bound + U[i]
        if bound  θ
            pivot  i
            break
    if pivot = NIL
        break
    p  pivot.CURRENT-DOCUMENT
    if order[1].CURRENT-DOCUMENT = p
        score  0
        for each iterator i whose current document is p
            score  score + i.CURRENT-CONTRIBUTION
            i.ADVANCE()
        RETAIN-IF-TOP-K(H, (p, score), k)
    else
        order[1].ADVANCE-TO(p)
return H in descending result order

Sorting mm cursors on every turn makes this presentation easy to inspect but is not the only implementation. The interesting operation is ADVANCE-TO(p): a postings iterator can seek to the first document ID at least pp, often using skip data. The algorithm's benefit depends on how many exact scores and postings advances it avoids, not on the spelling of the cursor container.

Why the advance is sound

The dangerous line is the one that advances the first iterator. It may jump over document IDs without scoring them. We need more than a successful trace.

At the start of each turn, order live iterators by current document ID. Let pp be the first pivot document, and let PP be the iterators before the pivot. By the definition of “first pivot,”

iPUi<θ. \sum_{i \in P} U_i < \theta.

Now consider any document dd skipped when the first iterator advances to pp. Because d<pd < p, every iterator at pp or later is already past dd and cannot contain it. Thus dd can receive contributions only from iterators in PP. Validity of the bounds gives

S(d,q)iPUi<θ. S(d,q) \le \sum_{i \in P} U_i < \theta.

So dd cannot enter the heap. Advancing past it preserves the top kk.

If no pivot exists, even the sum of every remaining bound is below θ\theta. The same argument covers every unseen document, so termination is safe. If all iterators before the pivot already point to pp, the algorithm does not skip: it computes pp's exact score and updates the heap. These cases exhaust every turn, which proves that the returned heap matches exhaustive evaluation.

Reconstruction — identify the missing premise. The inequality is useful only if its bounds cover all contributions the skipped document could still receive.

What the proof costs

With mm query terms, exhaustive document-at-a-time evaluation visits the union of their postings and computes every candidate's exact score. The teaching algorithm adds cursor ordering and bound additions. If it re-sorts cursors each turn, that bookkeeping can cost O(mlogm)O(m \log m) per turn; practical variants keep the small cursor set ordered more carefully. The heap costs O(logk)O(\log k) for an accepted replacement and O(k)O(k) space.

No single asymptotic expression promises a speedup. On the trace above:

Work item Exhaustive WAND trace
exact document scores 6 4
candidates rejected by a bound 0 2
bound additions 0 9
heap capacity 2 2

WAND paid nine cheap additions to avoid two exact scores. Whether that wins in time depends on the scorer, postings layout, bound tightness, cache behavior, and query. The next chapter measures this crossover instead of assuming it.

Four wrong turns

Treat a prediction as a bound

A learned model says a candidate will probably score 2.4. The threshold is 2.9, so the engine skips it. This is approximate retrieval, not WAND's exact contract: the candidate may really score 3.1. A prediction becomes a safe bound only with a separately proved error envelope that covers the candidate.

Round a bound downward

The true maximum contribution is 1.204, stored as 1.2 to save space. That tiny underestimate can invalidate the proof. Bounds must round outward. A looser 1.21 is safe and may skip less; 1.20 is unsafe if the contract is real-valued.

Freeze the initial threshold

At the start, θ=0\theta=0, so almost nothing can be pruned. The useful threshold is the live kth score, which rises as better candidates arrive. Candidate order therefore affects work even though it must not affect the final answer.

Assume tighter is always faster

Per-block or per-impact bounds may reject more candidates, but reading and checking their metadata costs work. On short postings or homogeneous scores, the proof can cost more than the score it avoids. Correctness is monotone in bound looseness; performance is not monotone in bound detail.

Where the law stops

The proof above relies on additive nonnegative contributions and bounds that cover the exact scorer. Negative contributions, cross-term interactions, query-dependent features, and a later reranker need their own envelopes. One safe design retrieves a generous candidate set with bounded additive scores, then applies the unconstrained model only to those candidates. That preserves the first stage's theorem; it does not prove that the cascade equals ranking the entire collection with the later model.

MaxScore, introduced by Howard Turtle and James Flood, reaches the same broad goal with a different organization: terms whose maximum contributions cannot change the current top kk become nonessential. WAND and MaxScore should not be collapsed into one folklore algorithm. They are distinct ways to spend bounds against a threshold. The useful common abstraction is the proof obligation.

Transfer — separate a safe ceiling from a useful guess.

Lessons

  • Top-k retrieval needs a threshold: the current kth score.
  • A valid upper bound is a ceiling, not an estimate or average.
  • Sorted postings prove that iterators at the pivot or later cannot contribute to earlier document IDs.
  • An underestimate can change the answer; an overestimate only loses work.
  • Dynamic pruning exchanges scoring work for proof work, so correctness alone does not establish a speedup.

Practice

  1. Retrieval. Without looking back, state the inequality that licenses a pre-pivot cursor advance.
  2. Discrimination. Give one quantity that predicts a score well but is not a valid upper bound.
  3. Trace. Set k=1k=1 in the six-candidate table. Recompute every threshold, pivot, exact score, and skip.
  4. Repair. Suppose one query feature subtracts a penalty. State what must change in the bound before the proof applies.
  5. Transfer. Name a non-search top-k system. Identify its score to beat, an admissible upper bound, and the cost of checking that bound.

The remaining question is economic. This chapter proved that skipping is safe; it did not prove that skipping is cheap. The Arithmetic of Skipping asks when a finer proof refunds more work than it consumes.

References

  1. Broder, Carmel, Herscovici, Soffer, Zien. “Efficient Query Evaluation Using a Two-Level Retrieval Process.” CIKM 2003, 2003. — introduces WAND and its term-score upper-bound evaluation
  2. Turtle and Flood. “Query Evaluation: Strategies and Optimizations.” Information Processing & Management 31(6), 1995. — introduces MaxScore's organization of essential and nonessential terms
  3. Edsger W. Dijkstra. “Concern for Correctness as a Guiding Principle for Program Composition.” EWD 288, 1970. — primary source for the epigraph and its argument about what evidence can establish