Intermediate8 min read6 of 10

Sparse Encoding: Fast, Interpretable, and Underrated

TF-IDF sparse vectors explained — why 99.97% of the vector is zeros, how that makes search 10x faster than dense, and when sparse consistently beats semantic.

Sparse Encoding: Fast, Interpretable, and Underrated

Dense vectors compress meaning into 768 floats — every dimension is active, and none of them map to anything human-readable. Sparse vectors take the opposite approach: they have 50,000+ dimensions, but 99.97% are zero. The non-zero dimensions correspond to actual terms in your vocabulary.

Dense vs. sparse comparison

Dense vector for "expensive smartphone": [0.23, -0.87, 0.41, 0.12, ...] ← 768 opaque floats, all non-zero Sparse vector for "expensive smartphone": { "expensive": 0.85, "smartphone": 0.79, "phone": 0.52, "device": 0.31, } ← 4 of 50,000 possible terms active, rest are zero

You can read a sparse vector and understand exactly what it says. That interpretability is a production advantage — debugging bad results is far easier when you can see which terms drove a match.

How sparse encoding works

TF-IDF (Term Frequency–Inverse Document Frequency) is the foundation:

TF(term, doc) = how often this term appears in this document IDF(term) = log(total_docs / docs_containing_term) TF-IDF(t, d) = TF × IDF

High IDF = the term is rare across the corpus → it's distinctive. High TF = the term appears frequently in this document → it's important here. A term that appears in every document (like "the") has IDF ≈ 0 and contributes nothing.

Sparse encoding step-by-step

The full pipeline:

  1. Tokenize the text
  2. Remove stop words
  3. Calculate TF per document
  4. Calculate IDF across corpus
  5. Multiply TF × IDF
  6. L2-normalize the resulting vector
  7. Store only non-zero values (the sparse representation)

Why sparse search is 10x faster

Dense similarity requires a dot product across all 768 dimensions — one number per dimension per document.

Sparse similarity only processes overlapping terms:

Query: {"expensive": 0.85, "smartphone": 0.79} Doc A: {"expensive": 0.72, "phone": 0.54} ← 1 overlap Doc B: {"laptop": 0.88, "screen": 0.61} ← 0 overlaps, skip entirely

Sparse similarity calculation

Documents with zero query-term overlap are never touched. In practice, a query with 5 terms overlaps with only ~0.1% of a large corpus — the other 99.9% are skipped entirely. This is why sparse search is 10× faster:

MethodP95 latencyMemory (1M docs)
Dense (768d)45ms~3 GB
Sparse (TF-IDF)5ms~300 MB

When sparse beats dense

Query typeDense NDCG@10Sparse NDCG@10Winner
"affordable smartphone" (synonym)0.910.52Dense
Exact product code "SKU-12345"0.680.89Sparse
Technical term "NullPointerException"0.640.84Sparse
Conceptual "heart disease"0.930.47Dense
Log trace ID search0.300.95Sparse

Sparse wins on controlled vocabularies: product SKUs, error codes, function names, legal case numbers, log trace IDs — any query where the user knows the exact term.

[Key Insight] Use sparse search when your users search with terms from a controlled vocabulary: catalog codes, error strings, API method names, specification numbers. These are tokens that dense vectors encode poorly because they're treated as rare or unknown during embedding model training.

Learned sparse expansion

Standard TF-IDF fails on synonyms: the word "expensive" doesn't expand to "costly". Learned sparse models fix this by adding weighted related terms at reduced weight:

Base encoding: {"expensive": 0.85, "smartphone": 0.79} After expansion: {"expensive": 0.85, "smartphone": 0.79, "costly": 0.43, ← added at 50% weight "pricey": 0.43, ← added at 50% weight "phone": 0.37} ← added at lower weight

Learned sparse expansion

Expansion factor (typically 0.3–0.5) controls how aggressively synonyms are added. Too high and you lose the precision advantage of sparse; too low and recall doesn't improve.

Models trained end-to-end for sparse expansion (like the SPLADE family) consistently outperform manual synonym dictionaries because they learn domain-specific relationships from data.

Best use cases for sparse encoding

  • E-commerce: SKU search, exact product name match
  • Technical documentation: function names, configuration keys, error codes
  • Legal and compliance: case numbers, statute citations, exact terminology
  • Log search: trace IDs, IP addresses, structured log fields
  • Medical records: ICD codes, drug names, procedure codes

All of these share one property: users know exactly what they're looking for, and the terms are precise.