Study Vault Search notes, aliases and full text
Graph
TopicDBISAI-written · not yet proofread

Inverted Index and Ranking

Keyword search with an inverted index whose postings carry scores (tf, tf.idf, BM25) that are summed while merging the lists, plus Zipf's law for word frequencies.

Concepts#

  1. Inverted Indexword → sorted list of postings (doc ID, score); the data structure start here
  2. Zipf's Lawword frequencies follow Fn=C⋅n−αF_n = C \cdot n^{-\alpha}; few very long lists, many short ones
  3. Term Frequencynumber of occurrences of a word in a document; the naive posting score
  4. TF-IDFtf weighted by log⁡2(N/df)\log_2(N/\mathrm{df}) so that common words count little
  5. BM25tf.idf with saturating, length-normalized tf; the standard score

How it fits together#

Pipeline. Tokenize, build the inverted lists in one linear pass (IDs arrive increasing, so lists are sorted), then compute a score per posting. A query merges the lists of its words (intersection for AND, merge with score summation for OR, see List Intersection), sorts by summed score and returns the top kk (partial sort, Θ(n+klog⁡k)\Theta(n + k \log k)).

Scores, step by step. Each step fixes a defect of the previous one:

ScorePosting scoreDefect fixedRemaining defect
tftf\mathrm{tf}nonefrequent words (of) dominate
tf.idftf⋅log⁡2(N/df)\mathrm{tf} \cdot \log_2(N/\mathrm{df})common words weigh littletf grows linearly and with document length
BM25tf∗⋅log⁡2(N/df)\mathrm{tf}^* \cdot \log_2(N/\mathrm{df})tf saturates at k+1k+1, normalized by DL/AVDL\mathrm{DL}/\mathrm{AVDL}parameters k,bk, b must be tuned (on a training set)

BM25 contains the others as special cases: k=0k = 0 gives binary scores times idf, k→∞k \to \infty with b=0b = 0 gives tf.idf. The idf factor is the same throughout, and dfw\mathrm{df}_w is just the length of the inverted list of ww.

Zipf’s law connects the pieces: it predicts the skewed list lengths (order lists by length before intersecting) and motivates idf (the most frequent words carry the least information).

Vector space view. The scored inverted index is the row-major sparse term-document matrix AA; the score sum for a 0/1 query vector qq is qTAq^T A (DBIS_VL11 > Retrieval as a matrix-vector product). With BM25 entries this gives exactly the inverted-index ranking.

Pitfall. Requiring all query words to match (pure AND) misses relevant documents; boost them instead of filtering (DBIS_VL02 > Further refinements and other methods).

Key Formulas#

  • idfw=log⁡2Ndfw\mathrm{idf}_w = \log_2 \frac{N}{\mathrm{df}_w},  tf.idf=tf⋅idfw\ \text{tf.idf} = \mathrm{tf} \cdot \mathrm{idf}_w — TF-IDF
  • tf∗=tf (k+1)k α+tf\mathrm{tf}^* = \frac{\mathrm{tf}\,(k+1)}{k\,\alpha + \mathrm{tf}},  α=1−b+b DLAVDL\ \alpha = 1 - b + b\,\frac{\mathrm{DL}}{\mathrm{AVDL}} — BM25
  • Fn=C⋅n−α  ⟺  log⁡Fn=−αlog⁡n+log⁡CF_n = C \cdot n^{-\alpha} \iff \log F_n = -\alpha \log n + \log C — Zipf’s Law

Exercises & Exam Questions#

WS 23/24, Task 1.1 (6 points)#

SuccessSolution

Inverted index (word: list of (document, tf) pairs):

WordPostings
a(D1,1), (D2,1), (D3,2), (D4,2)(D_1, 1),\ (D_2, 1),\ (D_3, 2),\ (D_4, 2)
b(D1,1), (D2,2), (D3,1)(D_1, 1),\ (D_2, 2),\ (D_3, 1)
c(D1,3), (D2,4), (D3,2), (D4,3)(D_1, 3),\ (D_2, 4),\ (D_3, 2),\ (D_4, 3)
d(D1,1), (D2,1), (D3,1)(D_1, 1),\ (D_2, 1),\ (D_3, 1)

Word frequencies (summed across all documents):

  • c: 3+4+2+3=123 + 4 + 2 + 3 = 12
  • a: 1+1+2+2=61 + 1 + 2 + 2 = 6
  • b: 1+2+1=41 + 2 + 1 = 4
  • d: 1+1+1=31 + 1 + 1 = 3

Sorted: F1=12, F2=6, F3=4, F4=3F_1 = 12,\ F_2 = 6,\ F_3 = 4,\ F_4 = 3.

From F1=C=12F_1 = C = 12 and F2=12⋅2−α=6⇒α=1F_2 = 12 \cdot 2^{-\alpha} = 6 \Rightarrow \alpha = 1.

Verification: F3=12/3=4F_3 = 12/3 = 4 and F4=12/4=3F_4 = 12/4 = 3. Both match.

