liveBook · Manning

Term frequency-inverse document frequency

As explained in Elasticsearch in Action, Second Edition

A term frequency–inverse document frequency algorithm ranks documents using query-term frequency in documents and term rarity across the collection.

TF-IDF ranks documents by combining two signals: how often a query term appears in one document and how rare that term is across the collection. Frequent terms in a specific document receive more weight, while terms common across many documents receive less. section 3.4.2

Ranking Java book titles

For a search for Java, “Mastering Java: Learning Core Java and Enterprise Java With Examples” contains Java three times, while “Effective Java” and “Head First Java” contain it once. TF-IDF therefore favors higher term frequency, while inverse document frequency reduces the value of terms appearing broadly across the collection.

Document length also affects practical relevance. A term repeated in a short title can be more useful than the same term appearing in a much longer field, so field choice matters when comparing matches. section 3.4.1

Use TF-IDF as a baseline for understanding Elasticsearch relevance:

  • Elasticsearch applies similarity algorithms per field through mappings and can select or customize similarity when the default is unsuitable. section 3.4.2
  • BM25 improves on TF-IDF with nonlinear term-frequency saturation and document-length normalization; BM25 is Elasticsearch’s default. section 3.4.2

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Defines term and document frequency, demonstrates their relevance relationship, and stops short of detailed mathematical derivation.
Examples
Uses Java book titles, term-frequency counts, document-frequency effects, and comparisons between title and synopsis fields.
What you can do after
Evaluate why documents receive different relevance scores and when BM25 may better suit Elasticsearch search results.
How it connects
Connects TF-IDF to BM25, relevancy scores, similarity algorithms, field length normalization, and full-text queries.

Also explained in

Compare how 10 books explain term frequency-inverse document frequency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.