OCT 22, 2026· 3 min read
ArchitectureEngineeringRagbm25

Building BM25 from Scratch in Python: Understanding Every Line of a Search Engine Updated

What happens when you sea

Section Heading

This is a block quote Systems are designed for simplicity; complexity is the tax of deferred understanding.
ComputeTailLatency.tsTYPESCRIPT
// Sample TypeScript Code Runner
function computeLatencyP99(samples: number[]): number {
  const sorted = [...samples].sort((a, b) => a - b);
  const idx = Math.floor(sorted.length * 0.99);
  return sorted[idx] || 0;
}

const measurements = Array.from({ length: 1000 }, () => Math.random() * 4.2);
const p99 = computeLatencyP99(measurements);
console.log("Computed p99 tail latency:", p99.toFixed(3), "ms");
Console Output
Computed p99 tail latency: 4.158 ms
Pipeline Latency VisualizerInteractive Visual
Pipeline Latency VisualizerInteractive Visual

rch for something?

Imagine that you have a collection of documents and a user enters this query: python machine learning

A search engine needs to answer three questions:

  1. Which documents are relevant to the query?
  2. How relevant is each document?
  3. In what order should the documents appear?

Consider this small collection:

documents = [ "Python is a programming language.", "Machine learning uses algorithms to learn from data.", "Python is useful for machine learning.", "A guide to cooking rice and vegetables.", "Python machine learning machine learning machine learning."]

This is the Second Heading

Does heading gets a sepereator?

Another Second Heading

Section Heading

Subsection Title

Suppose we search for python machine learning.

Document 1 contains python, document 2 contains machine and learning, and document 3 contains all three query terms. Document 5 contains repeated occurrences of machine learning. If I want to write another paragraph, should I keep writing or can I also give an enter?

This is another paragraph. Nice

Document 1 contains python, document 2 contains machine and learning, and document 3 contains all three query terms. Document 5 contains repeated occurrences of machine learning.

A basic search could return every document containing at least one query word. But this does not tell us which document is most relevant.

Document 3 matches the entire query naturally. Document 5 contains many repetitions, which may indicate relevance, but repetitions should not necessarily give it an unlimited advantage. Document 4 has no matching terms and should not appear among the relevant results.

The central problem is ranking: matching words is not enough; we need a principled way to score documents. 1.2 Lexical retrieval Lexical retrieval finds documents by matching words or tokens.

For example, a lexical search for Python programming can match a document containing those words, even if it does not use the exact phrase.

Lexical retrieval is particularly useful for:

  • Product names and identifiers.
  • Legal section numbers.
  • Tax form names.
  • Error messages.
  • Exact terminology.
  • Technical phrases appearing in documents.

However, lexical retrieval does not inherently understand meaning. A search for car does not automatically match automobile unless the retrieval system has some additional mechanism for handling synonyms or related terms.

ComputeTailLatency.tsPYTHON
documents = [
    "Python is a programming language.",
    "Machine learning uses algorithms to learn from data.",
    "Python is useful for machine learning.",
    "A guide to cooking rice and vegetables.",
    "Python machine learning machine learning machine learning."
]
Console Output
Computed p99 tail latency: 4.158 ms

New paragraph text. Write with clarity, precision, and depth.

More from · Issue 01 · Systems, Language & Computation