Title
COMP6714: Information Retrieval & Web Search
Introduction to
Information Retrieval
Lecture 4: Index Construction
1
COMP6714: Information Retrieval & Web Search
Plan
▪ Last lecture:
▪ Dictionary data structures
▪ Tolerant retrieval
▪ Wildcards
▪ Spell correction
▪ Soundex
▪ This time:
▪ Index construction
a-hu
hy-m
n-z
mo
on
among
$m mace
abandon
amortize
madden
among
2
COMP6714: Information Retrieval & Web Search
Index construction
▪ How do we construct an index?
▪ What strategies can we use with limited main
memory?
Ch. 4
3
COMP6714: Information Retrieval & Web Search
Hardware basics
▪ Many design decisions in information retrieval are
based on the characteristics of hardware
▪ We begin by reviewing hardware basics
Sec. 4.1
4
COMP6714: Information Retrieval & Web Search
Hardware basics
▪ Access to data in memory is much faster than access
to data on disk.
▪ Disk seeks: No data is transferred from disk while the
disk head is being positioned.
▪ Therefore: Transferring one large chunk of data from
disk to memory is faster than transferring many small
chunks.
▪ Disk I/O is block-based: Reading and writing of entire
blocks (as opposed to smaller chunks).
▪ Block sizes: 8KB to 256 KB.
Sec. 4.1
5
COMP6714: Information Retrieval & Web Search
Hardware basics
▪ Servers used in IR systems now typically have several
GB of main memory, sometimes tens of GB.
▪ Available disk space is several (2–3) orders of
magnitude larger.
▪ Fault tolerance is very expensive: It’s much cheaper
to use many regular machines rather than one fault
tolerant machine.
Sec. 4.1
6
COMP6714: Information Retrieval & Web Search
Hardware assumptions
▪ symbol statistic value
▪ s average seek time 5 ms = 5 x 10−3 s
▪ b transfer time per byte 0.02 μs = 2 x 10−8 s
▪ processor’s clock rate 109 s−1
▪ p low-level operation 0.01 μs = 10−8 s
(e.g., compare & swap a word)
▪ size of main memory several GB
▪ size of disk space 1 TB or more
Sec. 4.1
7
COMP6714: Information Retrieval & Web Search
RCV1: Our collection for this lecture
▪ Shakespeare’s collected works definitely aren’t large
enough for demonstrating many of the points in this
course.
▪ The collection we’ll use isn’t really large enough
either, but it’s publicly available and is at least a more
plausible example.
▪ As an example for applying scalable index
construction algorithms, we will use the Reuters
RCV1 collection.
▪ This is one year of Reuters newswire (part of 1995
and 1996)
Sec. 4.2
8
COMP6714: Information Retrieval & Web Search
A Reuters RCV1 document
Sec. 4.2
9
COMP6714: Information Retrieval & Web Search
Reuters RCV1 statistics
▪ symbol statistic value
▪ N documents 800,000
▪ L avg. # tokens per doc 200
▪ M terms (= word types) 400,000
▪ avg. # bytes per token 6
(incl. spaces/punct.)
▪ avg. # bytes per token 4.5
(without spaces/punct.)
▪ avg. # bytes per term 7.5
▪ non-positional postings 100,000,000
Sec. 4.2
10
COMP6714: Information Retrieval & Web Search
▪ Documents are parsed to extract words and these
are saved with the Document ID.
I did enact Julius
Caesar I was killed
i’ the Capitol;
Brutus killed me.
Doc 1
So let it be with
Caesar. The noble
Brutus hath told you
Caesar was ambitious
Doc 2
Recall IIR 1 index construction
Term Doc #
I 1
did 1
enact 1
julius 1
caesar 1
I 1
was 1
killed 1
i’ 1
the 1
capitol 1
brutus 1
killed 1
me 1
so 2
let 2
it 2
be 2
with 2
caesar 2
the 2
noble 2
brutus 2
hath 2
told 2
you 2
caesar 2
was 2
ambitious 2
Sec. 4.2
11
COMP6714: Information Retrieval & Web Search
Term Doc #
I 1
did 1
enact 1
julius 1
caesar 1
I 1
was 1
killed 1
i’ 1
the 1
capitol 1
brutus 1
killed 1
me 1
so 2
let 2
it 2
be 2
with 2
caesar 2
the 2
noble 2
brutus 2
hath 2
told 2
you 2
caesar 2
was 2
ambitious 2
Term Doc #
ambitious 2
be 2
brutus 1
brutus 2
capitol 1
caesar 1
caesar 2
caesar 2
did 1
enact 1
hath 1
I 1
I 1
i’ 1
it 2
julius 1
killed 1
killed 1
let 2
me 1
noble 2
so 2
the 1
the 2
told 2
you 2
was 1
was 2
with 2
Key step
▪ After all documents have been
parsed, the inverted file is
sorted by terms.
We focus on this sort step.
We have 100M items to sort.
Sec. 4.2
12
COMP6714: Information Retrieval & Web Search
Hash based in-memory index
construction
▪ Another in-memory index construction method is to
use hash-tables
▪ Append (docid, pos) to the existing (partial) postings list of
the token; create a new postings list if necessary
▪ Generally, faster than sorting-based method
▪ Further optimizations
▪ Dealing with collision: insert-at-back and move-to-front
heuristics
▪ Saving space and time: use ArrayList to implement
postings lists
Sec. 4.2
13
COMP6714: Information Retrieval & Web Search
Scaling index construction
▪ In-memory index construction does not scale.
▪ How can we construct an index for very large
collections?
▪ Taking into account the hardware constraints we just
learned about . . .
▪ Memory, disk, speed, etc.
Sec. 4.2
14
COMP6714: Information Retrieval & Web Search
Sort-based index construction
▪ As we build the index, we parse docs one at a time.
▪ While building the index, we cannot easily exploit
compression tricks (you can, but much more complex)
▪ The final postings for any term are incomplete until the end.
▪ At 12 bytes per non-positional postings entry (term, doc,
freq), demands a lot of space for large collections.
▪ T = 100,000,000 in the case of RCV1
▪ So … we can do this in memory in 2009, but typical
collections are much larger. E.g. the New York Times
provides an index of >150 years of newswire
▪ Thus: We need to store intermediate results on disk.
Sec. 4.2
15
COMP6714: Information Retrieval & Web Search
Use the same algorithm for disk?
▪ Can we use the same index construction algorithm
for larger collections, but by using disk instead of
memory?
▪ No: Sorting T = 100,000,000 records on disk is too
slow – too many disk seeks.
▪ We need an external sorting algorithm.
Sec. 4.2
16
COMP6714: Information Retrieval & Web Search
Bottleneck
▪ Parse and build postings entries one doc at a time
▪ Now sort postings entries by term (then by doc
within each term)
▪ Doing this with random disk seeks would be too slow
– must sort T=100M records
If every comparison took 2 disk seeks, and N items could be
sorted with N log2N comparisons, how long would this take?
Sec. 4.2
17
COMP6714: Information Retrieval & Web Search
BSBI: Blocked sort-based Indexing
(Sorting with fewer disk seeks)
▪ 12-byte (4+4+4) records (term, doc, freq).
▪ These are generated as we parse docs.
▪ Must now sort 100M such 12-byte records by term.
▪ Define a Block ~ 10M such records
▪ Can easily fit a couple into memory.
▪ Will have 10 such blocks to start with.
▪ Basic idea of algorithm:
▪ Accumulate postings for each block, sort, write to disk.
▪ Then merge the blocks into one long sorted order.
Sec. 4.2
18
COMP6714: Information Retrieval & Web Search Sec. 4.2
19
COMP6714: Information Retrieval & Web Search
Sorting 10 blocks of 10M records
▪ First, read each block and sort within:
▪ Quicksort takes 2N ln N expected steps
▪ In our case 2 x (10M ln 10M) steps
▪ Exercise: estimate total time to read each block from
disk and and quicksort it.
▪ 10 times this estimate – gives us 10 sorted runs of
10M records each.
▪ Done straightforwardly, need 2 copies of data on disk
▪ But can optimize this
Sec. 4.2
20
COMP6714: Information Retrieval & Web Search Sec. 4.2
21
COMP6714: Information Retrieval & Web Search
Example
▪ Settings
▪ B: Block/page size
▪ M: Size of main memory in pages (e.g., = 10 blocks)
▪ N: Number of documents (e.g., = 10000)
▪ R: Size of the (term, docID) pairs one document emits.
▪ Simplifying assumptions:
▪ R: the same for all documents
▪ B = c*R, for some integer c (e.g., c = 5)
▪ All I/Os have the same cost
COMP6714: Information Retrieval & Web Search
External Merge-Sort: Phase I
▪ Phase I: load the (term, docID) pairs from (M*B)/R
documents (at a time) into M buffer pages; sort
▪ Result: (initial) runs of length M pages
▪ # of runs = 200
M page of main memory
DiskDisk
. . .. . .
(this, 1), (is, 1), …
(I, 2), (am, 2), …
…
(today, c*M), …
sort
COMP6714: Information Retrieval & Web Search
Phase II /1
▪ Recursively merge (up to) M – 1 runs into a new run
▪ Result: runs of length M (M – 1) pages
M bytes of main memory
DiskDisk
. . .. . .
Input Buffer M-1
Input Buffer 1
Input Buffer 2
. . . .
Output
Buffer
(M-1)-way Merge
COMP6714: Information Retrieval & Web Search
Phase II /2
▪ Recursively merge (up to) M – 1 runs into a new run
▪ Result: runs of length M (M – 1)2 pages
M bytes of main memory
DiskDisk
. . .
Input Buffer M-1
Input Buffer 1
Input Buffer 2
. . . .
Output
Buffer
(M-1)-way Merge
COMP6714: Information Retrieval & Web Search
Phase II /3
▪ Recursively merge (up to) M – 1 runs into a new run
▪ Result: a single run
M bytes of main memory
DiskDisk
Input Buffer M-1
Input Buffer 1
Input Buffer 2
. . . .
Output
Buffer
(M-1)-way Merge
COMP6714: Information Retrieval & Web Search
Cost of External Merge Sort
▪ Number of passes: 1 + log𝑀−1
𝑁𝑅
𝑀𝐵
Total I/O cost: 2 ⋅
𝑁𝑅
𝐵
⋅ 1 + log𝑀−1
𝑁𝑅
𝑀𝐵
blocks/pages
▪ How much data can we sort with 10MB RAM?
▪ Assume B = 4KB
▪ 1 pass 10MB “data”
▪ 2 passes ≈25GB “data” (M-1 = 2559)
▪ 3 passes ?
▪ Can sort most reasonable inputs in 2 or 3 passes !
Another
Example
COMP6714: Information Retrieval & Web Search
Example: 2-Way Merge for 20 Runs
R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 R13 R14 R15 R16 R17 R18 R19 R20
S1 S2 S3 S4 S5 S6 S7 S8 S9 S10
T1 T2 T3 T4 T5
U1 U2
U3
V1 V2
W1
Number of passes = 5
COMP6714: Information Retrieval & Web Search
Example: 5-Way Merge for 20 Runs
R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 R13 R14 R15 R16 R17 R18 R19 R20
T1
S1 S2 S3 S4
Number of passes = 2
COMP6714: Information Retrieval & Web Search
K-way Merge: Using a min-heap
30
adapted from https://karticks.wordpress.com/2009/07/29/the-mapreduce-design-pattern-demystified/
99
COMP6714: Information Retrieval & Web Search
Remaining problem with sort-based
algorithm
▪ Our assumption was: we can keep the dictionary in
memory.
▪ We need the dictionary (which grows dynamically) in
order to implement a term to termID mapping.
▪ Actually, we could work with term,docID postings
instead of termID,docID postings . . .
▪ . . . but then intermediate files become very large.
(We would end up with a scalable, but very slow
index construction method.)
Sec. 4.3
31
COMP6714: Information Retrieval & Web Search
SPIMI:
Single-pass in-memory indexing
▪ Key idea 1: Generate separate dictionaries for each
block – no need to maintain term-termID mapping
across blocks.
▪ Key idea 2: Don’t sort. Accumulate postings in
postings lists as they occur.
▪ With these two ideas we can generate a complete
inverted index for each block.
▪ These separate indexes can then be merged into one
big index.
Sec. 4.3
32
COMP6714: Information Retrieval & Web Search
SPIMI-Invert
▪ Merging of blocks is analogous to BSBI.
Sec. 4.3
33
COMP6714: Information Retrieval & Web Search
SPIMI: Compression
▪ Compression makes SPIMI even more efficient.
▪ Compression of terms
▪ Compression of postings
▪ See next lecture
Sec. 4.3
34
COMP6714: Information Retrieval & Web Search
Distributed indexing
▪ For web-scale indexing (don’t try this at home!):
must use a distributed computing cluster
▪ Individual machines are fault-prone
▪ Can unpredictably slow down or fail
▪ How do we exploit such a pool of machines?
Sec. 4.4
35
For those interested in the topic, read the textbook
for distributed indexing using the Map-Reduce
paradigm.
Also check out:
http://terrier.org/docs/v3.5/hadoop_indexing.html
http://terrier.org/docs/v3.5/hadoop_indexing.html
COMP6714: Information Retrieval & Web Search
Dynamic indexing
▪ Up to now, we have assumed that collections are
static.
▪ They rarely are:
▪ Documents come in over time and need to be inserted.
▪ Documents are deleted and modified.
▪ This means that the dictionary and postings lists have
to be modified:
▪ Postings updates for terms already in dictionary
▪ New terms added to dictionary
Sec. 4.5
36
COMP6714: Information Retrieval & Web Search
Simplest approach – Immediate Merge
▪ Maintain “big” main index
▪ New docs go into “small” auxiliary index
▪ Merge immediately with the big main index when memory
is full
▪ Search across both, merge results
▪ Deletions
▪ Invalidation bit-vector for deleted docs
▪ Filter docs output on a search result by this invalidation
bit-vector
▪ Periodically, re-index into one main index
Sec. 4.5
37
COMP6714: Information Retrieval & Web Search
Issues with main and auxiliary indexes
▪ Problem of frequent merges – you touch stuff a lot
▪ Poor performance during merge
▪ Actually:
▪ Merging of the auxiliary index into the main index is efficient if we
keep a separate file for each postings list.
▪ Merge is the same as a simple append.
▪ But then we would need a lot of files – inefficient for O/S.
▪ Assumption for the rest of the lecture: The index is one big
file.
▪ In reality: Use a scheme somewhere in between (e.g., split
very large postings lists, collect postings lists of length 1 in one
file etc.)
Sec. 4.5
38
COMP6714: Information Retrieval & Web Search
Another Extreme – No Merge
Sec. 4.5
▪ Whenever memory is full, write the sub-index to the
disk
▪ Never merge sub-indexes
▪ Pros:
▪ High indexing performance
▪ Cons:
▪ Slow query performance
▪ Require Ω(|C|/M) seeks to fetch the inverted list for a term
39
COMP6714: Information Retrieval & Web Search
Compromise – Logarithmic merge
Sec. 4.5
▪ Comprise of the previous two extremes
▪ Generation of a sub-index
▪ The one directly created from in-memory index has
generation = 0
▪ Merge of multiple sub-index with max generation = g gives
a new index with generation = g+1
▪ Invariant: no two sub-indexes can have the same
generation
▪ When memory is full, create I0
▪ If we have two sub-indexes of generation g, merge them to
form a single sub-index of generation g+1
40
COMP6714: Information Retrieval & Web Search
Illustration
Sec. 4.5
41
COMP6714: Information Retrieval & Web Search
Algorithm
▪ Maintain a series of indexes, each twice as large as
the previous one.
▪ Keep smallest (Z0) in memory
▪ Larger ones (I0, I1, …) on disk
▪ If Z0 gets too big (> n), write to disk as I0
▪ or merge with I0 (if I0 already exists) as Z1
▪ Either write merge Z1 to disk as I1 (if no I1)
▪ or merge with I1 to form Z2
▪ etc.
Sec. 4.5
42
COMP6714: Information Retrieval & Web Search Sec. 4.5
43
COMP6714: Information Retrieval & Web Search
Logarithmic merge
▪ Auxiliary and main index: index construction time is
O(C2/M) as each posting is touched in each merge.
▪ C = Collection Size, and M = memory size
▪ Logarithmic merge: Each posting is merged
O(log C/M) times, so complexity is O(C log (C/M))
▪ So logarithmic merge is much more efficient for
index construction
▪ But query processing now requires the merging of
O(log T) indexes
▪ Whereas it is O(1) if you just have a main and auxiliary
index
Sec. 4.5
44
COMP6714: Information Retrieval & Web Search
Lucene
45
▪ Mergefactor:
▪ http://lucene.sourceforge.net/talks/inktomi/
▪ In animation:
▪ http://blog.mikemccandless.com/2011/02/visualizing-
lucenes-segment-merges.html
http://lucene.sourceforge.net/talks/inktomi/
http://blog.mikemccandless.com/2011/02/visualizing-lucenes-segment-merges.html
COMP6714: Information Retrieval & Web Search
Further issues with multiple indexes
▪ Collection-wide statistics are hard to maintain
▪ E.g., when we spoke of spell-correction: which of
several corrected alternatives do we present to the
user?
▪ We said, pick the one with the most hits
▪ How do we maintain the top ones with multiple
indexes and invalidation bit vectors?
▪ One possibility: ignore everything but the main index for
such ordering
▪ Will see more such statistics used in results ranking
Sec. 4.5
46
COMP6714: Information Retrieval & Web Search
Dynamic indexing at search engines
▪ All the large search engines now do dynamic
indexing
▪ Their indices have frequent incremental changes
▪ News items, blogs, new topical web pages
▪ Sarah Palin, …
▪ But (sometimes/typically) they also periodically
reconstruct the index from scratch
▪ Query processing is then switched to the new index, and
the old index is then deleted
Sec. 4.5
47
COMP6714: Information Retrieval & Web Search Sec. 4.5
48
COMP6714: Information Retrieval & Web Search
Other sorts of indexes
▪ Positional indexes
▪ Same sort of sorting problem … just larger
▪ Building character n-gram indexes:
▪ As text is parsed, enumerate n-grams.
▪ For each n-gram, need pointers to all dictionary terms
containing it – the “postings”.
▪ Note that the same “postings entry” will arise repeatedly
in parsing the docs – need efficient hashing to keep track
of this.
▪ E.g., that the trigram uou occurs in the term deciduous will be
discovered on each text occurrence of deciduous
▪ Only need to process each term once
Why?
Sec. 4.5
49
COMP6714: Information Retrieval & Web Search
Resources for today’s lecture
▪ Chapter 4 of IIR
▪ MG Chapter 5
▪ Original publication on MapReduce: Dean and
Ghemawat (2004)
▪ Original publication on SPIMI: Heinz and Zobel (2003)
Ch. 4
50