Skip to main content
Ceisium logo Ceisium
Rare query terms separating a small set of documents from common matches in an IDF ranking diagram.
Rare query terms separating a small set of documents from common matches in an IDF ranking diagram.

Rare query terms separating a small set of documents from common matches in an IDF ranking diagram.

What is Inverse Document Frequency?

Inverse document frequency helps search rank rare query words above words that appear everywhere. Learn the idea with a simple docs-search example.

Inverse document frequency sounds like a formula term, but the idea starts with a very normal search problem.

A user searches your docs for:

TEXT
hmac webhook setup

The search engine can find pages that contain those words. That is matching. But after matching, it still has to rank the pages.

This is where the problem appears. setup may show up across hundreds of docs pages. webhook narrows the field. hmac probably appears on only a few pages, and those pages are much more likely to answer the query.

Inverse document frequency, or IDF, is the small ranking idea that handles this difference. It asks the search system to treat words that appear everywhere as weaker signals, and words that appear in only a few documents as stronger signals.

What is inverse document frequency?

IDF is a way to measure how much a term helps separate documents inside one searchable collection.

Here, a document can be a docs page, product page, support article, API page, or any other page the search engine can return.

The collection is the full set of those searchable documents.

For each term, IDF asks:

TEXT
How many documents contain this term?

That one count changes how much the term should count during ranking.

Rare terms separate. Common terms blur. IDF is not judging whether a word is useful in normal language. It is judging whether the word separates documents inside this collection.

In our example collection, the counts may look like this:

TermPages containing the termIDF intuition
setup700 pagesweak signal
webhook90 pagesuseful signal
hmac8 pagesstrong signal

The table does not say setup is useless. It says setup does not narrow the search very much. The term is still part of the query, but it should not dominate the score.

IDF starts from the collection: a word that appears everywhere does less separating work than a word that appears on only a few pages.
Black llama-head blob using a lamp to separate rare hmac pages from many setup pages.

IDF starts from the collection: a word that appears everywhere does less separating work than a word that appears on only a few pages.

Why not just count matching words?

Imagine the query is:

TEXT
hmac webhook setup

Now compare two pages.

Page A:

TEXT
Webhook setup guideConfigure webhook endpoints and delivery settings.

Page B:

TEXT
Verify HMAC webhook signaturesUse HMAC to verify webhook payloads before accepting events.

Both pages match the query. Page A matches webhook and setup. Page B matches hmac and webhook.

A simple word-counting system may only ask:

TEXT
How many query words did this page match?

IDF adds a better question:

TEXT
Which matched words are actually good at narrowing the result set?

That is the mechanism. IDF does not replace matching. It adds weight after matching, so a focused page with the specific term can beat a broader page with a more common term.

The simple IDF formula

The common textbook shape is:

TEXT
idf(term) = log(total documents / documents containing term)

So if a collection has 1,000 pages:

TEXT
idf("setup")   = log(1000 / 700)idf("webhook") = log(1000 / 90)idf("hmac")    = log(1000 / 8)

You do not need the exact decimal values here. The direction is the useful part:

  • setup appears on many pages, so its weight is low.
  • webhook appears on fewer pages, so its weight is higher.
  • hmac appears on very few pages, so its weight is highest.

Different search systems use slightly different versions of the formula. Some add smoothing so the math behaves nicely when a term appears everywhere or almost nowhere.

The mental model stays the same:

TEXT
more documents contain the term -> lower IDFfewer documents contain the term -> higher IDF

IDF depends on the collection

A word is not rare by itself. It is rare inside a specific collection.

On a general marketing website, api may be rare. On a developer docs site, api may appear everywhere.

So the search engine has to learn IDF from the actual pages being searched. When it builds the index, it counts how many documents contain each term.

Rarity is local to the corpus. This is why IDF is a collection-level signal. The same word can get a different weight on two different websites.

This also means the weights can change over time. If you add many new pages about webhooks, webhook becomes less rare in that collection than it was before.

How IDF affects ranking

Now switch to a second query:

TEXT
api token reset

On many developer docs sites, api appears everywhere. token narrows the field. reset may point to an even smaller group of account or security pages.

IDF turns those collection counts into weights:

TEXT
api   -> common, lower weighttoken -> more specific, medium weightreset -> more specific, higher weight

Now a page about resetting API tokens has a better chance of beating a generic API overview, even if the overview repeats api many times.

IDF gives each query term a collection-level weight before the final ranking score is built.
Black llama-head blob placing api, token, and reset weights on an IDF scale.

IDF gives each query term a collection-level weight before the final ranking score is built.

This is why IDF is useful in technical search. Error codes, config keys, API fields, package names, CLI flags, and protocol names often carry the real intent.

What IDF does not do

It does not understand synonyms. If the user searches sign in and the document says login, IDF alone does not know those words mean similar things.

It also does not prove that every rare word is important. A typo can be rare too. IDF only says the term is unusual in this collection; the rest of the ranking system still has to decide whether the document is actually useful.

That is why real search systems combine IDF with other signals:

  • whether the query words appear in the document
  • how often they appear
  • where they appear, such as title or body
  • how long or focused the document is
  • other lexical or semantic retrieval steps

IDF is one part of the ranking story.

How IDF connects to TF-IDF and BM25

IDF is the second half of TF-IDF .

TF-IDF combines:

  • term frequency: how often the query term appears in this document
  • inverse document frequency: how rare the query term is across all documents

BM25 also uses an IDF-style rarity signal. But BM25 adds more ranking controls, especially term frequency saturation and document length normalization.

So if you are learning keyword ranking, the path is:

TEXT
word matching -> IDF -> TF-IDF -> BM25

IDF is the point where search stops treating every matching word as equally useful.

Summary

Inverse document frequency helps search engines answer one ranking question:

TEXT
Which matching words actually separate the useful pages from the noisy pages?

Words like setup, guide, or page can still matter, but they appear in too many places to carry the whole score. Terms like hmac, retry-after, sso, or a specific API field often narrow the result set faster.

That is why IDF is one of the basic ideas behind TF-IDF, BM25, and many keyword search systems.

Thanks for reading.

Next reads