Indexing in a Distributed Search 00:00:01 A word points back to its documents A merchant adds a product description. Searching every description for each shopper would repeat the same work. An inverted index spends work on the write so a later search can start with the word instead of the entire catalog. It has a title, text and metadata. Lowercase the text, split it into tokens and preserve each token’s position in the document. This analyzer omits the common word the while preserving position gaps, so later phrase matching can still use word distance. This example uses Porter stemming. Jumps and dogs map to jump and dog; the indexed stem is a normalized lookup key, not always a dictionary word. Choose the document shard with the hash of the document identifier, modulo three. The shard receives the terms and the positions at which they occurred. Shard 1 adds the postings to its own lists. A posting names a document containing the term. As soon as the analysis rules change, the meaning of those postings becomes part of the index's stored contract. 00:01:07 Searchable and durable are different moments New documents first reach a log and a memory buffer in this example. A later refresh can expose a searchable segment. Acknowledging a write and finding it through search can therefore happen at different times. D1 and D2 go to the log and the buffer. The buffer becomes segment 1. Each refresh adds a segment. D1 is marked in segment 1. The new D1 is in segment 4. Four segments become one. The gone document is left out. Background merges combine segments and remove obsolete entries when safe. They reclaim space and simplify later reads, but they also compete for machine resources while they run. 00:01:47 A posting list can shrink Consider a common term with many matching document identifiers. Storing every identifier at fixed width spends the same space on a tiny gap as on a large number. Sorted identifiers give the encoder a different option. With fixed-width storage, each document identifier in this example occupies four bytes. Each id minus the one before. All are below 128, so one byte each. Large gaps need more bytes. Rare words have short lists, so it does not matter. Compression trades decoding work for fewer bytes to store and transfer. Its payoff depends on the gaps in this particular posting list and on how the query engine skips through it. 00:02:28 Put each document on one shard One index no longer fits its owner. A document-based split assigns each document to a shard and places that document's terms there. Think through the consequence for a query that does not specify a shard. Its hash says shard 0. They go to shards 1, 0 and 2. Any shard may have a match, so all are asked. Short answers come back and are merged. If every query names a tenant, use it as the key. Then one shard answers. Searching every shard can be appropriate. A natural routing key such as a tenant may reduce fan-out when every relevant query genuinely carries that key. 00:03:07 The merchant cannot find a new product The merchant sees a successful write but the product is absent from the first search. Before blaming ranking, follow its durable write and the separate event that makes a refreshed searcher see the segment. The primary writes the log and the buffer, and says ok. The write is in the log, but the current searcher does not yet see its posting. Searching for the product returns no match. After the next refresh in this example the product is in a searchable segment. Its ID lookup and its search result now agree. Show the seller their own product from the database by id, and let search be a second behind. A direct read by identifier can confirm the saved product while the search index catches up. Tell the merchant what has been accepted and what has become searchable as two distinct promises.