Back to Generative AI Notes
Topic #91

Semantic Similarity

Semantic similarity measures how close two pieces of text are in meaning — computed by comparing their embedding vectors, not by comparing their literal words.

Semantic vs Keyword Similarity — The Core Distinction

QueryKeyword Matching Would FindSemantic Search Also Finds
"cheap flights to Paris"Documents literally containing "cheap," "flights," "Paris""affordable airfare to France," "budget-friendly Paris travel deals" — no shared exact words

This is the single biggest practical advantage of semantic search over traditional keyword search — matching intent and meaning, not just vocabulary overlap.

How Similarity Is Actually Computed

query_vector = embed("cheap flights to Paris")
doc_vector_1 = embed("affordable airfare to France")
doc_vector_2 = embed("best hiking trails in Switzerland")

similarity_1 = cosine_similarity(query_vector, doc_vector_1)  # high
similarity_2 = cosine_similarity(query_vector, doc_vector_2)  # low

# Rank documents by similarity score, return the most similar

See Cosine Similarity for exactly how that score is calculated.

Semantic Similarity Isn't Perfect

It can occasionally match on topical similarity rather than actual answerable relevance — e.g. a query about "canceling a flight" might semantically match closely with "booking a flight" content, since both are strongly flight-related, even though they're not what the user actually needs. This is a real, known limitation, part of why hybrid search (combining semantic with keyword-based matching) and reranking exist as complementary techniques.

Practical Use Case

Any search or retrieval feature where users phrase things differently than how content is written — customer support search, internal documentation search, RAG retrieval — benefits substantially from semantic similarity over pure keyword matching.

Common Mistakes

  • Assuming semantic similarity always outperforms keyword search — for exact-match needs (product codes, specific technical terms, proper nouns), keyword or hybrid approaches can be more reliable
  • Not evaluating retrieval quality on real queries, assuming semantic search "just works" without measuring actual relevance

Interview Relevance

"Why would semantic search find a relevant document that keyword search misses?" — the "affordable airfare to France" / "cheap flights to Paris" example above is exactly the kind of concrete answer expected.

Practice Question

Give an example query where semantic search alone might retrieve a topically related but actually unhelpful result, and explain why.

Want to go beyond the notes?

Join Coding Now Tech Institute's Generative AI course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Semantic Similarity – FAQs

Quick answers about learning Semantic Similarity in Generative AI.

This free note from Coding Now Tech Institute explains Semantic Similarity in Generative AI — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Generative AI topic on Coding Now Tech Institute, including Semantic Similarity, is 100% free with no signup required.
With focused practice, most students grasp Semantic Similarity in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now