The previous post covered the performance fight. This one goes inside the executor: how a query runs from Weight to Scorer to DocIdSetIterator, and what impacts, WAND and MAXSCORE each solve.

1. The execution skeleton

Lucene executes a query as a tree:

Query → Weight (reusable scoring logic)
      → Scorer (one per segment)
        → DocIdSetIterator (iterate docs) + score function
  • Weight: segment-independent “how to score”;
  • Scorer: segment-specific “which docs to score”;
  • DISI: only nextDoc() / advance(doc).

The naive execution: walk each clause’s postings, intersect/union, score each doc. Slow but correct.

2. impacts: block-level upper bounds

The first optimization is skipping. Lucene splits postings into blocks (128 docs by default) and records at each block header:

  • the maximum score inside the block;
  • a pointer to the next block.

That’s impacts. With it the executor can answer: “no document in this block can beat X — skip the whole block.”

advanceShallow(target) moves the current document to the start of the block containing target and loads its bound. A C++ port trap: advanceShallow must go through the full doAdvanceShallow (including the needsRefilling rewind), or block metadata desyncs.

3. WAND: pruning disjunctions with upper bounds

For A OR B OR C, WAND keeps three structures:

StructureRole
tail (max-score heap)current block upper bounds of all clauses
lead (candidate chain)candidates in doc order, ready to score
head (doc heap)clause iterators ordered by doc id

Each step:

  1. Take the current doc d, sum the upper bounds of clauses containing d → leadScore;
  2. Add tailMaxScore from the tail;
  3. If leadScore + tailMaxScore < minCompetitiveScore, skip the batch;
  4. Otherwise score d for real.

minCompetitiveScore comes from the Top-N collector: once N results are collected, the Nth score becomes the threshold, and candidates below it are dropped.

A practical detail: leadCost must be the disjunction’s own cost; getting it wrong weakens pruning.

4. MAXSCORE: another pruning strategy

MAXSCORE uses prefix sums instead of a heap: sort clauses by upper bound, compute remaining[i] = sum of the first i bounds. For doc d:

  • if prefixScore + remaining[i] < threshold, clauses after i need not be evaluated;
  • this can touch fewer iterators than WAND.

Lucene picks MAXSCORE when TOP_SCORES and minShouldMatch <= 1, otherwise WAND.

A real lesson: we tried porting MAXSCORE to C++ and it dropped documents on 3 fuzzy queries — a self-check reported 7 unjustified drops we couldn’t explain. We reverted and documented it, enabling it only for ≥3 clauses. That matters more than “looks faster”.

5. Conjunctions: two-phase advancing

A naive A AND B AND C drives from the shortest postings and advances the others to align. The optimized version is block-max conjunction:

  • compute the maximum possible score of the current block from each clause’s bound;
  • if the bound is below the threshold, skip the block;
  • otherwise advance clauses in cost order (cheap filters first).

6. Measurements

Same task set, 1M docs, Top-10:

StageQPS
Naive128
impacts + WAND~300
Two-phase conjunction + minCompetitiveScore propagation~450
Postings decode and hot-path tuning592 (Java 644)

p99 dropped from 37.9 ms to 18.3 ms — better than Java’s 22.7 ms.

7. Summary

  • The executor is Weight → Scorer → DISI; the optimization is touching fewer docs;
  • impacts provides block bounds and underpins WAND/MAXSCORE;
  • WAND uses heaps, MAXSCORE uses prefix sums, and Lucene chooses by query shape;
  • When porting pruning, dropping documents is worse than being slow — revert and document.

Get the next essay

New essays on products, business and tech — plus what the experiment's numbers actually say.