Overview

5 Word embeddings based ranking

This chapter explains how a search engine translates a user’s query into an ordered list of results and why ranking quality is central to perceived relevance. After queries are analyzed and matched in inverted indexes, a ranking function scores candidate documents so that the most relevant appear first—crucial because users rarely look beyond the first page. Beyond pure term matching, effective ranking can and should incorporate contextual signals such as user history, location, and recency, all of which can boost documents aligned with the user’s likely intent.

The chapter first reviews traditional retrieval models. Statistical models like TF-IDF within the Vector Space Model represent queries and documents as high-dimensional bag-of-words vectors and rank via cosine similarity; they are simple and strong baselines but ignore word order, grow with vocabulary size, and have limited semantic awareness. Probabilistic models such as Okapi BM25 dampen runaway term-frequency effects and improve length normalization, yet still operate as bag-of-words with heuristic approximations. In Lucene, these models are encapsulated by the Similarity API, enabling practical configuration and experimentation. The chapter then pivots to neural information retrieval, showing how dense word embeddings (e.g., word2vec) provide compact, semantically meaningful representations. By averaging word vectors to build query/document embeddings and comparing them with cosine similarity—and further “smoothing” with term frequency or TF-IDF weights to emphasize informative terms—one can correct failures of pure frequency-based ranking and better reflect intent.

To move from promising examples to robust practice, the chapter emphasizes evaluation. Using Lucene4IR and trec_eval on benchmark data, it compares TF-IDF, BM25, language models, and a custom WordEmbeddingsSimilarity that scores with averaged, smoothed embeddings. Results show that embeddings can slightly outperform classic baselines for top-k precision and NDCG, though gains depend on data and tuning; no single model dominates universally. A practical takeaway is to measure against clear objectives (precision, recall, NDCG) and consider hybrid ranking: combining multiple scoring functions (e.g., embeddings with BM25 or language models) often yields the best overall effectiveness. The chapter concludes that modern ranking benefits from mixing semantic embeddings with proven lexical signals, guided by systematic evaluation and real-world constraints.

Figure 5.1. Querying, retrieving and ranking
ranking mm
Figure 5.2. Cosine similarity
cosinesim
Figure 5.3. Word vectors for words bernhard, riemann and influence
wordvectors
Figure 5.4. Averaging bernhard, riemann and influence word vectors
wordvectors mean
Figure 5.5. Similarity between query and document embeddings
avgwvsim
Figure 5.6. Averaged word vector smoothed by term frequencies
wordvectors tf
Figure 5.7. Averaged word vector smoothed by TF-IDF
wordvectors tfidf

 Summary

  • Classic retrieval models like VSM and BM25 provide good baseline for ranking documents but also lack semantic understanding of text capabilities
  • Neural information retrieval models aim to provide better semantic understanding capabilities for ranking documents
  • Distributed representations of words (like the ones generated by word2vec) can be combined together to generated document embeddings for queries and documents
  • Averaged word embeddings can be used to generate effective Lucene Similarities which can achieve good results when evaluated against IR datasets

FAQ

What does “ranking” mean in search, and why is it so important?Ranking is the process of ordering retrieved documents by estimated relevance to a query. Good ranking surfaces the most useful results in the first positions, boosting precision and user satisfaction while reducing the need to scan many results.
How do TF, DF, TF‑IDF, and the Vector Space Model work?TF counts how often a term appears in a document; DF counts in how many documents a term appears. TF‑IDF weights terms higher if they are frequent in a document but rare across the collection. In the Vector Space Model, documents and queries are vectors of term weights and are compared via cosine similarity.
What are the main limitations of TF‑IDF and bag‑of‑words VSM?- Ignores word order and syntax (bag of words).
- Scores can be skewed by repeated terms or document length without proper normalization.
- Very high‑dimensional, sparse vectors that grow with vocabulary size.
- Limited semantic understanding (exact term matching bias).
How does Lucene handle ranking, and which Similarities are available?Lucene uses the Similarity API to score documents. ClassicSimilarity implements VSM+TF‑IDF; BM25Similarity (default since Lucene 6) implements Okapi BM25; language‑modeling similarities (e.g., LMJelinekMercerSimilarity) and others are also available. Set the same Similarity at indexing and search time.
Why is BM25 often preferred over plain TF‑IDF?BM25 dampens the impact of very high term frequency (saturation) and normalizes for document length more robustly, generally producing better rankings. It’s still a bag‑of‑words model and relies on approximations, so it may struggle with very long documents or special collections.
What is neural information retrieval, and why use embeddings?Neural IR represents text (words, queries, documents) as dense vectors (embeddings) that capture semantic similarity. These fixed‑size vectors are compact and can retrieve semantically related content even without exact term matches.
How do averaged word embeddings rank documents, and what can go wrong?Compute an embedding for a query and for each document by averaging their word vectors, then score with cosine similarity. Pitfalls include poor embeddings from too little training data and inflated scores for documents dominated by a single query term (e.g., repeated words).
How does smoothing with TF or TF‑IDF improve averaged embeddings?When forming document vectors, divide each word vector by a weighting factor (e.g., term frequency or TF‑IDF). This shifts the document embedding toward rarer, more informative terms and reduces the influence of frequent or repeated words, improving ranking quality.
How can I evaluate ranking quality for different models in practice?Use Lucene4IR to index and retrieve over a fixed dataset with various Similarities, then assess effectiveness with trec_eval metrics such as MAP, R‑prec, P@k, and NDCG. This enables fair, repeatable comparisons and data‑driven tuning.
Should I combine neural and classic ranking models?Often yes. Mixing embeddings‑based scoring with classic models (e.g., BM25 or language models) via MultiSimilarity can yield better overall effectiveness (e.g., improved NDCG), leveraging complementary strengths of lexical and semantic signals.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Deep Learning for Search ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Deep Learning for Search ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Deep Learning for Search ebook for free