Real-World Applications of Strings
Search Engines and Indexing
Read aloud is unavailable in this browser.
The Miracle of Modern Search
When you type a query into a search engine and receive results in a fraction of a second, you're witnessing one of the most remarkable feats of software engineering. Behind that deceptively simple interface lies an intricate system that has indexed hundreds of billions of web pages, processed petabytes of text, and built data structures that can pinpoint relevant documents among trillions of possibilities—all before you finish reading this sentence.
Search is fundamentally a string problem. Every step of the journey—from crawling web pages and extracting text, to building inverted indices and matching query terms, to ranking results by relevance—involves string manipulation at unprecedented scale. Understanding how search engines work reveals the extraordinary power of string-based data structures and algorithms.
But search engines are just the most visible example. The same principles apply to:
- Database full-text search
- Code search tools (searching across millions of files)
- Log analysis systems (finding patterns across billions of log entries)
- E-commerce product search
- Email search (scanning years of correspondence)
By the end of this page, you will understand the fundamental data structures and algorithms that power search systems—particularly the inverted index. You'll see how string processing concepts like tokenization and normalization combine with sophisticated data structures to enable efficient full-text search at scale.
The Fundamental Challenge
To appreciate search indexing, consider the naive alternative: linear search.
Imagine searching for the phrase "data structures" across a collection of documents. The naive approach would:
- Open each document
- Scan its contents for the query string
- Record matches and move to the next document
For a small personal collection, this might work. But consider the numbers for a web-scale search engine:
- ~200+ billion indexed web pages (estimated for major search engines)
- Average page size: ~100 KB of text
- Total indexed text: ~20 petabytes
Even at SSD speeds of ~500 MB/s, linearly scanning this data would take:
20,000,000 GB ÷ 0.5 GB/s ≈ 40 million seconds ≈ 1.3 years
Users expect results in milliseconds, not years. This gap—from years to milliseconds—is bridged by indexing: preprocessing documents to build data structures that make queries fast.
| Approach | Preprocessing | Query Time | Space Overhead |
|---|---|---|---|
| Naive linear scan | O(1) — none | O(total document size) | O(1) — none |
| Inverted index | O(total document size) | O(query terms × docs with term) | ~20-100% of document size |
The core insight:
Instead of asking "which documents contain this query?" at query time (expensive), we ask "for each term, which documents contain it?" at index time (amortized over many queries). This inverts the relationship between terms and documents—hence the name inverted index.
This is a classic DSA trade-off: precompute once, query many times. We invest O(n) preprocessing time to enable O(1) or O(log n) query time. The same principle underlies hash tables, binary search trees, and many other data structures we'll study.
Indexing is a prime example of the fundamental DSA insight: you can often trade preprocessing time for query time. If you'll answer many queries over the same data, preprocessing becomes extremely valuable. This is why indices exist in databases, search engines, and many other systems.
Building Blocks — Tokenization Revisited
Before we can index documents, we must break them into searchable units. This brings us back to tokenization, but now with search-specific considerations.
What constitutes a token for search?
In search systems, tokens are typically words or terms—the units users will query for. But defining "word" is surprisingly complex:
- Is "state-of-the-art" one word or four?
- Is "don't" one word or two?
- What about URLs, email addresses, or phone numbers?
- How do we handle languages without whitespace (Chinese, Japanese)?
- Are "Run", "run", and "running" the same for search purposes?
These questions have no universal answers; different search systems make different choices based on their use cases.
- •Whitespace and punctuation: Basic delimiters, but punctuation within tokens (e.g., "C++") needs special handling
- •Case folding: Converting to lowercase so "Python" matches "python"
- •Normalization: Handling accents, Unicode equivalences, and character variations
- •Numbers: Whether to index numeric values and in what form
- •Special characters: Handling symbols, emojis, and domain-specific notation
- •Compound words: German "Bundesrepublik" vs. English "Federal Republic"
Stemming and Lemmatization:
To improve recall (finding more relevant documents), search systems often reduce words to their root forms:
Stemming: A rule-based approach that chops off word endings.
- "running" → "run"
- "computers" → "comput"
- "computational" → "comput"
Stemming is fast but can produce non-words ("comput" isn't a word) and occasionally conflates unrelated terms.
Lemmatization: A linguistically informed approach using dictionaries.
- "better" → "good"
- "was" → "be"
- "running" → "run"
Lemmatization produces valid words but requires language-specific resources and is slower.
Stop Word Removal:
High-frequency words with low information content ("the", "a", "and", "is") are often excluded from the index:
- Reduces index size significantly
- Speeds up query processing
- But can hurt phrase queries ("to be or not to be")
Modern search engines have moved away from aggressive stop word removal, instead handling them specially for phrase queries while still optimizing for common terms.
A code search engine tokenizes differently than a web search engine. An email search handles "@" and "." differently than a document search. The tokenization choices you make fundamentally shape what can be found. There is no perfect tokenizer—only appropriate ones for specific use cases.
The Inverted Index
The inverted index is the cornerstone data structure of search engines. It inverts the natural document-to-terms relationship, instead mapping from terms to documents.
Conceptual structure:
term → [list of document IDs containing the term]
For example, given three documents:
- Doc 1: "data structures and algorithms"
- Doc 2: "algorithms for data science"
- Doc 3: "machine learning algorithms"
The inverted index would look like:
algorithms → [1, 2, 3]
data → [1, 2]
learning → [3]
machine → [3]
science → [2]
structures → [1]
Now, to find documents containing "algorithms", we simply look up "algorithms" in this mapping—O(1) with a hash table or O(log n) with a sorted structure—and immediately get [1, 2, 3].
Postings Lists:
The lists of document IDs are called postings lists or posting lists. In practice, they contain more than just document IDs:
| Field | Purpose | Use Case |
|---|---|---|
| Document ID | Unique document identifier | Core retrieval |
| Term frequency | How many times term appears in doc | Relevance scoring |
| Positions | Where in the document term appears | Phrase queries, proximity search |
| Field | Which field (title, body, etc.) | Field-specific boosting |
| Offsets | Character positions for highlighting | Snippet generation |
Building an inverted index:
The index construction process:
- Document acquisition: Crawl or load documents
- Parse and extract text: Handle HTML, PDF, etc.
- Tokenize: Break text into terms
- Normalize: Apply case folding, stemming, etc.
- Build postings: Record which documents contain which terms
- Sort and merge: Organize for efficient storage and retrieval
- Compress: Reduce index size
For very large collections, this is done in parallel across many machines, with intermediate results merged together.
Index compression:
Postings lists can be huge. For a common term like "the" in a web index, the postings list might contain billions of document IDs. Compression is essential:
- Variable-byte encoding: Use fewer bytes for small numbers
- Delta encoding: Store differences between consecutive IDs rather than absolute IDs
- Gamma/Elias coding: Compact representations for small integers
- Block compression: Compress chunks of postings together
Well-compressed indices are typically 25-30% of the original text size, while still allowing fast random access to any postings list.
Conceptually, an inverted index is a hash map where keys are terms and values are lists of documents. This mental model helps: term lookup is O(1), and the list retrieval is O(k) where k is the number of matching documents. The efficiency comes from this pre-computed mapping.
Query Processing
With an inverted index in place, query processing becomes a series of set operations on postings lists.
Single-term queries:
The simplest case: look up the term, return its postings list.
- Query: "algorithms"
- Lookup: index["algorithms"] → [1, 2, 3]
- Result: Documents 1, 2, 3
Boolean queries:
Combine terms with AND, OR, and NOT:
-
AND (intersection): Documents containing ALL terms
- Query: "data AND algorithms"
- Compute: [1, 2] ∩ [1, 2, 3] = [1, 2]
-
OR (union): Documents containing ANY term
- Query: "machine OR data"
- Compute: [3] ∪ [1, 2] = [1, 2, 3]
-
NOT (difference): Documents containing first but not second
- Query: "algorithms NOT machine"
- Compute: [1, 2, 3] − [3] = [1, 2]
Efficient intersection:
Intersection is the most common operation (AND queries). For two sorted postings lists, we can intersect in O(n + m) time using a merge-like algorithm:
- Maintain pointers to both lists
- If elements equal, add to result, advance both
- Otherwise, advance the pointer to the smaller element
- Repeat until either list is exhausted
This is the same algorithm used in merge sort—a fundamental DSA technique applied to search.
Phrase queries:
Matching exact phrases requires knowing where terms appear, not just which documents contain them.
- Query: "data structures"
- Check: Does "structures" appear immediately after "data" in any document?
This requires positional indices—postings lists that include term positions. We intersect documents, then verify that positions differ by exactly 1.
Phrase indexing significantly increases index size (positions are verbose) but is essential for many queries.
Proximity queries:
A generalization of phrase queries: terms must appear within n words of each other.
- Query: "algorithms" NEAR/5 "efficiency"
- Check: Are these terms within 5 positions of each other?
This is useful when exact phrase matching is too strict but any document containing both terms is too loose.
The implementation is similar to phrase queries but allows a position difference up to n rather than exactly 1.
Query optimization:
Real search systems optimize query execution in sophisticated ways:
- Process terms in order of selectivity: Start with rare terms (short postings lists) to minimize intermediate result sizes
- Skip lists: Jump over large portions of postings lists that can't contribute to results
- Early termination: For ranked retrieval, stop once top-k results are stable
- Cached results: Store results for popular queries
- Query rewriting: Expand synonyms, fix spelling, suggest alternatives
These optimizations can reduce query processing time by orders of magnitude.
Query processing is fundamentally about set operations: intersection, union, difference. The efficiency of these operations on sorted lists—O(n + m) rather than O(n × m)—is why sorted postings lists are the standard representation. This is DSA in action.
Relevance Ranking
Finding matching documents is only half the battle. For most queries, far more documents match than a user wants to see. The search engine must rank results by relevance, showing the best matches first.
Term frequency (TF):
A document mentioning "algorithms" 10 times is likely more relevant to that query than one mentioning it once. The term frequency captures this:
TF(t, d) = count of term t in document d
But raw counts are problematic. A 10,000-word document will naturally have more term occurrences than a 100-word document. Solutions include:
- Logarithmic TF: 1 + log(TF) dampens high frequencies
- Normalized TF: Divide by document length
Inverse document frequency (IDF):
Not all terms are equally important. "The" appears in almost every document; "algorithm" is more selective. Inverse document frequency captures term importance:
IDF(t) = log(N / DF(t))
Where N is total documents and DF(t) is documents containing term t.
Rare terms have high IDF; common terms have low IDF.
TF-IDF scoring:
Combining these gives the famous TF-IDF score:
Score(t, d) = TF(t, d) × IDF(t)
For a multi-term query, sum the TF-IDF scores of all query terms. This simple formula remains surprisingly effective.
| Term | TF in Doc | DF | IDF (N=1000) | TF-IDF |
|---|---|---|---|---|
| the | 50 | 999 | 0.001 | 0.05 |
| algorithms | 5 | 100 | 1.0 | 5.0 |
| efficient | 2 | 50 | 1.3 | 2.6 |
Beyond TF-IDF: Modern Ranking:
While TF-IDF remains a core signal, modern search engines use hundreds of ranking factors:
Document-level signals:
- PageRank or other link-based authority measures
- Document freshness and update frequency
- Domain authority and trust
- User engagement metrics (click-through rate, dwell time)
Query-document signals:
- BM25 (a sophisticated TF-IDF variant)
- Proximity of query terms in the document
- Term location (title, heading, body)
- Field-specific weighting
Machine learning:
- Learning-to-rank models trained on user behavior
- Neural retrieval models (BERT, transformers)
- Personalization based on user history
Practical implementation:
Ranking must be fast—computing scores for millions of candidates per query isn't practical. Search engines use tiered approaches:
- First pass: Fast boolean retrieval to get candidate set
- Second pass: Score candidates with TF-IDF or BM25
- Re-ranking: Apply expensive ML models to top candidates only
Building an inverted index is well-understood engineering. Ranking is where search engines compete. Google's success came not from finding matching documents but from ranking them better than competitors (initially via PageRank, now via sophisticated ML). Retrieval is solved; ranking is where innovation happens.
Specialized Index Structures
The basic inverted index handles exact term matching. But real search needs additional capabilities, requiring specialized data structures.
Tries for Prefix Search:
Autocomplete—suggesting queries as users type—requires efficient prefix search. Tries (prefix trees) excel here:
For vocabulary: ["algorithm", "algorithms", "algebra", "alpha"]
(root)
|
a
/
l lpha
/
gebra gorithm(s)
Looking up all words starting with "alg" traverses to the "alg" node and collects all descendants. This is O(prefix length + results) rather than O(vocabulary size) for linear scan.
N-gram Indices:
For substring search ("gor" finding "algorithms"), we can build indices on character n-grams:
- 3-grams of "algorithms": ["alg", "lgo", "gor", "ori", "rit", "ith", "thm", "hms"]
An index mapping n-grams to words enables substring matching. Searching for "gor" returns ["algorithms", "categories", ...]. This enables more flexible matching than whole-word indices.
Soundex and Phonetic Indices:
For names and words that might be misspelled but sound similar, phonetic algorithms encode words by pronunciation:
- "Smith", "Smyth", "Smythe" → all encode to "S530"
Indexing by phonetic code enables fuzzy matching for names and spoken words.
- •Trie / Prefix tree — Efficient prefix search, autocomplete, dictionary operations
- •Suffix tree / Suffix array — All substring matching, but expensive to build
- •N-gram index — Practical substring matching via character chunks
- •Phonetic index — Fuzzy matching based on pronunciation
- •Spatial index — Geographic search (e.g., "restaurants near me")
- •Temporal index — Time-range queries (e.g., "news from last week")
- •Vector index — Semantic similarity search using embeddings (modern neural search)
Combining indices:
Production search systems often combine multiple index types:
- Inverted index for primary term lookup
- Trie for autocomplete
- N-gram index for spell correction
- Vector index for semantic matching
Queries may hit multiple indices, with results merged and ranked. This layered architecture enables sophisticated search experiences while maintaining performance.
Each index type represents a different trade-off between construction cost, storage size, and query performance. A trie enables O(k) prefix search but uses more memory than a sorted list. An n-gram index enables substring matching but explodes index size. Choosing the right index for each query type is essential for balanced search systems.
Scale and Distributed Search
No single machine can hold the entire web index or process thousands of queries per second. Web-scale search requires distributed systems that spread data and computation across thousands of machines.
Sharding strategies:
Document sharding: Each shard holds complete documents. Different documents go to different shards.
- Pros: Query processing is embarrassingly parallel
- Cons: Every query must hit every shard (to find all matching documents)
Term sharding: Each shard holds postings lists for different terms.
- Pros: Simple queries hit fewer shards
- Cons: Complex queries (AND, phrase) require coordination across shards
Most production systems use document sharding with replicas for fault tolerance.
Query fan-out:
For document-sharded indices, a query:
- Arrives at a coordinator
- Fans out to all index shards in parallel
- Each shard finds and scores local matches
- Results stream back to the coordinator
- Coordinator merges and re-ranks the top results
This architecture allows linear scaling: double the shards to handle double the data or double the query load (with replication).
| Metric | Value | Implication |
|---|---|---|
| Indexed pages | 200+ billion | Distributed storage essential |
| Index size | 100+ petabytes | Thousands of machines |
| Queries per second | 100,000+ | Massive replication for throughput |
| Query latency | <200ms | Parallelism and caching critical |
| Index updates | Billions/day | Near-real-time indexing |
Replication and fault tolerance:
With thousands of machines, failures are constant. Each shard is replicated across multiple machines:
- Read replicas: Serve queries from any replica
- Leader-follower: Updates go to leader, propagate to followers
- Automatic failover: If a replica fails, others handle its load
The system must also handle:
- Network partitions (machines becoming unreachable)
- Slow replicas (not failed, just slow)
- Rolling updates (updating software without downtime)
Caching layers:
Query patterns follow power laws: a small percentage of queries account for a large percentage of traffic. Aggressive caching dramatically reduces load:
- Query result cache: Store top-k results for popular queries
- Postings cache: Keep hot postings lists in memory
- CDN caching: Geographic distribution of cached results
Cached queries can be served in microseconds, reserving computation for novel or long-tail queries.
Distributed search introduces challenges beyond the core algorithms: network latency, partial failures, consistency, and coordination overhead. A query touching 1000 shards is only as fast as the slowest shard (tail latency). Managing this complexity is a significant engineering challenge.
KEEP LEARNING
Keep learning for free.
Read the complete lesson, save your notes and pick up where you left off with a free account. No card needed.