C=12,α=1\boxed{C = 12, \quad \alpha = 1}

WS 23/24, Task 1.3 (4 points)#

SuccessSolution

Result list: [6,7,2,4,5][6, 7, 2, 4, 5]. Relevant set: {1,2,3,4}\{1, 2, 3, 4\}, so R=4R = 4.

Relevance of result positions: [0,0,1,1,0][0, 0, 1, 1, 0] (IDs 2 and 4 are relevant).

P@2P@2: Among the first 2 results (6,7)(6, 7), none are relevant.

P@2=02=0P@2 = \frac{0}{2} = 0

P@R=P@4P@R = P@4: Among the first 4 results (6,7,2,4)(6, 7, 2, 4), two are relevant (2 and 4).

P@R=P@4=24=12P@R = P@4 = \frac{2}{4} = \frac{1}{2}

AP: Sum P@kP@k at each relevant position, divided by R=4R = 4.

  • Position 3 (ID 2, relevant): P@3=1/3P@3 = 1/3
  • Position 4 (ID 4, relevant): P@4=2/4=1/2P@4 = 2/4 = 1/2
AP=14(13+12)=14⋅56=524AP = \frac{1}{4}\left(\frac{1}{3} + \frac{1}{2}\right) = \frac{1}{4} \cdot \frac{5}{6} = \frac{5}{24}

DCG@3: Given rel1=0\text{rel}_1 = 0, rel2=0\text{rel}_2 = 0, rel3=4\text{rel}_3 = 4:

DCG@3=0log⁡22+0log⁡23+4log⁡24=0+0+42=2DCG@3 = \frac{0}{\log_2 2} + \frac{0}{\log_2 3} + \frac{4}{\log_2 4} = 0 + 0 + \frac{4}{2} = 2

WS 17/18, Task 1.1–1.2 (10 points)#

SuccessSolution

1.1 – Binary relevance: Relevant results in the list: #2 (grade 3) and #4 (grade 2). R=3R = 3 total relevant.

  • P@2=1/2P@2 = 1/2 (one relevant in top 2)
  • P@R=P@3=1/3P@R = P@3 = 1/3 (one relevant in top 3)
  • AP=13(P@2+P@4)=13(1/2+2/4)=13⋅1=1/3AP = \frac{1}{3}(P@2 + P@4) = \frac{1}{3}(1/2 + 2/4) = \frac{1}{3} \cdot 1 = 1/3

1.2 – Graded relevance:

DCG@2=0log⁡22+3log⁡23=0+3log⁡23≈1.893DCG@2 = \frac{0}{\log_2 2} + \frac{3}{\log_2 3} = 0 + \frac{3}{\log_2 3} \approx 1.893

For iDCG@2, sort relevances descending: [3,2,1,0,0][3, 2, 1, 0, 0], so:

iDCG@2=3log⁡22+2log⁡23=3+2log⁡23≈4.262iDCG@2 = \frac{3}{\log_2 2} + \frac{2}{\log_2 3} = 3 + \frac{2}{\log_2 3} \approx 4.262nDCG@2=DCG@2iDCG@2=3/log⁡233+2/log⁡23=33log⁡23+2≈0.444nDCG@2 = \frac{DCG@2}{iDCG@2} = \frac{3/\log_2 3}{3 + 2/\log_2 3} = \frac{3}{3 \log_2 3 + 2} \approx 0.444

Occurrences#

SemesterTaskPointsSubtopics
DBIS Exam WS1516 (not published)Task 120ptsdot-product similarity, AP, rank decomposition
DBIS Exam WS1617 (not published)Task 120ptstf.idf matrix, tf-to-tf.idf function, P@2, AP
DBIS Exam WS1718 (not published)Task 120ptsP@2, P@R, AP, DCG, nDCG, dcg/ndcg functions
DBIS Exam WS1920 (not published)Task 125ptsZipf’s law, nDCG function, exponential search
DBIS Exam WS2122 (not published)Task 125ptstf.idf matrix, dot-product, P@k, galloping search
DBIS Exam WS2223 (not published)Task 320ptsterm-document matrix, hyperplane, F1 score
DBIS Exam WS2324 (not published)Task 120ptstf scores, merge function, P@2/P@R/AP/DCG

Lectures#

  • DBIS_VL01 — keyword search, inverted index, construction, Zipf’s law
  • DBIS_VL02 — scored postings, tf, idf, tf.idf, BM25
  • DBIS_VL11 — term-document matrix; sparse row-major matrix = inverted index; BM25 entries
  • DBIS_VL13 — example exam task on ranking and precision
  • Evaluation — measures how good the produced ranking is (P@k, AP, nDCG, bpref)
  • List Intersection and Search — algorithms for intersecting and merging the inverted lists
  • Fuzzy Search and Q-Grams — a q-gram index has the same structure as an inverted index (q-grams → word IDs instead of words → document IDs), and candidate filtering reuses the same intersection and merge algorithms
  • Word Embeddings and Language Models — vector space model and cosine similarity generalize the dot-product ranking

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only