Glossary
TF-IDF
TF-IDF (term frequency, inverse document frequency) is a weighting scheme that scores a word's importance to a document by how often it appears there and how rare it is across the collection.
TF-IDF, short for term frequency, inverse document frequency, is a way of scoring how important a word is to one document within a larger collection. It multiplies two numbers: how often the word appears in the document, and how rare the word is across all documents. A word that appears often in one document but rarely elsewhere gets a high score, while common words such as "the" or "and" score close to zero. TF-IDF has been a foundation of keyword search and text analysis since the 1970s, and its ideas still sit inside modern ranking functions.
The two halves of the formula
Term frequency (TF) measures how often a term appears in a document. The simplest version is a raw count: if “refund” appears six times in a help article, its term frequency is six. Raw counts favour long documents and repeated words, so most systems dampen them, commonly with 1 + log(count), or divide by the document’s length so a word that fills a short note is weighted fairly against one scattered through a long manual.
Inverse document frequency (IDF) measures how much information a term carries across the collection. If a collection holds N documents and a term appears in df of them, the classic IDF is log(N / df). A term that appears in every document has an IDF of log(1), which is zero. A term found in only a handful of documents has a high IDF. The idea, introduced by Karen Spärck Jones in 1972, is that rare terms are better at telling documents apart.
The TF-IDF weight of a term in a document is the product of the two. Each document can then be represented as a vector with one dimension per word in the vocabulary, where most entries are zero because any single document uses only a small part of the vocabulary. These are sparse vectors, and they are stored efficiently in an inverted index that maps each term to the documents containing it.
How TF-IDF ranks search results
To use TF-IDF for search, the query is turned into a vector the same way as the documents, and each document is scored by how closely its vector matches the query’s. The usual measure is cosine similarity, which compares direction rather than length, so a long document is not favoured simply for containing more words. Documents that share the query’s rare, distinctive terms rise to the top; documents that share only common words fall away.
This is the core of classic keyword search and full-text search. Before a term is weighted, the text usually passes through tokenization and normalisation steps such as lowercasing, stemming or lemmatization, so that “refunds”, “refunded” and “refund” count as the same term. Stop-word lists remove the most common words entirely, though IDF already pushes their weight towards zero.
A worked example
Take a support knowledge base of 10,000 articles and the query “chargeback refund policy”. Using base-10 logarithms for readability:
Term | Documents containing it | IDF = log(10,000 / df) |
|---|---|---|
the | 9,900 | 0.004 |
policy | 2,000 | 0.70 |
refund | 500 | 1.30 |
chargeback | 20 | 2.70 |
Now consider two articles. Article A mentions “refund” eight times and “policy” five times but never “chargeback”. Article B mentions “chargeback” four times and “refund” twice. With log-scaled term frequency, Article A’s strongest weight comes from “refund” at about 1.3 times 1.9, or 2.5. Article B’s “chargeback” contributes about 2.7 times 1.6, or 4.3, on top of a smaller “refund” weight. Article B ranks higher because it contains the rarest and most specific word in the query, which is what a person asking about chargebacks wants.
The example also shows the method’s blind spot. An article titled “Disputed card payments” that never uses the word “chargeback” scores zero for that term, even if it is the best answer. TF-IDF only rewards exact term overlap after normalisation.
From TF-IDF to BM25
Most production keyword search engines today use BM25, which grew out of the same probabilistic retrieval research and can be read as a refined TF-IDF. BM25 adds two corrections. Term frequency saturates, so the tenth mention of “refund” adds far less than the second. And document length is normalised against the collection average, with a tunable parameter controlling how strongly. In most benchmarks BM25 beats plain TF-IDF ranking, which is why it is the default in Lucene-based engines and many others.
TF-IDF remains common outside search ranking. It is a standard baseline in text classification, a quick way to extract keywords that characterise a document, a feature for clustering, and a cheap signal for spotting near-duplicates. Many machine learning libraries include a TF-IDF vectoriser for exactly these jobs.
Strengths and limits
TF-IDF is fast, transparent and needs no training. You can read off why a document matched: these terms, with these weights. It handles exact identifiers well, such as error codes, product SKUs, legal citations and personal names, which dense retrieval models sometimes blur because they map text into a space of meanings rather than strings. It also adapts automatically to a collection, since IDF is computed from the documents themselves, and updating it as documents arrive is cheap compared with retraining a model.
Its limits are the mirror image. TF-IDF has no notion of meaning, so synonyms and paraphrases do not match, and a word with two senses is treated as one. It ignores word order, so “dog bites man” and “man bites dog” look identical. It is sensitive to spelling, which is why search systems add fuzzy search or typo tolerance on top. And because weights depend on the collection, a term that is rare in one corpus may be common in another, so scores are not comparable across indexes.
These limits are what semantic search addresses. An embedding model maps text to dense vectors where similar meanings sit close together, so “chargeback” and “disputed card payment” can match. The trade-off and the cases where each wins are covered in semantic search vs keyword search and BM25 vs embeddings.
TF-IDF in modern retrieval
Rather than choosing one, most current systems combine them. Hybrid search runs a keyword method such as BM25 alongside dense vector search and merges the result lists, often with reciprocal rank fusion. The keyword side catches exact names and codes; the dense side catches meaning. A reranking model can then reorder the merged shortlist.
Learned sparse models such as SPLADE keep the sparse, inverted-index form of TF-IDF but let a neural model decide which terms matter and add related terms the document never used, a kind of built-in query expansion. They show how the structure TF-IDF introduced has outlived the original weighting.
Retrieval-augmented generation pipelines also use TF-IDF and BM25 directly. A retriever that must find passages mentioning a specific contract clause number or API name often performs better with a keyword component, and evaluation of either approach comes down to measures such as precision and recall. Comparisons of search engines built on these methods, such as Exabase vs Elasticsearch and Exabase vs Typesense, show how the keyword layer fits into wider platforms.
Practical tips
Fit IDF on a corpus that resembles what you will search. Weights learned from general web text will under-rate terms that are common on the web but rare in your domain, and the reverse. Recompute periodically as the collection grows, because a term’s rarity changes over time.
Keep identifiers intact during tokenization. Splitting “ERR-4012” into “err” and “4012” loses the very precision that makes keyword search worth having. Some teams index both the split and unsplit forms.
Treat field boundaries as signals. A query term found in a title usually deserves more weight than the same term in a footer, and most engines let you weight fields separately rather than treating a document as one bag of words.
Decide how to handle languages early. Stemmers and stop-word lists are language-specific, so a corpus that mixes English, German and Spanish needs either per-language analysis or a fallback that skips stemming. Compound-heavy languages such as German may also need decompounding so that a search for one part of a long word still matches.
Check the variant your library uses. Scikit-learn, for example, smooths IDF by adding one to document counts and applies sublinear term frequency only when asked, and these defaults change rankings noticeably. When comparing results across tools or papers, confirm the exact formula before concluding that one method is better.
Where Exabase fits
Exabase does not expose TF-IDF or BM25 parameters, and it does not need a separate keyword index from you. Deep Search treats every text query as hybrid by default, combining typo-tolerant keyword matching with semantic search, so exact names, codes and phrases match alongside paraphrases. Wrapping a phrase in double quotes forces an exact match when that is what you need, and a precision parameter between 0 and 1 controls how wide or narrow the results are.
Content stored through Resources is indexed automatically at write time, including text inside PDFs, transcribed audio and video, and bookmarked pages, and results come back as matched chunks with page numbers or timestamps. Each tenant’s index stays inside its own Base. The searching resources guide shows the query options, the RAG accuracy solution explains why passage-level matching matters for answers, and the knowledge bases use case shows the pattern in an application. If your project calls for hand-tuned keyword scoring, such as custom field boosts or a specialised analyser, a dedicated search engine gives you that control.
Frequently asked questions
What does TF-IDF stand for?
TF-IDF stands for term frequency, inverse document frequency. Term frequency counts how often a word appears in a document, and inverse document frequency measures how rare the word is across the collection. Their product is the word’s weight.
Is TF-IDF still used?
Yes. It is a common baseline for text classification, keyword extraction and clustering. For search ranking, most engines use BM25, a refinement of the same idea, often combined with semantic search.
What is the difference between TF-IDF and BM25?
BM25 adds term-frequency saturation and tunable document-length normalisation to the TF-IDF idea. Repeated words add diminishing value, and long documents are not over-rewarded, which usually gives better rankings.
Is TF-IDF semantic search?
No. TF-IDF matches exact terms after normalisation and has no notion of meaning, so synonyms do not match. Semantic search uses embeddings to match by meaning, and hybrid search combines the two.
Why does IDF use a logarithm?
The logarithm keeps weights in a useful range. Without it, a term found in one document out of a million would outweigh everything else by a huge factor; the log compresses that so rare terms matter more without dominating.
Does TF-IDF work for short texts like tweets or titles?
It works, but term frequency carries little information when most words appear once, so the score is driven almost entirely by IDF. Short texts often benefit more from embeddings or hybrid search.
Does Exabase use TF-IDF?
Exabase does not expose TF-IDF settings. Deep Search combines typo-tolerant keyword matching with semantic search on every text query, and content is indexed automatically when stored.
Related pages: BM25, Keyword search, Inverted index, Sparse vectors, Hybrid search, Stemming, BM25 vs embeddings, Deep Search, When keyword beats semantic: 25 real queries, information retrieval