Skip to content

What is semantic search and how does it differ from traditional keyword search?

Beginner · 2 min read · RAG & Vector Databases

Open in study mode to save this question and track progress

Short answer: Semantic search retrieves documents based on conceptual meaning and intent in vector space regardless of exact word overlap; traditional keyword search (e.g. BM25 / TF-IDF) relies on exact lexical string matching between query and document tokens.

Detail

Keyword search struggles when users ask queries using synonyms, misspellings, or alternative phrasing.

Query: "How to fix a leaky faucet?"
Keyword Search (BM25): Looks for exact strings "fix", "leaky", "faucet". Misses "Repair dripping tap".
Semantic Search:       Embeds query intent -> Retrieves "Guide to repairing a dripping water tap".

Core Comparison

MetricLexical Keyword Search (BM25)Semantic Vector Search
Matching MechanismExact token string overlapDistance between vector embeddings
Synonym AwarenessPoor (Requires manual synonym dictionaries)High (Inherent in pre-trained embeddings)
Domain-Specific TermsHigh (Excels at exact part numbers / SKUs)Moderate (Can miss rare exact codes without fine-tuning)
Index TypeInverted IndexHNSW / IVFFlat Graph Indexes

Example

Comparing BM25 lexical match vs Cosine vector similarity in Python:

python
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np

docs = ["How to fix a dripping water tap", "Database index optimization guide"]
query = "repair leaky faucet"

# Lexical TF-IDF match (Zero overlap -> 0 score)
tfidf = TfidfVectorizer()
matrix = tfidf.fit_transform([query] + docs)
scores = (matrix[0] * matrix[1:].T).toarray()
print("Lexical Match Scores:", scores) # Returns 0.0 for both because exact words don't match

Interview tips

  • Highlight why enterprise RAG systems use Hybrid Search (combining BM25 for exact SKU codes + Dense Vectors for semantic intent).
  • Explain cross-encoder reranking as the final stage of hybrid search pipelines.

Related Concepts


⬅ Back to RAG and Vector Databases · All topics