Highlights

Contents

Part I
01

Search basics: why LIKE fails

We begin with the document a reader wants to find. A database scan can find characters while missing the reader’s meaning. We will separate matching, ranking, freshness, and access control before building the index.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Search basics: why LIKEfails2Tokenization and analysis3The inverted index4Ranking: TF-IDF and BM255The indexing pipeline6Shards and replicas7Autocomplete and fuzzysearch8Operating a searchcluster9Case study: search end toend

#Section 1A phrase in a million documents

A Heron viewer types red bird while looking for a match report. The service stores an illustrative million reports, each occupying 500 bytes. Reading every report examines 500,000,000 bytes before it returns any result. The reader wanted a small list, yet the machine touched the whole collection.

A search engine is a system that builds a representation of documents so it can retrieve and rank likely matches without reading every document for every question. It spends work when content arrives, then reuses that work across queries. That trade is attractive when the same collection receives many questions and changes less often than it is searched.

The first distinction is between a document and a result. A document is the stored content, together with identifiers and fields. A result is a document selected for this query, usually with a score and a snippet. The query does not merely ask whether bytes occur. It can ask whether words occur near one another, whether a title deserves extra weight, and whether a document is visible to this user.

ONE QUESTION, TWO ACCESS PATHSscan: read every reporteach cell is 10,000 reports1,000,000 x 500 B500,000,000 bytes per queryindex: read only the candidatesdictionarybirdred10,000 xfewer byteseach cell is 1 report100 x 500 B50,000 bytes per query
Figure 15.1Illustrative corpus and candidate sizes, computed in search-numbers.py. A candidate count is a workload assumption, not a guaranteed selectivity.

A 100-document candidate set requires 50,000 document bytes before overhead, a factor of 10,000 less than the scan. This does not prove the index is always faster. A broad query may select most documents, and a cold index may cause many random reads. The mechanism removes unnecessary work when words narrow the collection.

#Section 2Why LIKE is a different question

Suppose a report says systems for distributed scoring. The SQL condition body LIKE '%distributed systems%' rejects it because the exact consecutive characters do not appear. Another report quoting the phrase to criticize it matches. Character containment gives no natural account of relevance, word order variants, spelling errors, or importance within a field.

A leading wildcard usually prevents an ordinary ordered text index from narrowing to a contiguous prefix range. The engine therefore needs a scan or another suitable index structure. Some databases support full-text and trigram indexes, so the honest comparison is between index capabilities, workloads, and operational needs. It is wrong to claim that SQL databases cannot search text.

A full-text query asks about analyzed terms and their relationships rather than raw character containment. The index might recognize distributed and systems independently, store their positions, and rank a title containing both above an unrelated body mention. The application still chooses the intended semantics. Exact phrase, all words, any word, and substring are different questions.

THE SAME TEXT, DIFFERENT PREDICATESexact phraseall terms, any orderdistributedsystemsdistributedsystemsreport Aintro to distributed systemsintro to distributed systemsmatchintro to distributed systemsboth termsreport B, from section 2systems for distributed scoringsystems for distributed scoringreversed and not adjacentno phrasesystems for distributed scoringboth termsLIKE '%distributed systems%' agrees with the phrase column for these two reports
Figure 15.2Illustrative matching trace. The final row changes the predicate, rather than pretending that phrase and term search are equivalent.

Before choosing Elasticsearch, write the query classes and required guarantees. A small catalogue with infrequent writes may work well with database full-text search. A large multilingual corpus with relevance tuning and independent query scaling may justify a separate engine. The cost is another derived copy that can lag, fail, and need rebuilding.

#Section 3Matching is not ranking

Our tiny corpus contains four reports. Document 1 says red bird scores, document 2 says blue bird scores, document 3 says red match live, and document 4 says bird bird match. A query for bird matches documents 1, 2, and 4. The question of which comes first remains unanswered.

Matching is the yes-or-no decision that a document satisfies the query. Ranking orders the matching documents by a chosen measure of usefulness. A filter such as visibility=public can remove a document without contributing a relevance score. A scoring clause can contribute a number while also imposing a matching condition.

The search process first determines eligible candidates, then evaluates the score for those candidates, then retains the best results. In practice, an implementation can interleave these stages and skip work using score bounds. Keep the conceptual responsibilities distinct even when the optimized execution is less linear.

ELIGIBILITY BEFORE ORDERbird postingsbird124d1d2d4visibility=publicd2 removed before scoringBM25 for birdd40.490428d10.356675only eligible documents get a scoreresults1. d42. d1a score is not permission
Figure 15.3Illustrative execution path. A filter removes ineligible candidates before the final response.

A relevance score is not a probability that a report is true. It is a comparison value under a particular query and corpus representation. Two queries may use different terms and have incomparable score scales. Even identical scoring formulas can differ across shards if their local term statistics differ.

#Section 4Freshness is part of correctness

A scorer changes a title from match scheduled to match cancelled. The database acknowledges the change, but search still returns the old title. Nothing in the relevance formula can repair that failure. Search has an independent freshness contract, the allowed delay between a source change and its searchable representation.

The sequence is source commit, change publication, index ingestion, and searchable visibility. A write acknowledgement from one stage says nothing automatically about later stages. Name the acknowledgement boundary rather than saying that a write is simply complete.

For Heron, the illustrative source publishes 20 events each second. A one-second refresh interval can collect 20 writes before a new search view opens, apart from ingestion lag. A user needing immediate confirmation should receive the committed source record or use an explicit visibility workflow. Ordinary browse searches can tolerate a bounded delay if the product states it.

A COMMIT AND A SEARCH VIEWcommit v8ingest v8query sees v7refresh opens v8sourcev7v8indexerv7v8search viewv7v8readerdurable is not yet searchablev8v8
Figure 15.4Illustrative states. Source durability, indexing progress, and search visibility are separate boundaries.