Indexing in distributed search
medium 28 minHow documents become a searchable index spread over many machines: the analysis chain, posting lists and their compression, segments and merges, and the choice between splitting by document or by term.
See it run
Text becomes postings, postings become segments, segments live on shards.
Operations
From text to postings
Eight steps for one sentence.
Step 1 of 8
A document arrives
| Step | Terms |
|---|---|
| input | The Quick Brown Fox jumps |
| over 2 lazy Dogs! |
Document D1
It has a title, text and metadata.
Step 1: A document arrives
def index(doc): text = doc.text.lower() words = tokenize(text) kept = [(w, i) for i, w in enumerate(words) if w not in STOP] terms = [(stem(w), i) for w, i in kept] postings = group_by_term(terms) shard = crc32(doc.id) % SHARDS shards[shard].add(doc.id, postings)- Write
- Work for each word
- Search
- Nothing yet
Say that the query goes through the same chain.
In the real world
The same indexing in distributed search doing real jobs. Pick one and step through it.
A seller saves a product and searches at once.
Step 1 of 4
Saved
| Time | Log | Segment |
|---|---|---|
| 0.0 s | product |
Safe
The primary writes the log and the buffer, and says ok.
Step 1: Saved
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Different chains for query and document
criticalThe document has jump, the query asks for jumps, and they never meet.
Wrongindex[stem(word)]lookup(query_word)Rightindex[stem(word)]lookup(stem(query_word))Counting an update as a new document
majorThe old postings stay, and df and tf are wrong.
Wrongindex.add(doc)Rightindex.remove(doc.id)index.add(doc)Refreshing after every write
majorThousands of tiny segments, endless merges and cold caches.
Wrongwrite(doc); refresh()Rightwrite(doc)# refresh runs every secondA home-made stemmer
minorCutting endings blindly turns red into r and shoes into sho.
Wrongw[:-2] if w.endswith("ed") else wRightporter.stem(w)
Interview tips
How to explain it
- Term to documents.
- Segments never change.
- Split by document.
Memory trick: INDEX
- Inverted, Normalise.
- Distribute, Extract.
- eXpress lookups.
Numbers to say
- Refresh about 1 s.
- Variable bytes at best 4x.
- One list of 10 million: 15 MB.
Watch out
- Safe is not findable.
- Shard count is in the address.
- Analyzer change means rebuild.