SkillPilotAI

Search Engine - Full-Text Search

Finding relevant results in milliseconds, based on the words the user typed, out of crores of products/documents.

20.1 Why is it used?

  • SQL's LIKE '%red shoes%' does a full table scan on crores of rows, is very slow, and gives no relevance ranking.
  • Users misspell words (iphnoe) or use synonyms (mobile/phone); a search engine handles fuzzy matching.
  • Filters and facets are needed: brand, price range, rating, size - with fast counts through aggregation.
  • Results must be ranked by relevance (BM25 score), popularity and stock.
  • The same technology is used for log search (ELK stack).

20.2 How it works

  • Inverted index: for each word, a list of the documents it appears in, for example 'shoes' -> [doc1, doc7, doc99]; like the index at the back of a book.
  • Analyzer: tokenizes text, lowercases, removes stopwords and applies stemming (running -> run) before indexing; the same analyzer runs on the query too.
  • Relevance scoring: BM25 (an improved TF-IDF); documents with rare words and more matches rank higher.
  • Sharding and replicas: the index is split into shards spread over nodes; replicas give read scale and HA.
  • Near real-time: a new document becomes searchable after the refresh interval (default ~1 sec).
  • Sync from DB: the DB stays the source of truth, and changes flow to the search index through CDC (Debezium) or Kafka.
  • Fuzzy search, synonyms, facets/aggregations, highlighting - the main features of e-commerce search.
  • Vector/semantic search: meaning-based search through embeddings (kNN); hybrid (BM25 + vector) is common these days.
A. Indexing: CDC sync from DB to search index
  1. 1.Seller: adds a new product
  2. 2.MySQL: source of truth
  3. 3.Debezium: CDC change
  4. 4.Kafka: topic products
  5. 5.Indexer: builds the doc
  6. 6.Elasticsearch: index

The product is searchable after the ~1 sec refresh interval

B. Analyzer + Inverted Index (like a book's back-of-book index)
  1. 1.doc7: 'Nike Running Shoes'
  2. 2.Analyzer: tokenize, lowercase, remove stopwords, stem: running -> run
  3. 3.Inverted index: nike -> doc7, doc99
  4. 4.run -> doc7, doc42
  5. 5.shoe -> doc1, doc7, doc99
  6. 6.red -> doc1, doc55

SQL LIKE '%red shoes%' = full table scan, no ranking

C. Query flow: 'nike runing shoes under 3000'
  1. 1.User query: 'nike runing shoes under 3000'
  2. 2.Query parse: fuzzy runing -> running, filter price <= 3000
  3. 3.ES coordinator: sends to every shard in parallel + brand facet agg
  4. 4.ES cluster: Shard 1, Shard 2, Shard 3 ... Shard 100, each returns local top hits (50 crore products, 100+ shards + replicas for read scale)
  5. 5.Merge + re-rank: BM25 + popularity + stock + sponsored
  6. 6.Response: top 20 results, ~100 ms

On sale day, add replicas and use a query cache

20.3 Tools you can use

ToolTypeHow it is used
ElasticsearchOpen-source(ish) distributed search engine (Lucene)Product search, log search (ELK); JSON REST API, sharding, aggregations; also available managed as Elastic Cloud.
OpenSearchOpen-source fork of Elasticsearch (Apache 2.0)AWS's managed OpenSearch Service; forked from ES 7.10, almost the same API, for logs and product search.
Apache SolrOpen-source search server (Lucene)Older and mature; enterprise/document search, distributed through SolrCloud.
AlgoliaSaaS hosted searchSuper-fast typo-tolerant search-as-you-type, ranking tuned from a dashboard; pay per search/record.
Azure AI Search / Vertex AI SearchCloud managed searchManaged search services on Azure and GCP, with AI/semantic search.
Meilisearch / TypesenseOpen-source lightweight searchAlgolia-like ease for small-medium apps; can be self-hosted.

20.4 Comparison: Elasticsearch vs OpenSearch vs Solr vs Algolia

PointElasticsearchOpenSearchSolrAlgolia
TypeLucene based, self-host or Elastic CloudES fork, self-host or AWS managedLucene based, self-hostFully managed SaaS
LicenseAGPL/SSPL/Elastic license optionsApache 2.0, fully openApache 2.0Proprietary
Speed / latencyFast, depends on tuningLike ESFastVery fast, ~ms, edge optimized
ScalingShards + replicas, you manage itSame, managed on AWSSolrCloud + ZooKeeperAutomatic
Analytics / logsVery strong (Kibana)Strong (OpenSearch Dashboards)OkayNo, search only
Ops complexityMedium-HighMedium (lower when managed)HighVery low
CostInfra cost, cheap at large scaleInfra / AWS costInfra costUsage based, expensive at large scale
Best forProduct + log search, custom rankingAWS shops, need an open licenseLegacy/enterprise doc searchStartups, instant-search UX

20.5 When to choose which

  • Elasticsearch: a large e-commerce catalog, complex ranking and aggregations, and a team that can handle ops.
  • OpenSearch: you are on AWS and want a managed service + open-source license.
  • Solr: you are already on Solr or have an old enterprise document-search setup.
  • Algolia: you want to launch quickly, need the best search UX, and volume is moderate.
  • Small app that already has Postgres: to start with, Postgres full-text search (tsvector) may be enough.

20.6 Real-world example

Real world: Product search like Amazon/Flipkart (a hypothetical scenario)

  1. A seller adds a new product, and a row is inserted in MySQL (the source of truth).
  2. Debezium CDC sends the change to a Kafka topic, and an indexer service indexes the product into Elasticsearch.
  3. After ~1 second the product becomes searchable; 50 crore products are spread over 100+ shards.
  4. The user types 'nike runing shoes under 3000'; the analyzer tokenizes, and fuzzy matching turns 'runing' -> 'running'.
  5. The query is: text match + filter price<=3000 + brand facet aggregation; each shard searches in parallel.
  6. Results are re-ranked with BM25 score + popularity + stock + sponsored boost, and the top 20 return in ~100 ms.
  7. On sale day, read replicas are increased, and popular queries are cached.

20.7 Interview tips

  • Common mistake: making Elasticsearch the primary database - it is a search index; keep the source of truth in the DB.
  • Common mistake: creating too many small shards (oversharding) hurts cluster memory and performance. Not planning the mapping is also wrong: if text vs keyword is wrong, filters/sorting do not work.
  • Interview tip: be sure to explain the inverted index and the DB -> CDC -> search index sync flow.
  • Worth quoting: refresh interval ~1 sec (near real-time), BM25 ranking, and the full scan of LIKE '%x%' vs an index lookup.