liveBook · Manning

Similarity metric

As explained in AI-Powered Search

A similarity metric is a numerical measure that quantifies how closely two representations or objects correspond.

A similarity metric assigns a numerical score to how closely a query, document, phrase, or other representation corresponds to another. Search systems use the score to compare candidates and rank results. section 3.1.1 section 13.8

For text search, represent both inputs in a shared space before comparing them. A vocabulary-based representation gives every known term a dimension, while dense embeddings encode contextual meaning. Cosine similarity compares separately generated embeddings; cross-encoders instead encode the query and document together and return a context-sensitive score. section 3.1.1 section 13.8

Choose the comparison strategy according to the ranking task:

  • Use vector similarity to retrieve semantically related candidates, including phrases whose wording differs but whose meaning is related. section 13.4.3
  • Use a cross-encoder to rerank a smaller candidate set when shared query-document context matters more than independent encoding speed. section 13.8
  • Account for scale: comparing every pair among n phrases produces n² comparisons and increases computational and storage costs. section 13.4.3

From “apple juice” to ranked documents

Map the query “apple juice” and each document into the same term vector space. The query has nonzero values for “apple” and “juice”; linear algebra then measures its similarity to each document. In a semantic example, embeddings rank “it’s raining hard” close to “it is wet outside,” even though the wording differs. section 3.1.1 section 13.4.3

Listing 13.28 Reranking search results with a cross-encoder
from sentence_transformers import CrossEncoder
cross_encoder = \  #1
  CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")  #1

query = "mountain hike"
search_results = semantic_search_with_engine(  #2
  embeddings_collection, query, limit=50)  #2
pairs_to_score = [[query, doc["title"]]  #3
                  for doc in search_results]  #3
cross_scores = cross_encoder.predict(pairs_to_score,  #4
                 activation_fct=torch.nn.Sigmoid())  #5
reranked_results = rerank(search_results, cross_scores) #6
display_results(query, reranked_results[:10])

A runnable reranking pipeline shows how to generate an initial semantic-search result set, score query-document pairs with a cross-encoder, and reorder the results. section 13.8

How AI-Powered Search covers it

How deep it goes
Explains vector construction, embedding comparison, cross-encoder scoring, reranking, and quadratic all-pairs cost without deriving metric mathematics.
Examples
Uses apple-juice document vectors, weather and vehicle phrases, and mountain-hike cross-encoder reranking.
What it assumes
Assumes basic vectors and search ranking, while introducing dense representations, embeddings, cosine comparison, and cross-encoder scoring.
What you can do after
Apply it via sentence-transformers to compare embeddings, sort similarity scores, and rerank search results.
How it connects
Connects vector representations, embeddings, cosine similarity, semantic search, bi-encoders, cross-encoders, and ranking.

Also explained in

Compare how 10 books explain similarity metric →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Search. The section links point to the parts of the book it draws on.