Skip to main content

The Algorithmic Trick That Makes Search Engines Fast

Understanding the inverted index and the real architecture behind search engines

Updated 5 min readMarkdown
The Algorithmic Trick That Makes Search Engines Fast

The web contains billions of pages, yet search engines return relevant results in milliseconds.

How is that even possible?

The answer is surprisingly simple: search engines don’t search the web when you type a query. They pre-process the web ahead of time and store the results in a highly optimised data structure called an inverted index.

Your search is really just a fast lookup operation.


The inverted index approach...

Think of it like the index at the back of a textbook. The book doesn't reprint itself to help you find "photosynthesis" - it just says page 47, 112, 203. The inverted index works the same way, but for billions of web pages.

Let us understand with a simple example:

After indexing, the inverted index looks like this:


How Search Engines Build the Index?

It is done in 5 stages:

  • Crawling (discovering web pages) - Search engines use automated programs called crawlers or spiders to move from one web page to another by following links. As they visit pages, they download and read the content so it can later be added to the search index.

    Search engines like Google run billions of these crawls every day, which is why newly published pages can appear in search results within hours.

  • Text analysis (preparing the content) - Each page is cleaned and processed before being added to the index.

    The search engine:

    • converts text to lowercase

    • splits sentences into words

    • removes common words like “the”, “is”, and “a”

    • converts similar words into a common form (running → run)

    This helps treat “Python”, “python”, and “PYTHON” as the same word in the index.

  • Posting (writing to the index) - For every word it finds, the search engine stores a record containing:

    • term - the word

    • pageID - the page it appeared in

    • tf - how many times it appeared

    • positions - where it appeared in the page

    These records are combined into a giant lookup structure called the inverted index, which is stored across thousands of servers.

  • Scoring signals (collecting ranking data) - Alongside the index, the engine collects ranking signals for each page including:

    • PageRank - how many trusted websites link to the page

    • how fast the page loads

    • how recently it was updated

    • whether it works well on mobile devices

Continuous re-indexing - The web is constantly changing, so search engines keep revisiting pages to check for updates.

Popular websites like news or social media platforms may be crawled every few minutes, while less active pages might only be checked once in a while.


How a Query Is Processed?

Step What Happens
1. You type python data science
2. Analyse query Lowercase → tokenize → remove stop-words → stem
Result python data science
3. Index lookup Look up python → python-blog.com, pandas-tutorial.io
Look up data → python-blog.com, ml-guide.dev, pandas-tutorial.io
Look up science → python-blog.com
4. Intersect Pages containing all 3 terms → python-blog.com
Pages with 2 terms → pandas-tutorial.io (python + data)
5. TF-IDF score For each candidate page, score = sum of (tf × idf) for each query term
idf = log(total pages / pages containing term)
Rare terms score higher
6. Final ranking TF-IDF score + PageRank + freshness + 200 + other signals
Sort descending → top results returned
7. Time elapsed ~450 milliseconds

TF-IDF and BM25 - How Search Results Are Ranked

The inverted index helps search engines find matching pages. But matching isn’t enough. If millions of pages contain the word “Python”, which one should appear first? That’s where ranking algorithms like TF-IDF and BM25 come in.

TF-IDF ranks pages using two ideas:

  1. Term Frequency (TF) - How often does the word appear in this page? A page mentioning “Python” 10 times is probably more relevant than one mentioning it once.

  2. Inverse Document Frequency (IDF) - How rare is the word across all pages? Common words like “the” appear everywhere, so they carry little value. Rare words are more useful for ranking. Rarer words = higher weight.
    IDF(t) = log( N / df(t) )

Search engines combine TF and IDF to calculate a relevance score for each page.

Why BM25 Exists?

TF-IDF works well, but it has limitations: longer pages naturally contain more words repeating a word too many times keeps increasing the score BM25 improves this by: adjusting for document length reducing the impact of excessive repetition.

That’s why modern search engines and tools like Elasticsearch use BM25 instead of plain TF-IDF.


Search engines feel instant, but the heavy work happens long before you type a query.

By continuously crawling pages, building inverted indexes, and ranking results using algorithms like TF-IDF and BM25, search engines can return relevant results from billions of pages in just milliseconds.

The same core idea also powers:

  • Gmail search

  • GitHub code search

  • VS Code file search

  • e-commerce search

  • database full-text search

Different products. Different scale.
But the same fundamental idea underneath.

— Abhigna
Console Diaries — a developer’s notes
Connect with me on LinkedIn: a6h1gna

No comments yet. Be the first to comment.