Module 5: Keyword vs Semantic Search

4. Comparison: Keyword vs Semantic Search

Overview

Here you compare keyword search and semantic search side by side: advantages, limitations, use cases. You'll understand that they're not mutually exclusive: many systems use both (hybrid search).


Full comparison

AspectKeyword SearchSemantic Search
What it searches forExact wordsMeaning (similar vectors)
Synonyms❌ No (without a dictionary)✅ Yes (similar embeddings)
Related concepts❌ No✅ Yes
Exact words✅ Yes (guaranteed)⚠️ Not guaranteed
Speed✅ Very fast (inverted indexes)✅ Fast (with HNSW/IVF)
Setup costLow (just index the text)Medium (generate embeddings)
Query costMinimalMedium (embedding the query)
Explainable✅ Yes (it shows the matched words)⚠️ Hard (the score is abstract)
Scalability✅ Millions of docs✅ Millions of docs

Successes and failures

Case 1: A query with synonyms

Query: "car"

Documents:

  • Doc A: "The automobile needs gasoline"
  • Doc B: "The vehicle has 4 wheels"

Keyword:

  • Doc A: ❌ No match (it doesn't contain "car")
  • Doc B: ❌ No match

Semantic:

  • Doc A: ✅ Match (the embedding of "automobile" is similar to "car")
  • Doc B: ✅ Match (the embedding of "vehicle" is similar to "car")

Winner: Semantic search ✅


Case 2: A query with an exact code

Query: "error ABC-12345"

Documents:

  • Doc A: "Error ABC-12345 in the system"
  • Doc B: "A similar error ABC-12346"

Keyword:

  • Doc A: ✅ Exact match (it contains "ABC-12345")
  • Doc B: ❌ No match (a different code)

Semantic:

  • Doc A: ✅ Match (a similar embedding)
  • Doc B: ⚠️ It can get a high score (the embeddings of similar codes are close together)

Winner: Keyword search ✅ (exact precision is critical)


Case 3: A conceptual query

Query: "how to improve my app's speed"

Documents:

  • Doc A: "A performance optimization guide"
  • Doc B: "Tips to make your application faster"

Keyword:

  • Doc A: ⚠️ Partial match ("speed" doesn't appear)
  • Doc B: ⚠️ Partial match ("improve" doesn't appear as such)

Semantic:

  • Doc A: ✅ Match (the embeddings of "improve speed" ~ "optimize performance")
  • Doc B: ✅ Match (the embeddings of "speed" ~ "faster")

Winner: Semantic search ✅ (it understands synonyms and intent)


Decision matrix

Query typeBest methodExample
Exact (codes, names, numbers)Keyword"error E404", "article 42"
Conceptual (topics, questions)Semantic"How do I optimize?", "animals"
Exploratory (discovering related items)Semantic"topics about AI"
Legal/medical (precision is critical)Keyword + filters"diagnosis ICD-10 J44"
Ambiguous (multiple meanings)Hybrid"bank" (filter by context)

Summary

Key points:

  • Keyword: Precise for exact matches, doesn't understand synonyms
  • Semantic: Understands meaning, doesn't guarantee exact matches
  • They aren't mutually exclusive: Combine them in hybrid search
  • The decision: It depends on the type of query

Next capsule: 05-hybrid-search.md — Combining keyword + semantic.