Part 2: Boolean Retrieval
Francesco Ricci
Most of these slides comes from the course:
Information Retrieval and Web Search,
Christopher Manning and Prabhakar Raghavan
Content
p Term document matrix
p Information needs and evaluation of IR
p Inverted index
p Processing Boolean queries
p The merge algorithm
p Query optimization
p Skip pointers
p Dictionary data structures
n Hash tables
n Binary trees
Sec. 1.1
Term-document incidence
Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth
Antony 1 1 0 0 0 1
Brutus 1 1 0 1 0 0
Caesar 1 1 0 1 1 1
Calpurnia 0 1 0 0 0 0
Cleopatra 1 0 0 0 0 0
mercy 1 0 1 1 1 1
worser 1 0 1 1 1 0
Brutus AND Caesar BUT NOT 1 if play contains
Calpurnia word, 0 otherwise
Sec. 1.1
Incidence vectors
p So we have a 0/1 vector for each term
p To answer query:
n Brutus, Caesar and NOT Calpurnia
n take the vectors for
p Brutus 110100
p Caesar 110111
p Calpurnia (complemented) 101111
n Bitwise AND
p 110100 AND 110111 AND 101111 = 100100
4
Sec. 1.1
Answers to query
p Antony and Cleopatra, Act III, Scene ii
Agrippa [Aside to DOMITIUS ENOBARBUS]: Why, Enobarbus,
When Antony found Julius Caesar dead,
He cried almost to roaring; and he wept
When at Philippi he found Brutus slain.
p Hamlet, Act III, Scene ii
Lord Polonius: I did enact Julius Caesar I was killed i' the
Capitol; Brutus killed me.
http://www.rhymezone.com/shakespeare/
5
Sec. 1.1
Basic assumptions of IR
p Collection: fixed set of documents
p Goal: retrieve documents with information that is
relevant to the user’s information need and helps
the user complete a task
p Using the Boolean Retrieval Model means that
the information need must be translated into a
Boolean expression:
n terms combined with AND, OR, and NOT
operators
p We want to support ad hoc retrieval: provide
documents relevant to an arbitrary user
information need.
6
Sec. 1.1
How good are the retrieved docs?
p Precision : Fraction of retrieved docs that are
relevant to user’s information need
p Recall : Fraction of relevant docs in collection
that are retrieved
p More precise definitions and measurements to
follow in another lecture on evaluation.
7
Relevance
p Relevance is the core concept in
IR, but nobody has a good definition
n Relevance = useful
n Relevance = topically related
n Relevance = new
n Relevance = interesting
n Relevance = ???
p Relevance is very dynamic – it depends on the
needs of a person at a specific point in time
p The same result for the same query may be
relevant for a user and not relevant for another
Boolean Retrieval and Relevance
p Assumption: A document is relevant to the
information need expressed by a query if it
satisfies the Boolean expression of the query.
p Question: Is it always true?
p No: consider for instance a collection of
documents dated before 2014, and the query is
"oscar AND 2014". Would the documents
retrieved by this query relevant?
Relevance and Retrieved documents
Information need Ex: "lincoln"
relevant not relevant
Query and system
TP FP retrieved
not retrieved
FN TN
Documents Precision P = tp/(tp + fp)
= tp/retrieved
Recall R = tp/(tp + fn)
= tp/relevant
Sec. 1.1
Term-document incidence
Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth
Antony 1 1 0 0 0 1
Brutus 1 1 0 1 0 0
Caesar 1 1 0 1 1 1
Calpurnia 0 1 0 0 0 0
Cleopatra 1 0 0 0 0 0
mercy 1 0 1 1 1 1
worser 1 0 1 1 1 0
Brutus AND Caesar BUT NOT 1 if play contains
Calpurnia word, 0 otherwise
Sec. 1.1
Bigger collections
p Consider a more realistic case
p 1M (million) documents, each with about 1000
words
p Avg 6 bytes/word including spaces/punctuation
n 6GB of data in the documents
p Say there are 500K distinct terms among these
p 500K x 1M matrix has half-a-trillion 0’s and 1’s
p But it has no more than one billion 1’s Why?
n matrix is extremely sparse
p What’s a better representation?
n We only record the positions of the 1's.
12
Sec. 1.2
Inverted index
p For each term t, we must store a list of all
documents that contain t
n Identify each by a docID, a document serial
number
p Can we used fixed-size arrays for this?
Brutus 1 2 4 11 31 45 173 174
Caesar 1 2 4 5 6 16 57 132
Calpurnia 2 31 54 101
What happens if the word Caesar
is added to document 14?
13
Sec. 1.2
Inverted index
p We need variable-size postings lists
n On disk, a continuous run of postings is normal
and best
n In memory, can use linked lists or variable
length arrays
p Some tradeoffs in size/ease of insertion Posting
Brutus 1 2 4 11 31 45 173 174
Caesar 1 2 4 5 6 16 57 132
Calpurnia 2 31 54 101
Dictionary Postings
Sorted by docID (more later on why)14
Sec. 1.2
Inverted index construction
Documents to Friends, Romans, countrymen.
be indexed
Tokenizer
Token stream. Friends Romans Countrymen
More on Linguistic
these later. modules
Modified tokens friend roman countryman
Indexer friend 2 4
roman 1 2
Inverted index
countryman 13 16
Sec. 1.2
Indexer steps: Token sequence
p Sequence of (Modified token, Document ID)
pairs.
Doc 1 Doc 2
I did enact Julius So let it be with
Caesar I was killed Caesar. The noble
i' the Capitol; Brutus hath told you
Brutus killed me. Caesar was ambitious
Sec. 1.2
Indexer steps: Sort
p Sort by terms
n And then docID
Core
indexing
step
Sec. 1.2
Indexer steps: Dictionary & Postings
p Multiple term
entries in a single
document are
merged
p Split into
Dictionary and
Postings
p Doc. frequency
information is
added.
Why
frequency?
Will
discuss
later
Sec. 1.2
Where do we pay in storage?
Lists of
docIDs
Terms
and
counts Later in the
course:
• How do we
index
efficiently?
• How much
storage do we
Pointers need?
19
Exercise
p How many bytes do we need to store the inverted
index if there are:
n N = 1 million documents, each with about
1000 words
n Say there are M = 500K distinct terms among
these
n We need to store: term IDs, doc frequencies,
pointers to postings lists, list of doc IDs
(postings).
Exercise Solution
p Log2(500,000) = 19 bits are required for
representing the terms and the pointers to their
postings lists
n Hence 3 bytes (= 24bits, representing 16.7M of
alternatives) are enough for each term and
pointer
p 3 bytes for each term frequency (the largest term
frequency is 1M = #of docs)
p Hence 9 x 500,000 = 4.5 x 106
p We have at most 1 billion postings (#of tokens in
documents), hence 3 bytes for each posting
(docid) = 3x109
p In total 3,004,500,000 ~ 3GB
Sec. 1.3
The index we just built
p How do we process a query? Today’s
focus
p Later - what kinds of queries can we process?
22
Sec. 1.3
Query processing: AND
p Consider processing the query:
Brutus AND Caesar
n Locate Brutus in the Dictionary
p Retrieve its postings
n Locate Caesar in the Dictionary
p Retrieve its postings How we can
n “Merge” the two postings merge?
2 4 8 16 32 64 128 Brutus
1 2 3 5 8 13 21 34 Caesar
23
The idea
brutus nn 02 nn 04 nn nn nn nn nn nn nn nn nn nn nn 16
cesar 01 02 nn nn 05 nn nn 08 nn nn nn nn 13 nn nn nn
position 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16
p If we have the incidence vectors we scan in
parallel the entries of the two vectors – starting
from the first position (here I wrote the doc id,
e.g., "08", instead of 1 and "nn" instead of 0)
p Try to replicate this idea but imagine that in
these two arrays you removed the "nn" entries ...
p Keep a pointer to each list, advance the
pointer to the smallest docID and check if
now the pointers refer to the same docID.
Sec. 1.3
The merge
p Walk through the two postings simultaneously, in
time linear in the total number of postings entries
2 4 8 16 32 64 128 Brutus
2 8
1 2 3 5 8 13 21 34 Caesar
If the list lengths are x and y, the merge takes O(x+y)
operations.
Crucial: postings sorted by docID.
25
Intersecting two postings lists
(a “merge” algorithm)
26
Sec. 1.3
Boolean queries: Exact match
p The Boolean retrieval model is being able to ask a
query that is a Boolean expression:
n Boolean Queries are queries using AND, OR
and NOT to join query terms
p Views each document as a set of words
p Is
precise: document matches condition or
not.
n Perhaps the simplest model to build an IR
system on
p Primary commercial retrieval tool for 3 decades
p Many search systems you still use are Boolean:
n Email, library catalog, Mac OS X Spotlight.
27
Sec. 1.4
Example: WestLaw http://www.westlaw.com/
p Largest commercial (paying subscribers) legal
search service (started 1975; ranking added
1992)
p Tens of terabytes of data; 700,000 users
p Majority of users still use boolean queries
p Example query:
n What is the statute of limitations in cases
involving the federal tort claims act?
n LIMIT! /3 STATUTE ACTION /S FEDERAL /2
TORT /3 CLAIM
p /3
= within 3 words, /S = in the same
sentence
28
Sec. 1.3
More general merges
p Exercise: Adapt the merge for the queries:
Brutus AND NOT Caesar
Brutus OR NOT Caesar
Can we still run through the merge in time O(x+y)?
What can we achieve?
29
Sec. 1.3
Merging
What about an arbitrary Boolean formula?
(Brutus OR Caesar) AND NOT
(Antony OR Cleopatra)
p Can we always merge in “linear” time?
n Linear in what?
p Can we do better?
30
Sec. 1.3
Query optimization
p What is the best order for query processing?
p Consider a query that is an AND of n terms
p For each of the n terms, get its postings, then
AND them together
Brutus 2 4 8 16 32 64 128
Caesar 1 2 3 5 8 16 21 34
Calpurnia 13 16
Query: Brutus AND Calpurnia AND Caesar
31
Sec. 1.3
Query optimization example
p Process in order of increasing term freq, i.e.,
posting list length:
n start with smallest set, then keep cutting
further.
This is why we kept
document freq. in dictionary
Brutus 2 4 8 16 32 64 128
Caesar 1 2 3 5 8 16 21 34
Calpurnia 13 16
Execute
the
query
as
(Calpurnia
AND
Brutus)
AND
Caesar.
32
Sec. 1.3
More general optimization
p e.g., (madding OR crowd) AND (ignoble
OR strife)
p Get doc. freq.’s for all terms
p Estimate the size of each OR by the sum of its
doc. freq.’s (conservative)
p Process in increasing order of OR sizes.
33
Algorithm for conjunctive queries
p The intermediate result is in memory
p The list is being intersected with is read from disk
p The intermediate result is always shorter and
shorter
Exercise
p Recommend a query
processing order for
Term Freq
eyes 213312
(tangerine OR trees) AND kaleidoscope 87009
(marmalade OR skies) AND marmalade 107913
(kaleidoscope OR eyes) skies 271658
tangerine 46653
trees 316812
35
What’s ahead in IR? Beyond term
search
p What about phrases?
n Stanford University
p Proximity: Find Gates NEAR Microsoft.
n Need index to capture position information in
docs.
p Zones in documents: Find documents with
(author = Ullman) AND (text contains
automata).
36
Evidence accumulation
p 1 vs. 0 occurrence of a search term
n 2 vs. 1 occurrence
n 3 vs. 2 occurrences, etc.
n Usually more seems better
p Need term frequency information in docs
37
FASTER POSTINGS MERGES:
SKIP POINTERS/SKIP LISTS
Sec. 2.3
Recall basic merge
p Walk through the two postings simultaneously, in
time linear in the total number of postings entries
2 4 8 41 48 64 128 Brutus
2 8
1 2 3 8 11 17 21 31 Caesar
If the list lengths are m and n, the merge takes O(m+n)
operations.
Can we do better?
Yes (if index isn’t changing too fast).
Sec. 2.3
Augment postings with skip pointers
(at indexing time)
41 128
2 4 8 41 48 64 128
11 31
1 2 3 8 11 17 21 31
p Why?
p To skip postings that will not figure in the search
results.
p How?
p Where do we place skip pointers?
Sec. 2.3
Query processing with skip pointers
41 128
2 4 8 41 48 64 128
11 31
1 2 3 8 11 17 21 31
Suppose we’ve stepped through the lists until we
process 8 on each list. We match it and advance.
We then have 41 and 11 on the lower. 11 is smaller.
But instead to advance to 17 the skip successor of
11 on the lower list is 31, and it is smaller than 41,
so we can skip ahead.
Intersect with skip pointers
Sec. 2.3
Where do we place skips?
p Tradeoff:
n More skips → shorter skip spans ⇒ more likely
to skip. But lots of comparisons to skip
pointers.
n Fewer skips → few pointer comparison, but
then long skip spans ⇒ few successful skips.
Sec. 2.3
Placing skips
p Simple heuristic: for postings of length L, use √L evenly-
spaced skip pointers
p This takes into account the distribution of query terms in a
simple way – the larger the doc frequency of a term the
larger the number of skip pointers
p Easy if the index is relatively static; harder if postings keep
changing because of updates
p This definitely used to help; with modern hardware it may
not (Bahle et al. 2002) unless you’re memory-based:
n because the I/O cost of loading a bigger index structure
can outweigh the gains from quicker in memory
merging!
Sec. 3.1
Dictionary data structures for inverted
indexes
p The dictionary data structure stores the term
vocabulary, document frequency, pointers to
each postings list … in what data structure?
Sec. 3.1
A naïve dictionary
p An array of struct:
char[20] int Postings *
20 bytes 4/8 bytes 4/8 bytes
p How do we store a dictionary in memory efficiently?
p How do we quickly look up elements at query time?
Sec. 3.1
Dictionary data structures
p Two main choices:
n Hash table
n Tree
p Some IR systems use hashes, some trees
Sec. 3.1
Hashes
p Each vocabulary term is hashed to an integer
n (We assume you’ve seen hashtables before)
p Pros:
n Lookup is faster than for a tree: O(1)
p Cons:
n No easy way to find minor variants:
p judgment/judgement
n No prefix search ("bar*") [tolerant retrieval]
n If vocabulary keeps growing, need to
occasionally do the expensive operation of
rehashing everything
Sec. 3.1
Tree: binary tree
Root
a-m n-z
a-hu hy-m n-sh si-z
Sec. 3.1
Tree: B-tree
a-hu n-z
hy-m
n Definition: Every internal node has a number of
children in the interval [a,b] where a, b are
appropriate natural numbers, e.g., [2,4].
Sec. 3.1
Trees
p Simplest: binary tree
p More usual: B-trees
p Trees require a standard ordering of characters and
hence strings … but we have one – lexicographic
n Unless we are dealing with Chinese (no unique
ordering)
p Pros:
n Solves the prefix problem (terms starting with
'hyp')
p Cons:
n Slower: O(log M) [and this requires balanced tree]
n Rebalancing binary trees is expensive
p But B-trees mitigate the rebalancing problem.
Reading Material
p Chapter 1
p Section 2.3: Faster postings list intersection via
skip pointers
p Section 3.1: Search structures for dictionaries