RecSys & Search

Semantic vs Lexical Search

Contrasting exact keyword matching against dense vector embedding similarity search.

🟡 intermediate5 min readretrieval
Semantic Search and Lexical Search represent the two fundamental retrieval paradigms in search systems. Lexical Search (BM25 / TF-IDF) matches exact sparse keyword tokens between query and document text. Semantic Search (Dense Vector Retrieval) maps text into continuous embedding spaces, capturing conceptual intent independent of exact word overlap. Hybrid Search combines both methods using Reciprocal Rank Fusion to maximize search recall and precision.

The Two Foundations of Search

When a user types a query into a search bar, how should the database find relevant documents?

Two distinct paradigms exist:

┌──────────────────────────┬──────────────────────────┐
│ 1. LEXICAL SEARCH        │ 2. SEMANTIC SEARCH       │
├──────────────────────────┼──────────────────────────┤
│ Matches EXACT KEYWORD    │ Matches CONCEPTUAL INTENT│
│ tokens (BM25 / TF-IDF).  │ via Dense Embeddings.    │
│ Sparse Inverted Index.   │ Vector Database ANN.     │
│ Fast, exact SKU matching.│ Synonym & Paraphrase match│
└──────────────────────────┴──────────────────────────┘

1. Lexical Search (Sparse Inverted Index)

Lexical search algorithms (BM25, TF-IDF, ElasticSearch) treat text as a bag of sparse tokens:

$$\text{BM25}(D, Q) = \sum_{i=1}^n \text{IDF}(q_i) \cdot \frac{f(q_i, D) \cdot (k_1 + 1)}{f(q_i, D) + k_1 \cdot \left(1 - b + b \cdot \frac{|D|}{\text{avgdl}}\right)}$$

2. Semantic Search (Dense Vector Embedding)

Semantic search maps text into a continuous $d$-dimensional embedding space using deep neural networks (SBERT, OpenAI Embeddings):

$$\text{Score}(Q, D) = \text{CosineSim}(\text{Embed}(Q), \text{Embed}(D))$$

Comprehensive Head-to-Head Comparison

Query TypeLexical Search (BM25)Semantic Search (Vector)Winner
"Error Code 0x80070005"Exact Match FoundReturns generic Windows error docsLexical
"How to cure a headache?"Misses docs saying "migraine relief"Matches "migraine treatment"Semantic
"Part SKU-9842"Exact Match FoundFails (Collapses vector)Lexical
Cross-Lingual SearchFails (Different languages)Matches across languagesSemantic

The Solution: Hybrid Search

Production search systems do not choose between Lexical and Semantic search.

They execute Hybrid Search: running BM25 and Dense Vector Search in parallel and combining ranks via Reciprocal Rank Fusion (RRF).

Say this out loud

Lexical Search matches exact sparse keyword tokens using BM25 and inverted indexes, excelling at exact part numbers and error codes. Semantic Search maps text into dense embedding spaces to match conceptual intent independent of word choice. Production systems combine both via Hybrid Search to cover exact keyword precision and broad semantic recall.

Followups to expect

  1. What is SPLADE (Sparse Neural Search)? A neural model that predicts sparse token weights over full vocabulary dictionaries, combining neural semantic understanding with inverted index BM25 speed.
  2. How does Chunking impact Lexical vs Semantic Search? Lexical search scales well on full documents due to TF-IDF length normalization, while Semantic vector search requires smaller text chunks (256 to 512 tokens) to maintain dense vector embedding quality.

Check yourself

Question 1 of 3

What primary failure mode affects pure Lexical Search (BM25) when users search using synonyms or paraphrased queries?

More in RecSys & Search

See all →
Collaborative Filtering5 minThe Cold Start Problem4 minTwo-Stage: Retrieval then Ranking5 min