The inverted index
A book index maps words to pages. An inverted index maps terms to the documents containing them (a postings list), often with positions and frequencies.
A query for "butter paneer" looks up both postings lists and intersects (AND) or unions (OR) them, then scores the results.
Text analysis
Before indexing (and querying), text runs through an analyzer:
- Tokenise: split into words.
- Normalise: lowercase, strip accents.
- Remove stop words (optional): "the", "a".
- Stem / lemmatise: "running" → "run".
- Synonyms: "tv" ↔ "television"; regional terms ("curd" ↔ "dahi").
- N-grams for partial matching and typo tolerance; edge n-grams for prefix search.
Indexing and querying must use compatible analyzers, or nothing matches.
Relevance
BM25, the default in Elasticsearch/OpenSearch/Lucene, scores a document higher when:
- the term appears often in this document (term frequency, with diminishing returns);
- the term is rare across all documents (inverse document frequency);
- the document is short (a match in a title beats a match in a 5,000-word page).
Real products layer more on top: field boosts (title > description), business signals (popularity, ratings, availability, distance), personalisation, and often a two-stage ranking: a fast first stage retrieves ~1,000 candidates, then a learned ranking model re-orders the top 100.
Architecture of a search cluster
- An index is split into shards (each a self-contained Lucene index) and each shard has replicas.
- A query goes to a coordinating node, which fans out to one copy of every shard (scatter), then merges the top results (gather).
- More shards → more parallel indexing; more replicas → more query throughput and fault tolerance.
- Near-real-time: new documents become searchable after a refresh (typically ~1 s).