Why search isn't a B-tree
A query arrives as a term, not a key, so the index must map each term to the documents that contain it.
A B-tree answers questions about keys it can order, such as equality and ranges, because each comparison discards a subtree. A word inside a document has no fixed position in that order, so the B-tree must read every leaf. An inverted index flips the mapping: each token points to the document IDs that contain it. A search then becomes a few list lookups and an intersection, and the write path pays the cost once so reads stay cheap.
Build the index from tokens rather than from the text. The write path lowercases each document, strips punctuation, and splits on whitespace into terms such as apple and berry. For each term, the structure stores a posting list of the sorted document IDs that contain it, plus term frequencies for ranking. A query for apple and berry becomes two list lookups and an intersection. Keep the term dictionary sorted and a prefix query becomes a range scan over tokens. Suffix queries need a second structure: reverse each term at write time, sort the reversed copies, and reverse the query. Each document belongs to one shard, which holds a complete index over its own documents.
The abstraction hides a cost that lands on writes. A document measured in megabytes produces one posting per distinct token, so each insert touches many lists. It also hides placement. A local layout keeps each document's postings on the shard that owns it, so every search fans out to all shards and merges results on a coordinator. A global layout puts every posting for a term on one partition, which concentrates the work there. The specific failure: a catalog search on a description column gets a B-tree, and the planner ignores it for a LIKE with a leading wildcard. Every row is read, so latency grows with the catalog.
Classify the predicate before you choose a structure. If the query is equality or a range on a value, build a B-tree. If the query is a term inside text, build an inverted index and accept that each write costs more than the document alone. Store document IDs in postings, not bodies, unless measured latency justifies the storage. Choose partition scope from the queries you will run: if every search stays inside one chat, partition by chat ID so one partition answers. If one search box covers the whole site, plan for scatter-gather and measure fan-out before fixing the shard count. Confirm with a two-term query that the plan touches only their postings.
- Posting entry
- one document ID
- Document body
- megabytes
- Index scope
- one shard
- Term lookup
- one per term
- Leading-wildcard scan
- every row
One entry in a term's list, naming a document that contains the term. IDs stay small while the bodies they point to can run to megabytes.
The stored text. Copying it into many postings multiplies storage cost.
Each document lives on one shard, and that shard's index covers only its own documents.
A two-term query costs two list reads and an intersection, not a single key probe.
A LIKE with a leading % cannot use key order, so the database reads the whole table.
The naive setup treats the search index like a database table: one index, one copy of the documents, queried wherever the data happens to sit. Elasticsearch's documentation describes a different unit. Each document belongs to exactly one shard, and each shard holds a complete index over the documents it owns.
The wall shows up when a query must consider every document. Because matches can live on any shard, a search goes to all of them and the results come back to be merged, so the slowest shard sets the response time.
The lesson comes from the storage layout: choose the shard count and partition key around the query scope you actually have. A per-shard index keeps each search inside the data it needs, while one global index for all terms concentrates work where the common terms live.