Search Systems (Elasticsearch, Inverted Index)
Why Not Just SQL LIKE?
-- This is SLOW on large tables (full scan, no index):
SELECT * FROM products WHERE name LIKE "%wireless headphone%";
Full-text search engines use inverted indexes: map every word to the documents containing it.
Inverted Index
Documents:
doc1: "wireless bluetooth headphone"
doc2: "wired headphone with mic"
doc3: "wireless mouse"
Inverted Index:
"wireless" → [doc1, doc3]
"bluetooth" → [doc1]
"headphone" → [doc1, doc2]
"wired" → [doc2]
"mouse" → [doc3]
"mic" → [doc2]
Query "wireless headphone" → intersection of [doc1,doc3] ∩ [doc1,doc2] = [doc1]
Elasticsearch Architecture
- Distributed (sharded + replicated)
- Near real-time (documents searchable within ~1 second)
- Relevance scoring (TF-IDF, BM25)
- Aggregations (analytics on search results)
When to Use
- Full-text search (products, articles, logs)
- Log aggregation and analysis (ELK stack)
- Autocomplete / typeahead
- Faceted search (filter by category, price range, etc.)
Key Takeaways
- Inverted index: maps words → documents (opposite of normal index)
- Elasticsearch: distributed search engine, near real-time
- Use alongside your primary DB, not instead of it
- Sync data from DB → Elasticsearch via CDC or periodic indexing