AI EngineeringInterview preparation
Foundation·Concept·2 min read

Keyword search and BM25

Practice this question

Overview

  • Keyword search matches words or indexed terms rather than learned meaning.
  • BM25 is a common relevance formula that rewards useful matching terms, accounts for document length, and avoids letting repeated words dominate indefinitely.
  • It is usually implemented over an inverted index: a map from terms to documents.

Why this matters

Exact names, clause codes, policy numbers, and rare terms are often retrieved better lexically.

An everyday example

Searching a book index for “Clause 7.2” is more precise than describing what the clause might mean.

A technical example

Searching POL-2026-42 should prioritize documents containing that actual identifier.

How it works

  1. Analyze text into terms
  2. Build term/document index
  3. Analyze query
  4. Score matching documents
  5. Sort and filter.

Architecture and flow

Query terms → inverted index/BM25 → ranked matches.

The insurance assistant example

Find the exact exclusion code mentioned in a letter before explaining its wording.

When to use it

Exact identifiers, product terminology, and search requiring transparent lexical matching.

When to avoid it

Pure keyword search may fail “vehicle collision” versus “car accident” when no shared terms exist.

Interview answer

“BM25 is strong for exact terminology and identifiers. Semantic search handles paraphrases, so I often combine them.”

Benefits, trade-offs, and production considerations

  • Tokenization, stemming, stopwords, language analyzers, and synonyms affect results.
  • Preserve identifier fields separately; stripping punctuation may damage an exact policy-code search.
Adapted from 13_AI.MD · “Keyword search and BM25”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.