Search Engine (Elasticsearch, OpenSearch, Meilisearch)
Inverted-index-based full-text and structured search — the right tool for 'find me records matching this query' when SQL LIKE isn't fast enough.
Why it exists
SQL LIKE '%foo%' is O(N) — a full table scan. At any real scale, full-text search on a relational database is unusable. Search engines flip the model: they build an inverted index (word → list of document IDs containing it) so queries are O(matches), not O(rows). They also handle relevance scoring, faceted filtering, fuzzy matching, and aggregation in a way that's painful in SQL.
How it works
At index time, each document is analyzed: tokenized into terms, lowercased, stemmed, and possibly filtered (stopwords). Each term is added to the inverted index pointing to the doc ID. At query time, the search engine tokenizes the query the same way, retrieves candidate docs from the index, scores them (BM25 by default), and returns the top-K. Sharding splits the index across nodes; queries fan out and results are merged. Elasticsearch uses Lucene under the hood.
Scaling characteristics
A single Elasticsearch node handles ~10K QPS for typical queries; complex aggregations are 10-100× slower. Sharding scales writes and index size linearly. Replication provides read scale + fault tolerance. Index size dominates memory: keep the working set in RAM for fast queries. Rule of thumb: 20-30 GB per shard, ~1 shard per CPU core.
When to use it
- Full-text search over documents, articles, or products
- Faceted search + filters (e-commerce: brand, price range, in-stock)
- Log aggregation and search (Elasticsearch + Kibana = ELK stack)
- Autocomplete and suggestion
- Ad-hoc analytics on structured data with high cardinality
- Geo-spatial queries (find restaurants within 2 miles)
When NOT to use it
- Transactional storage — search engines aren't ACID; treat them as a derived index
- Small datasets where SQL indexes work fine
- Queries requiring cross-document JOINs — search engines are single-index by design
- Very high write rates without careful sharding
Failure modes
- Split-brain during network partition (Elasticsearch quorum matters) — set minimum_master_nodes carefully
- Index corruption during power loss without proper flush settings — always enable translog fsync
- Query overload from complex aggregations — set search timeouts + circuit breakers
- Reindexing during schema changes takes hours to days — plan capacity and use aliases for zero-downtime cutover
- Cluster overload from too many small shards — every shard has overhead; keep them large
Alternatives
- PostgreSQL full-text search (pg_search, tsvector) — good enough for <10M docs; keeps you on one datastore
- Meilisearch / Typesense — simpler, developer-friendly, great for typo-tolerance out of the box
- Algolia — hosted, best-in-class relevance out of the box, expensive at scale
- Vector search (pgvector, Pinecone, Qdrant) — for semantic search, replaces or complements keyword search
Interview questions
- Explain the difference between an inverted index and a B-tree. When would you prefer which?
- How do you handle a schema change on a 1TB Elasticsearch index?
- What are the trade-offs of using Elasticsearch as your source of truth vs an index derived from Postgres?
- How would you build autocomplete for 100M product names with typo tolerance?
- Your Elasticsearch cluster is running out of heap. What's your diagnostic sequence?
- How does BM25 scoring differ from TF-IDF, and when does it matter?
Systems that use this component
See how the real designs on this platform put search-engine to work — concrete usage context per system.
Twitter/X timeline
Elasticsearch for tweet search (~500B tweets indexed)
Open systemElasticsearch for hashtag and user search
Open systemSlack
Elasticsearch for message search per workspace
Open systemYouTube
Custom search — Google-scale infrastructure
Open systemDropbox
Elasticsearch for file search across workspace
Open system1990, Cornell: the inverted index changes how computers find text
In the late 1980s, if you wanted to search text, you scanned files linearly. Grep worked. Real search didn't. Then Gerard Salton at Cornell — a librarian's son who'd spent 30 years on the SMART system — codified the algorithms that made real search possible: the inverted index, TF-IDF scoring, and cosine similarity. His 1988 book Automatic Text Processing is still cited today. When Salton died in 1995, Sergey Brin was reading his papers in a Stanford library.
The industrialization came in stages. Apache Lucene (1999, Doug Cutting) turned Salton's algorithms into production-grade Java. Apache Solr (2004, Yonik Seeley) wrapped Lucene in a REST API. Elasticsearch (2010, Shay Banon) added distributed operation and JSON. OpenSearch forked from Elasticsearch in 2021 after license disputes. Meanwhile, Google went in a different direction — proprietary systems with PageRank + neural signals. Modern search stacks combine lexical (Lucene-family) and semantic (vector embeddings) approaches.
2023 changed everything. LLMs made vector search mainstream. Every major search engine added HNSW indexes for approximate nearest neighbor. Pinecone, Weaviate, Qdrant emerged as pure vector DBs. Retrieval-Augmented Generation (RAG) became the dominant AI pattern — and RAG is just "semantic search + LLM." Suddenly search is at the center of every modern AI product.
The core insight: search is not "LIKE '%foo%'". It's a ranked retrieval system — you don't want "matches yes/no," you want "which 10 documents best match this query, ranked by relevance?" Lexical search answers "does the query appear?". Semantic search answers "is this document about the query?". Modern search does both, then blends the results with rerankers to give you the best of each.
Historical timeline
- 1960sSMART project at CornellGerard Salton starts building the algorithms of information retrieval. Foundation for all future search.
- 1975Vector space model formalizedSalton et al. publish "A Vector Space Model for Automatic Indexing." Documents as vectors, queries as vectors, cosine similarity as ranking.
- 1988TF-IDF codifiedSalton's Automatic Text Processing book crystallizes term-frequency-inverse-document-frequency scoring.
- 1994Okapi BM25 publishedStephen Robertson at City University London formalizes BM25 — a probabilistic improvement over TF-IDF. Still the industry default lexical scorer.
- 1998Google + PageRankSergey Brin + Larry Page publish PageRank at Stanford. Link-based ranking. Transforms web search.
- 1999Apache Lucene 1.0Doug Cutting releases Lucene — Java library implementing Salton's algorithms production-grade. Powers most non-Google search for the next 20 years.
- 2004Apache SolrYonik Seeley builds a REST/HTTP wrapper around Lucene. Makes Lucene approachable for web developers.
- 2010Elasticsearch 1.0Shay Banon releases Elasticsearch — distributed, JSON-native Lucene. Explodes in popularity. Becomes THE search stack of the 2010s.
- 2013Word2VecMikolov et al. at Google publish Word2Vec. Words as dense vectors. Foundation for modern semantic search.
- 2018BERT + neural rerankersGoogle's BERT model shows transformers understand context better than any prior model. Search engines integrate BERT for reranking.
- 2019HNSW paper maturesMalkov & Yashunin's Hierarchical Navigable Small Worlds becomes the standard ANN index. Powers Pinecone, Weaviate, Milvus.
- 2021OpenSearch forks from ESAWS + Elastic license dispute → OpenSearch fork. Enterprise splits between the two. Open-source vs SaaS narrative.
- 2022Pinecone + Weaviate + Qdrant GAPurpose-built vector DBs achieve production maturity. Emerging category.
- 2023RAG becomes the AI paradigmGPT-4 + vector search = every AI app. Search is at the center of AI infrastructure.
- 2024Hybrid search dominatesBM25 + vector fusion (reciprocal rank fusion). LLM rerankers. Elastic, OpenSearch, Vespa all ship first-class hybrid.
The inverted index — foundation of full-text search
An inverted index flips the data model on its head. Instead of "doc → words," it's word → docs. Look up any term → get the list of docs containing it. O(1) per term. Try searching:
Analyzer pipeline: from raw text to searchable tokens
Before text hits the index, it goes through an analyzer: tokenize → normalize → stem → filter. Same pipeline runs on both indexed docs and queries — so "Running" matches "runs." Type in text and watch each stage:
Scoring: BM25, the industry default
"Matches" isn't enough — you need to rank. Lexical search uses BM25 (Best Match 25), a probabilistic model that scores each doc by:
score(D, Q) = Σ IDF(q_i) · (f(q_i, D) · (k1 + 1))
─────────────────────────────
f(q_i, D) + k1·(1 - b + b·|D|/avgdl)Vector search: understanding meaning, not just keywords
Lexical search finds documents that share words with the query. Vector search finds documents that share meaning. The trick: represent each document as a high-dimensional vector (typically 384-1536 dims) computed by an embedding model. Similar meanings → nearby vectors → cosine similarity ranks them.
Lexical (BM25)
Query: "cheap flights to Paris" → matches docs containing exact words.
✗ "Budget airfare France" (different words!)
Precise but literal. Misses synonyms and paraphrases.
Semantic (vector)
Query embedding is a 768-dim vector. Compared to doc embeddings via cosine.
✓ "Budget airfare France" (similar MEANING)
Understands paraphrasing. Great for RAG + Q&A.
HNSW: how vector search stays fast at scale
Comparing your query vector against 100M doc vectors linearly = too slow. HNSW (Hierarchical Navigable Small Worlds) is the industry-standard approximate nearest-neighbor index. Builds a multi-layer graph where top layers are sparse (long jumps) and bottom is dense (fine-grained). Search starts at the top, greedy-descends. O(log n) instead of O(n).
Scaling: shards for capacity, replicas for throughput
A single Lucene index maxes out around 100M-1B docs. Beyond that: shard — split the index across nodes. Add replicas to serve more queries in parallel. Elasticsearch/OpenSearch default: 1 primary + 1 replica.
Product comparison
| Product | Type | Model | Strength | Weakness |
|---|---|---|---|---|
| Elasticsearch | General | OSS (SSPL) + Managed | Massive ecosystem. Best-known. Rich query DSL. Kibana visualization. | Elastic License change (2021). JVM ops. Cost at scale. |
| OpenSearch | General | OSS (Apache 2.0) + AWS Managed | Free of Elastic license constraints. AWS-backed. Feature parity with ES7. | Community catch-up post-fork. Some diff from Elastic latest features. |
| Apache Solr | General (older gen) | OSS | Mature. Battle-tested at large orgs (Bloomberg, Netflix legacy). Robust config. | UX less modern than ES. Community activity lower. |
| Vespa | General + ML-native | OSS + Cloud | Serves at Yahoo scale. First-class vector search + tensor ranking. Real-time indexing. | Steeper learning curve. Smaller community than ES. |
| Meilisearch | General | OSS + Cloud | Instant search UX out of the box. Fast, simple. Great DX. | Not for petabyte scale. Fewer features than ES. |
| Typesense | General | OSS + Cloud | Sub-50ms P99 search. Simple ops. Alternative to Algolia. | Younger. Fewer integrations. |
| Algolia | General (managed) | Managed only | Best-in-class DX. Sub-50ms globally. Great for e-commerce site search. | Expensive at scale. Vendor lock-in. |
| Pinecone | Vector DB | Managed | First mover in managed vector search. Simple API. Great for RAG. | Only vectors. Expensive. Multi-tenant, some scaling limits. |
| Weaviate | Vector + hybrid | OSS + Cloud | OSS. Hybrid search built in. GraphQL API. Rich modules for embedding. | Younger. Complex configuration. |
| Qdrant | Vector DB | OSS + Cloud | Rust performance. Payload filtering rich. Free tier generous. | Smaller ecosystem than Pinecone. |
| Milvus | Vector DB | OSS + Zilliz cloud | Scales to billions of vectors. Industrial-grade. Kubernetes-native. | Operational complexity for self-hosted. |
| pgvector (Postgres extension) | Vector in SQL DB | OSS | Vector search inside your Postgres. Free. Zero new infra. HNSW support (0.5+). | Not as fast as pure vector DBs at billion-scale. Single-node ceiling. |
How to choose: Enterprise general search → Elasticsearch or OpenSearch. Site search managed → Algolia or Typesense. Vector-heavy RAG → Pinecone (managed) or Qdrant (OSS). Small budget or already on Postgres → pgvector. Yahoo-scale + ML ranking → Vespa.
12 real-world search deployments
Elasticsearch for code search
GitHub's repo + code search runs on a custom Elasticsearch fork. Millions of repos indexed. Tokenizes code smartly (respects camelCase, snake_case). Recently replaced with Blackbird for scale — but ES powered them for a decade.
Elasticsearch for driver search
Uber uses ES to find nearby drivers in real-time via geo queries + ES aggregations. Handles million-QPS at peak. Multi-region deployment for locality.
CirrusSearch on Elasticsearch
Wikipedia's search backend, CirrusSearch, is Elasticsearch. Indexes ~60M articles. Serves 100M+ searches/day. Custom scoring for wiki-specific relevance (redirects, categories).
Elasticsearch → in-house TimeSeries + logs
Netflix used ES for logs, metrics, security events at massive scale. Recently split some workloads to in-house tools. The famous "Mantis" project used ES for real-time event debugging.
Elastic + LLMs for product search
Shopify runs their storefront product search on Elasticsearch. Recently added semantic search via OpenAI embeddings. Millions of merchant storefronts, billions of products.
Vector search for RAG in Enterprise
ChatGPT Enterprise's document search uses vector embeddings + retrieval before feeding relevant chunks to GPT-4. This is RAG. Every enterprise AI product does this now.
Meilisearch → OpenSearch migration
Notion's search grew from Meilisearch to OpenSearch. Full-text + document titles + fuzzy matching. Query-suggestion powered by learned models.
Custom search + LLM synthesis
Perplexity does web search + LLM answering — retrieval-augmented generation at product scale. Their "search engine" is a hybrid of Bing web + LLM reranking + citation generation.
Elasticsearch for the world's Q&A
Stack Overflow indexes ~20M questions in Elasticsearch. Custom scoring that weights answers-with-accepted-answer higher. Handles >100M searches/mo.
Solr + custom ranking for e-commerce
Etsy uses Apache Solr for product search on 100M+ listings. Custom ranking uses TF-IDF + machine-learned CTR features. Personalized results per user.
Custom search built on OpenSearch
Airbnb's listing search runs on OpenSearch with heavy custom ranking. Location, availability, price, amenities all weighted. Millions of geographic queries daily.
Legacy Solr for financial news
Bloomberg Terminal's document search runs on Apache Solr. Regulatory filings, news, research reports — millions of docs, sub-100ms search. Old but rock-solid.
Key takeaways
- 1Search is ranked retrieval, not filtered SELECT. Get 10 best-matching docs, ranked by relevance.
- 2The inverted index (term → posting list of doc IDs) is the core data structure. O(1) per term.
- 3The analyzer pipeline (tokenize → lowercase → stem → filter) must run identically on docs and queries. Change it = reindex.
- 4BM25 is the industry-default lexical scorer. Beats TF-IDF. Tunable via k1 and b.
- 5Vector search (HNSW + cosine similarity) finds documents by meaning. Standard for RAG.
- 6Hybrid search (BM25 + vector, fused) beats either alone. Production RAG uses this.
- 7Scale via shards + replicas. Shards for capacity, replicas for query throughput.
- 8For most workloads: Elasticsearch or OpenSearch for general. Pinecone/Qdrant/Weaviate for pure vector. Algolia for managed site search. pgvector when you're already on Postgres.
References & further reading
- • Salton, G. et al. (1975). "A Vector Space Model for Automatic Indexing." CACM. Foundation of modern search.
- • Salton, G. (1988). Automatic Text Processing. Addison-Wesley. The bible.
- • Robertson, S. et al. (1994). "Okapi at TREC-3." The BM25 paper.
- • Manning, C., Raghavan, P., Schutze, H. (2008). Introduction to Information Retrieval. Cambridge. Free online. The modern textbook.
- • Malkov, Yu. A. & Yashunin, D. A. (2018). "Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs." The HNSW paper.
- • Devlin, J. et al. (2018). "BERT: Pre-training of Deep Bidirectional Transformers." The paper that made neural rerankers mainstream.
- • Elasticsearch Docs: "A Practical Introduction to the Vector Space Model" blog + "Query DSL" reference.
- • Lucene JavaDoc: Actually reading the source is faster than reading blogs about it.
- • Doug Turnbull, John Berryman (2016). Relevant Search. Manning. Excellent practical scoring guide.
- • Vespa Documentation: The best hybrid + ML-ranking reference in the OSS world.
- • Pinecone Learning Center: Best vector-search-for-devs content anywhere.