Building an inverted index means sorting $T$ (term, docID) pairs under a memory limit. A disk reads sequentially several orders of magnitude faster than at scattered positions, so every standard method sorts in memory and touches the disk only in sequential passes. Blocked sort-based indexing (BSBI) sorts one block of pairs at a time and merges, in $\Theta(T \log T)$. Single-pass in-memory indexing (SPIMI) gives each block its own dictionary, builds the blocks in $\Theta(T)$, and sorts only each block's terms. MapReduce spreads the work across machines. For a growing collection, logarithmic merging writes each posting at most $\lfloor \log_2 (T/n) \rfloor + 1$ times, against $\Theta(T^2/n)$ in all for an auxiliary index: on Reuters-21578, 4.5 million postings written against 26.4 million, with $n = 10{,}000$ in memory.
The inverted index article built an index for the thesis student's four Arctic sentences by collecting their 22 (term, docID) pairs, sorting them, and grouping the result. Reuters-21578 produces 725,793 such pairs. The Reuters RCV1 collection used throughout Manning, Raghavan, and Schütze's textbook has about 800,000 documents and about 100 million pairs. At four bytes for each term identifier and four for each docID, that is 800 megabytes of pairs to sort, before positions or frequencies are added. A web collection has thousands of times as many. The sort that took no time for four sentences becomes the central engineering problem of indexing. How do we sort and group far more data than fits in memory? In outline: work on one memory-sized block at a time, write each block's partial index to disk in a single sequential pass, and merge the blocks at the end.
This article describes the standard forms of that answer, following Manning, Raghavan, and Schütze (2008, ch. 4). Here is the route. First we see why the speed of a disk depends on the order in which it is read, a fact that shapes every algorithm here. Then come two ways to build an index on one machine, blocked sort-based indexing and single-pass in-memory indexing, each traced on the Arctic sentences and measured on Reuters-21578. Next we spread the work across many machines with MapReduce. Finally we keep an index current as documents arrive, and simulate the cost of two ways of doing so.
Why access patterns matter
Index construction is shaped by one fact about storage: reading data sequentially is far faster than reading it at scattered positions. On a spinning disk, moving the read head to a new position takes several milliseconds. In that time, tens or hundreds of kilobytes could have been read sequentially. Solid-state drives reduce the gap but do not eliminate it, and memory is faster still.
How large is the gap? Assume, for illustration, a disk that needs 5 milliseconds per seek and then transfers 100 megabytes per second.
- Reading RCV1's 800 megabytes of pairs sequentially takes $800 / 100 = 8$ seconds.
- Fetching the same 100 million pairs one at a time from random positions takes $10^{8} \times 5\ \text{ms} = 5 \times 10^{5}$ seconds, nearly six days.
The course text's own system parameters (its Table 4.1) are a 5 millisecond seek and 0.02 microseconds to transfer a byte, which is 50 megabytes per second. With those the sequential read takes $8 \times 10^{8} \times 2 \times 10^{-8} = 16$ seconds, and the random reads take the same $5 \times 10^5$ seconds. The exact figures vary from device to device, but the ratio of several orders of magnitude does not. An algorithm that sorted 100 million pairs on disk in random order would spend nearly all its time seeking. Efficient construction algorithms therefore do as much work as possible in memory. They write data to disk in large sequential blocks and read it back sequentially.
Blocked sort-based indexing
The simplest way to obey that rule is to keep the sorting method and apply it to one memory-sized piece at a time. Blocked sort-based indexing (BSBI) works this way (Manning, Raghavan, and Schütze, 2008, §4.2):
- Divide the collection into blocks small enough that each block's pairs fit in memory.
- For each block:
- parse the documents into (termID, docID) pairs, using a dictionary that maps each term to an integer termID;
- sort the pairs in memory;
- group them into postings lists for that block, and write the block's partial index to disk.
- Merge the block indexes into the final index.
The merge reads all block files simultaneously, a small buffer from each, and repeatedly takes the smallest termID among the buffers. A priority queue finds that smallest termID efficiently. Postings for the same term from different blocks are concatenated. That keeps them in docID order, because the blocks cover increasing ranges of docIDs. Every block file is read once, sequentially, and the final index is written once, sequentially.
For RCV1, Manning, Raghavan, and Schütze describe ten blocks of about ten million pairs each.
What BSBI costs
Here is the idea: every pair is parsed once, sorted once inside its block, and copied once by the merge, and only the sort costs more than a constant per pair.
The count. Let $T$ be the number of pairs, $n$ the number of pairs in a block, and $B = \lceil T/n \rceil$ the number of blocks. Let $E$ be the number of (term, block) entries, that is, the number of postings lists in all the block indexes together. Every such list holds at least one pair, so $E \le T$.
- Parsing. Each pair needs one lookup in the term-to-termID map. With a hash table that is constant expected time, so parsing takes $\Theta(T)$ operations.
- Sorting. A comparison sort of a block of $n$ pairs takes $\Theta(n \log n)$ comparisons. Over $B$ blocks that is $\Theta(Bn \log n) = \Theta(T \log n)$.
- Merging. The priority queue holds one entry per block, at most $B$. Each of the $E$ entries is pushed once and popped once, at $O(\log B)$ comparisons each, and each of the $T$ postings is copied once. The merge takes $O(E \log B + T)$.
The total is $\Theta(T \log n) + O(E \log B + T)$. Since $n \le T$, $B \le T$ and $E \le T$, this is $O(T \log T)$. When the number of blocks is fixed, as with the ten blocks of RCV1, $n = T/B$ and $\log n = \log T - \log B$, so the sorting term alone is $\Theta(T \log T)$. That is the course text's statement: BSBI has time complexity $\Theta(T \log T)$, because sorting is the step of highest complexity and $T$ bounds the number of items sorted (Manning, Raghavan, and Schütze, 2008, §4.2). The text adds that the time actually measured is usually dominated by parsing the documents and by the final merge, which are the steps that read and write the disk.
BSBI on the Arctic sentences
The four sentences make the steps concrete. After the analysis of the inverted index article (lowercasing, the eight stop words removed, Porter stemming), they are:
| docID | Terms |
|---|---|
| 1 | arctic sea ic declin climat warm |
| 2 | warm climat threaten arctic seabird food |
| 3 | climat model predict warmer winter europ |
| 4 | seabird arctic field guid |
Suppose memory holds only about a dozen pairs, so the collection is processed in two blocks of two documents. BSBI first needs termIDs. Assigning them in order of first appearance gives:
| Term | arctic | sea | ic | declin | climat | warm | threaten | seabird |
|---|---|---|---|---|---|---|---|---|
| termID | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| Term | food | model | predict | warmer | winter | europ | field | guid |
|---|---|---|---|---|---|---|---|---|
| termID | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
This map is global. When block 2 meets arctic, it must use the same number, 1, that block 1 used. Each block's pairs are written as (termID, docID), first as parsed, then after sorting:
Block 1 (docs 1, 2), as parsed:
(1,1) (2,1) (3,1) (4,1) (5,1) (6,1) (6,2) (5,2) (7,2) (1,2) (8,2) (9,2)
sorted:
(1,1) (1,2) (2,1) (3,1) (4,1) (5,1) (5,2) (6,1) (6,2) (7,2) (8,2) (9,2)
Block 2 (docs 3, 4), as parsed:
(5,3) (10,3) (11,3) (12,3) (13,3) (14,3) (8,4) (1,4) (15,4) (16,4)
sorted:
(1,4) (5,3) (8,4) (10,3) (11,3) (12,3) (13,3) (14,3) (15,4) (16,4)
Grouping each sorted block gives its partial index, which is written to disk:
- Block 1: 1 → 1, 2; 2 → 1; 3 → 1; 4 → 1; 5 → 1, 2; 6 → 1, 2; 7 → 2; 8 → 2; 9 → 2.
- Block 2: 1 → 4; 5 → 3; 8 → 4; 10 → 3; 11 → 3; 12 → 3; 13 → 3; 14 → 3; 15 → 4; 16 → 4.
The merge keeps a priority queue holding the next termID from each block. It pops the smallest, appends that block's postings for the term to the output, and pushes the block's following termID. The first pop is termID 1 from block 1, giving the list (1, 2). The second is termID 1 from block 2, extending it to (1, 2, 4). TermIDs 2, 3, and 4 occur only in block 1 and are copied straight through. TermID 5 then grows from (1, 2) to (1, 2, 3).
Example (counting the pops). The whole merge takes nineteen pops: one per (term, block) entry, nine from block 1 and ten from block 2. This is the quantity $E$ of the cost analysis. Only three termIDs, 1 (arctic), 5 (climat), and 8 (seabird), take two pops, because only they occur in both blocks. The final index:
| termID | Term | Postings |
|---|---|---|
| 1 | arctic | 1, 2, 4 |
| 2 | sea | 1 |
| 3 | ic | 1 |
| 4 | declin | 1 |
| 5 | climat | 1, 2, 3 |
| 6 | warm | 1, 2 |
| 7 | threaten | 2 |
| 8 | seabird | 2, 4 |
| 9 | food | 2 |
| 10–14 | model, predict, warmer, winter, europ | 3 each |
| 15–16 | field, guid | 4 each |
The postings are the same as those of the in-memory sort, but the lists come out in termID order, not alphabetical order. A dictionary sorted by term, needed for prefix search, is a separate step. Notice also that the merge never compared docIDs. Every posting of block 1 has a smaller docID than every posting of block 2, so appending block 2's postings after block 1's keeps every list sorted.
At a larger scale, the same procedure runs on Reuters-21578 (Lewis, 1997), tokenized as in the term statistics article. Its 725,793 distinct (term, document) pairs, in blocks of about 100,000 pairs, cut at story boundaries, produce eight block files and an eight-way merge. Each pop from the priority queue then costs about $\log_2 8 = 3$ comparisons. In the terms of the cost analysis, $T = 725{,}793$, $B = 8$ and $E = 80{,}987$. Sorting the eight blocks of pairs takes about $\sum n \log_2 n \approx 12.0$ million comparisons, and the merge's priority queue about $E \log_2 B \approx 0.24$ million. The sort is the dominant count, as the analysis says.
BSBI has a weakness: it needs the mapping from terms to termIDs for the whole collection, kept in memory throughout the build. For a very large collection that dictionary may itself be too large. And, as Heaps' law predicts, it keeps growing as long as new documents arrive.
Single-pass in-memory indexing
Can we drop the global map? Single-pass in-memory indexing (SPIMI), described by Heinz and Zobel (2003), avoids both the global dictionary and the sort of pairs. Here is the idea in one sentence: give each block its own dictionary of real terms, and append each posting straight to its term's list instead of sorting pairs.
- Process the documents as a stream. For each token, look up its term in a dictionary for the current block only. If the term is new to the block, create an empty postings list for it. Append the docID to the term's postings list. If the list already ends with that docID, increment only the posting's term frequency, if one is stored.
- When memory is full, sort the block's terms (not its pairs), write the block's dictionary and postings to disk in term order, and start a new block with an empty dictionary.
- Merge the blocks as in BSBI.
Each block has its own dictionary of actual terms, so no global termID mapping is needed, and blocks can be arbitrarily large given enough memory. Postings are appended directly to the list for their term, so the pairs never need sorting. Only each block's distinct terms are sorted, and there are far fewer of those. Documents are processed in docID order, so each list is already sorted by docID. Postings can also be compressed as blocks are written, which reduces the disk traffic of the merge.
What SPIMI costs
The count has the same three parts as for BSBI, with $T$ now the number of tokens, $m_b$ the number of distinct terms in block $b$, and $E = \sum_b m_b$ the number of dictionary entries written.
- Building the blocks. Each token costs one lookup in the block's hash dictionary and at most one append to a postings list. A list that doubles its allocation when full makes $k$ appends cost $O(k)$ in total, because the copies made at the doublings add up to less than $2k$. So building all the blocks takes $\Theta(T)$ expected operations.
- Sorting the terms. Block $b$ sorts its $m_b$ terms, $\Theta(m_b \log m_b)$ comparisons. Over all blocks that is at most $E \log m_{\max}$, where $m_{\max}$ is the largest block dictionary.
- Merging. As in BSBI, $O(E \log B + T)$.
The total is $\Theta(T) + O(E \log m_{\max} + E \log B)$. The course text states SPIMI's time complexity as $\Theta(T)$, on the grounds that no sorting of tokens is required and that all operations are at most linear in the size of the collection (Manning, Raghavan, and Schütze, 2008, §4.3). That holds whenever $E \log (m_{\max} B)$ is $O(T)$, which is the usual case, because vocabularies grow much more slowly than collections (Heaps' law).
Example (a common misstatement). "SPIMI runs in linear time" is true of building the blocks and of typical collections, and it is not a worst-case bound for the whole algorithm. If every token were a new term, then $E = T$, and sorting the terms of the blocks would cost $\Theta(T \log n)$, exactly BSBI's sorting cost. What SPIMI removes is the sorting of pairs: it sorts each block's distinct terms instead. On Reuters-21578 in the eight blocks measured below, the term sorts take about $\sum m_b \log_2 m_b \approx 1.08$ million comparisons, against about 12.0 million for BSBI's pair sorts, an eleventh as many.
SPIMI on the same sentences
With the same two blocks, SPIMI never assigns termIDs. Block 1 starts with an empty dictionary and reads document 1. Arctic, sea, ic, declin, climat, and warm are all new to the block, so each gets a list containing 1. Document 2 then appends 2 to the existing lists of warm, climat, and arctic, and creates new lists for threaten, seabird, and food. When memory is full, the block's nine terms, not its twelve postings, are sorted, and the block is written in term order:
| Block 1 (docs 1, 2) | Block 2 (docs 3, 4) |
|---|---|
| arctic → 1, 2 | arctic → 4 |
| climat → 1, 2 | climat → 3 |
| declin → 1 | europ → 3 |
| food → 2 | field → 4 |
| ic → 1 | guid → 4 |
| sea → 1 | model → 3 |
| seabird → 2 | predict → 3 |
| threaten → 2 | seabird → 4 |
| warm → 1, 2 | warmer → 3 |
| winter → 3 |
Block 2 builds a fresh dictionary that knows nothing of block 1. All ten of its terms are new to it, including arctic, climat, and seabird, which block 1 has already seen. Only the other seven are new to the collection. The merge then works exactly as in BSBI, comparing terms instead of termIDs. It produces the same postings, this time already in alphabetical order. The only sort was of the terms of each block.
Example (dictionary entries written). The collection has 16 distinct terms, and the two SPIMI blocks write nineteen dictionary entries between them, nine plus ten. The three terms that occur in both blocks are written twice. That is the price of forgetting the vocabulary between blocks: a term occurring in several blocks has a dictionary entry in each.
How large is that price on a real collection? Measured on Reuters-21578 in blocks of about 100,000 postings:
| Block | Postings | Terms in this block (SPIMI) | Terms seen so far (BSBI's map) |
|---|---|---|---|
| 1 | 100,129 | 10,851 | 10,851 |
| 2 | 100,002 | 10,564 | 15,252 |
| 3 | 100,061 | 10,972 | 18,815 |
| 4 | 100,223 | 10,623 | 21,529 |
| 5 | 100,113 | 10,793 | 24,046 |
| 6 | 100,052 | 10,762 | 26,228 |
| 7 | 100,045 | 11,342 | 28,548 |
| 8 | 25,168 | 5,080 | 29,187 |
Each SPIMI block holds a dictionary of about 11,000 terms, roughly constant from block to block. The map BSBI must keep in memory grows with the collection instead, as Heaps' law predicts, to 29,187 terms. Summed over the blocks, SPIMI writes 80,987 dictionary entries, 2.77 times the number of distinct terms, and the merge combines them. For this small collection either approach fits easily in memory. The difference matters when the global vocabulary runs to hundreds of millions of terms, while each block's stays bounded by the block size.
The algorithm is simple enough that most straightforward indexers are essentially SPIMI without the block flushing. That includes the indexers built on a dictionary of lists or sets in course projects. They work until the collection outgrows memory. The pipeline article discusses this in the context of my own homework.
Distributed indexing
What if one machine, however well it uses its disk, is still too slow? Then construction is distributed. Manning, Raghavan, and Schütze (2008, §4.4) describe indexing with MapReduce (Dean and Ghemawat, 2004), a framework that runs a computation in two phases across a cluster:
- In the map phase, many parsers each process a portion of the collection, called a split. Each parser emits (term, docID) pairs and writes them to local files called segments, partitioned by term range. For example, with three partitions, terms beginning a to f go to one segment, g to p to a second, and q to z to a third.
- In the reduce phase, each inverter is assigned one term range. It collects that range's segment files from every parser, sorts the pairs, and writes the postings for its terms.
On the Arctic sentences, with two parsers (documents 1 and 2, and documents 3 and 4) and the three term ranges above:
| Range | Parser 1 | Parser 2 | Inverter output |
|---|---|---|---|
| a–f | (arctic,1) (declin,1) (climat,1) (climat,2) (arctic,2) (food,2) | (climat,3) (europ,3) (arctic,4) (field,4) | arctic → 1, 2, 4; climat → 1, 2, 3; declin → 1; europ → 3; field → 4; food → 2 |
| g–p | (ic,1) | (model,3) (predict,3) (guid,4) | guid → 4; ic → 1; model → 3; predict → 3 |
| q–z | (sea,1) (warm,1) (warm,2) (threaten,2) (seabird,2) | (warmer,3) (winter,3) (seabird,4) | sea → 1; seabird → 2, 4; threaten → 2; warm → 1, 2; warmer → 3; winter → 3 |
Each inverter reads one row, from every parser, and sorts only that row's pairs. The rows are uneven: ten pairs in the first range against four in the middle one. Ranges split by first letter are rarely balanced, because letters begin very different numbers of words and terms differ enormously in frequency. Ranges chosen so that the partitions carry similar amounts of work are better, since the job finishes only when the slowest inverter does.
The framework handles scheduling, moves data between machines, and restarts tasks that fail. Partitioning by term range produces an index divided by term. As described in search at scale, large search engines usually serve indexes divided by document instead. The same framework can produce document-partitioned indexes by partitioning on docID, or a term-partitioned index can be reorganized after construction.
Keeping the index current
Every method so far builds an index once. But collections change. Documents are added, deleted, and modified, and an index built once and never updated soon becomes stale. Rebuilding from scratch is simple but slow, and a news or web index cannot wait. Manning, Raghavan, and Schütze (2008, §4.5) describe several schemes.
The auxiliary index
Keep the large main index on disk and a small auxiliary index in memory.
- New documents are indexed into the auxiliary index.
- Queries are run against both indexes, and their results are combined.
- Deleted documents are recorded in an invalidation bit vector, one bit per docID, and filtered out of results.
- A modified document is treated as a deletion followed by an addition.
When the auxiliary index fills memory, it is merged into the main index. The cost of that merge is the problem. Here is the idea in one sentence: each merge rewrites the whole main index, and the main index keeps growing, so the total grows with the square of the collection. In symbols, let the auxiliary index hold $n$ postings and let the collection eventually reach $T$ postings, so that there are $j = T/n$ merges. The $k$th merge writes a main index of $kn$ postings. The total written is
$$n + 2n + \cdots + jn = n\,\frac{j(j+1)}{2} = \frac{T^2}{2n} + \frac{T}{2},$$
which is $\Theta(T^2/n)$, the order the course text gives (Manning, Raghavan, and Schütze, 2008, §4.5). For a large and growing collection that is too slow.
Logarithmic merging
Instead of a single main index, keep a series of indexes of sizes $n$, $2n$, $4n$, $8n$, and so on, with at most one of each size. When the in-memory index fills, write it to disk as an index of size $n$. If an index of that size already exists, merge the two into one of size $2n$. If one of size $2n$ already exists, merge again into one of size $4n$, and so on. The pattern is the carrying of digits when 1 is added to a binary number.
The total work falls to $\Theta(T \log(T/n))$, because each posting is processed once on each of at most $\log_2(T/n)$ levels (Manning, Raghavan, and Schütze, 2008, §4.5). The price is that a query must consult up to $\log_2(T/n)$ indexes instead of two. Lester, Moffat, and Zobel (2005) analyzed a family of such geometric partitioning schemes, which let a system trade update cost against query cost. Updating an index derives that trade for any size ratio between levels, and the cost of deletion.
The bound, proved. Write $I_i$ for the on-disk index of size $2^i n$, and count a posting as written each time it is part of an index or an intermediate merge result written to disk.
Claim. A posting that is in $I_i$ has been written at most $i + 1$ times.
Proof. By induction on the flushes. An index $I_0$ created by flushing memory alone holds postings written once, and $0 + 1 = 1$. Otherwise the flush finds $I_0, I_1, \ldots, I_{i-1}$ all present and $I_i$ absent. It merges memory with $I_0$ and writes the result, merges that with $I_1$ and writes the result, and so on, $i$ merges in all, the last of which is the new $I_i$. A posting from memory is written by all $i$ merges, $i$ times. A posting from $I_m$, for $m < i$, had been written at most $m + 1$ times by the induction hypothesis. It joins at the merge with $I_m$ and is written by that merge and the $i - m - 1$ after it, so its count is at most $(m + 1) + (i - m) = i + 1$. $\blacksquare$
An index $I_i$ exists only if $2^i n \le T$, so $i \le \log_2(T/n)$. Every posting is therefore written at most $\lfloor \log_2 (T/n) \rfloor + 1$ times, and the total written is at most $T(\lfloor \log_2 (T/n) \rfloor + 1)$. The claim also bounds the count by the number of flushes. The index $I_i$ is first created at flush $2^i$, so after $j$ flushes every index present has $i \le \log_2 j$, and each posting has been written at most $\lfloor \log_2 j \rfloor + 1$ times. A query consults one index for each 1 in the binary form of $j$, which is also at most $\lfloor \log_2 j \rfloor + 1$.
The first few flushes show the binary-counter behavior. Write $I_0, I_1, I_2, \dots$ for on-disk indexes of sizes $n, 2n, 4n, \dots$:
| Flush | Before | Merges | After | Postings written | Indexes on disk |
|---|---|---|---|---|---|
| 1 | none | none | $I_0$ | $n$ | 1 |
| 2 | $I_0$ | memory + $I_0$ | $I_1$ | $2n$ | 1 |
| 3 | $I_1$ | none | $I_0, I_1$ | $n$ | 2 |
| 4 | $I_0, I_1$ | memory + $I_0$, then + $I_1$ | $I_2$ | $2n + 4n = 6n$ | 1 |
| 5 | $I_2$ | none | $I_0, I_2$ | $n$ | 2 |
| 6 | $I_0, I_2$ | memory + $I_0$ | $I_1, I_2$ | $2n$ | 2 |
| 7 | $I_1, I_2$ | none | $I_0, I_1, I_2$ | $n$ | 3 |
| 8 | $I_0, I_1, I_2$ | three carries | $I_3$ | $2n + 4n + 8n = 14n$ | 1 |
The set of indexes present after flush $j$ matches the binary digits of $j$: after flush 6, binary 110, there are indexes of sizes $2n$ and $4n$.
Example (the indexes after flush 11). After flush 11 the indexes on disk are $I_0$, $I_1$, and $I_3$, of sizes $n$, $2n$, and $8n$, because 11 is binary 1011. A query at that moment consults three indexes plus the one in memory.
The total written after eight flushes is $28n$. The auxiliary scheme, rewriting the whole main index at each flush, writes $n + 2n + \dots + 8n = 36n$ over the same eight flushes. The gap is small at first and widens quickly. After $j$ flushes the auxiliary scheme has written $n\,j(j+1)/2$ postings, which grows with the square of $j$.
The logarithmic total has a closed form when $j$ is a power of two. The table shows the cost of each flush: an odd flush writes $n$, and a flush whose number ends in $v \ge 1$ binary zeros makes $v$ carries and writes $2n + 4n + \cdots + 2^v n = (2^{v+1} - 2)n$. Among the flushes $1, \ldots, 2^k$ with $k \ge 1$:
- $2^{k-1}$ are odd, writing $2^{k-1} n$ in all;
- for each $v$ from 1 to $k - 1$, $2^{k-v-1}$ flushes end in exactly $v$ zeros, writing $2^{k-v-1}(2^{v+1} - 2)n = (2^k - 2^{k-v})n$ in all;
- one flush, the last, ends in $k$ zeros and writes $(2^{k+1} - 2)n$.
The middle group sums to $\left((k-1)2^k - (2^k - 2)\right)n$, using $\sum_{v=1}^{k-1} 2^{k-v} = 2^k - 2$. Adding the three groups,
$$2^{k-1}n + \left((k-2)2^k + 2\right)n + \left(2^{k+1} - 2\right)n = 2^k\left(k + \tfrac{1}{2}\right)n.$$
For $k = 3$ that is $8 \times 3.5\,n = 28n$, the table's total. With $T = 2^k n$ postings indexed, the total is $T(\log_2 (T/n) + \tfrac12)$: each posting is written $\log_2(T/n) + \tfrac12$ times on average, inside the bound of $\log_2(T/n) + 1$ proved above. The script checks the closed form, the cost of every flush and the bound for every $j$ up to 4,096.
Both schemes on Reuters-21578
Simulating both on Reuters-21578 makes the difference concrete. The stories are added in order, each contributing one posting per distinct term, with an in-memory index of $n = 10{,}000$ postings. The 725,793 postings then cause 72 flushes:
Both totals agree with the analysis.
- The auxiliary scheme writes about $P^2 / 2n$ postings for a final index of $P$ postings, here $725{,}793^2 / 20{,}000 \approx 26.3$ million.
- Logarithmic merging writes each posting about once per level it passes through, roughly $P \log_2(P/n) \approx 4.5$ million in total (here 4.47 million). The proved bound holds as well: with 72 flushes no posting is written more than $\lfloor \log_2 72 \rfloor + 1 = 7$ times, a ceiling of $7P = 5.08$ million.
Raising $n$ to 50,000 reduces the auxiliary scheme to 5.3 million postings written and the logarithmic one to 2.1 million. More memory helps both, but it does not change the quadratic shape. In both simulations a merge's cost is counted as the number of postings it writes, including every intermediate result, as in the table above.
This is how widely used systems work. Lucene writes new documents into small immutable segments and searches all current segments together. It marks deleted documents in a per-segment bitmap and merges segments in the background, according to a policy that groups segments of similar size (Apache Software Foundation, Lucene documentation for IndexWriter and TieredMergePolicy). That is logarithmic merging with refinements. A segment's postings are never modified once written. Readers can therefore search a fixed snapshot of the segments without locks while new segments are being written, and reopen the index to see newer ones. The same library stores its vector indexes for nearest-neighbor search segment by segment and builds the merged segment's vector index when segments merge, so the scheme of this section carries over to retrieval with learned embeddings. For those vector indexes the question of the first section, what must stay in memory and what can be read from disk, returns in Vector Search on Disk and with Filters, which stores a nearest-neighbor graph on a solid-state drive.
Deletions and statistics. Deleted documents still occupy space, and their postings still count toward term statistics, until a merge physically removes them. Collection statistics such as document frequency and average document length are therefore approximate between merges. When deletions are few, the error is small. A collection with heavy deletion should be merged regularly so that its statistics stay accurate.
Summary
| Method | Main idea | Running time | Needs in memory |
|---|---|---|---|
| In-memory sort | Sort all (term, docID) pairs at once | $\Theta(T \log T)$ | All pairs |
| BSBI | Sort pairs block by block, then merge | $\Theta(T \log T)$ | One block of pairs and the global term-to-termID map |
| SPIMI | Append postings to per-block term lists, then merge | $\Theta(T)$ to build the blocks, plus sorting each block's terms and the merge | One block's dictionary and postings |
| MapReduce | Parse in parallel, partition by term, invert in parallel | Divided across machines | One split per parser, one partition per inverter |
| Logarithmic merging | Keep indexes of doubling sizes and merge like binary carries | $\Theta(T \log(T/n))$ for updates | The current in-memory index |
Worked problems
The first problem is an exercise from chapter 4 of the course text (Manning, Raghavan, and Schütze, 2008), restated with its data. The other two are posed on this article's Arctic sentences and on its table of flushes. The script tools/experiments/indexing/reuters_construction.py checks every number.
Problem 1 (Exercise 4.1 of the course text). Suppose sorting $T$ pairs takes $T \log_2 T$ comparisons, and that the sort runs on disk with an algorithm not designed for it, so that every comparison costs two disk seeks. With a seek time of 5 milliseconds, how long would the sort take for RCV1's $T = 10^8$ pairs?
Solution. About $2.66 \times 10^7$ seconds, which is 308 days. First, $\log_2 10^8 = 8 \log_2 10 = 26.58$, so the sort makes $10^8 \times 26.58 = 2.66 \times 10^9$ comparisons. Each costs two seeks of $5 \times 10^{-3}$ seconds, that is $10^{-2}$ seconds. The total is $2.66 \times 10^9 \times 10^{-2} = 2.66 \times 10^7$ seconds, and $2.66 \times 10^7 / 86{,}400 \approx 308$ days. The same number of comparisons in memory, at the $10^{-8}$ seconds per low-level operation of the text's Table 4.1, would take under a minute if each comparison were a single operation. This is why BSBI sorts in memory and touches the disk only in sequential passes.
Problem 2. The four Arctic sentences arrive one at a time, in docID order, at an index that holds $n = 6$ postings in memory and uses logarithmic merging. Document 4 has just been added. (a) Which indexes exist, and how many postings have been written to disk? (b) Answer the query arctic AND seabird. (c) Document 2 is then deleted. Answer the query again, and give the document frequency the index reports for arctic.
Solution. (a) Two indexes are on disk, $I_1$ holding documents 1 and 2 and $I_0$ holding document 3, with document 4 in memory; 24 postings have been written. Documents 1, 2 and 3 have six distinct terms each, so each fills memory and causes a flush. Flush 1 writes document 1 as $I_0$, 6 postings. Flush 2 finds $I_0$ present, merges document 2 with it and writes $I_1$, 12 postings. Flush 3 finds no $I_0$ and writes document 3 as a new $I_0$, 6 postings. That is $6 + 12 + 6 = 24$, and the indexes present match the binary form of 3, which is 11. Document 4 has four postings and stays in memory.
(b) The answer is documents 2 and 4. Each document lives in exactly one index, so the query is answered in each index separately and the results are combined:
| Index | arctic |
seabird |
Intersection |
|---|---|---|---|
| $I_1$ (documents 1, 2) | 1, 2 | 2 | 2 |
| $I_0$ (document 3) | absent | absent | none |
| Memory (document 4) | 4 | 4 | 4 |
The union of the three intersections is 2, 4, the same answer the single index of the BSBI section gives, where arctic has postings 1, 2, 4 and seabird has 2, 4.
(c) The answer is document 4 alone, and the reported document frequency of arctic is still 3. The deletion sets the bit of document 2 in the invalidation bit vector, which becomes 0100 for documents 1 to 4. No postings change. The query runs as before and returns 2, 4. The filter then drops document 2, whose bit is set, and leaves 4. The posting of document 2 is still in $I_1$'s list for arctic, so counting postings over the three indexes gives $2 + 0 + 1 = 3$, although only two live documents contain the term. The statistic is corrected when a merge next rewrites $I_1$ and leaves the invalidated document out.
Problem 3. For logarithmic merging with flushes of exactly $n$ postings: (a) which indexes are on disk after flush 13; (b) how many postings does flush 12 write, and flush 16; (c) how many postings have been written in all after 16 flushes, and how many would the auxiliary-index scheme have written?
Solution. (a) $I_0$, $I_2$ and $I_3$, of sizes $n$, $4n$ and $8n$, because 13 is binary 1101. (b) Flush 12 writes $6n$ and flush 16 writes $30n$. The number 12 is binary 1100, ending in two zeros: memory merges with $I_0$ to write $2n$, and that merges with $I_1$ to write $4n$. The number 16 is binary 10000, ending in four zeros: the four carries write $2n + 4n + 8n + 16n = 30n$, which is $(2^{5} - 2)n$. (c) Logarithmic merging has written $72n$ and the auxiliary scheme $136n$. By the closed form with $k = 4$, the logarithmic total is $2^4 (4 + \tfrac12) n = 72n$. The auxiliary scheme writes $n \times 16 \times 17 / 2 = 136n$. After 16 flushes the auxiliary scheme has written almost twice as much, where after 8 flushes the ratio was $36/28$.
What we have learned
Building an index is sorting under a memory limit, and the disk rewards only long sequential reads and writes. BSBI sorts one block of pairs at a time and merges, for $\Theta(T \log T)$ comparisons, and keeps a global term map that grows with the collection. SPIMI gives each block its own dictionary and never sorts pairs, at the cost of writing a term once per block, 2.77 times over on Reuters-21578. MapReduce spreads parsing and inverting across machines. For a changing collection, logarithmic merging writes each posting at most $\lfloor \log_2 (T/n) \rfloor + 1$ times. It cut the merge traffic on Reuters from 26.4 million postings to 4.5 million, at the price of more indexes per query.
Every one of these algorithms moves postings to and from disk, so their cost is set by how many bytes a posting takes. On Reuters-21578 that is 2.9 megabytes as plain 4-byte integers. The next two articles show how small the index can be made without losing anything: Dictionary Compression for the dictionary, and Index Compression for the postings, where the smaller index is usually also the faster one to search.
References
- Apache Software Foundation. Apache Lucene core API documentation:
IndexWriterandTieredMergePolicy. https://lucene.apache.org/core/ record · 7 pages - Dean, J., and Ghemawat, S. (2004). MapReduce: Simplified data processing on large clusters. In Proceedings of OSDI 2004, 137–149. record · 2 pages
- Heinz, S., and Zobel, J. (2003). Efficient single-pass index construction for text databases. Journal of the American Society for Information Science and Technology, 54(8), 713–729. record
- Lester, N., Moffat, A., and Zobel, J. (2005). Fast on-line index construction by geometric partitioning. In Proceedings of CIKM 2005, 776–783. record · 2 pages
- Lewis, D. D. (1997). Reuters-21578 text categorization test collection, distribution 1.0. record · 11 pages
- Manning, C. D., Raghavan, P., and Schütze, H. (2008). Introduction to Information Retrieval, chapter 4. Cambridge University Press. record · 102 pages
Reviewed: verified, 16 Nov 2026 · checked against sources, 23 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 13 revisions