AlgoViz

Indexing in distributed search

medium 28 min

How 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

StepTerms
inputThe 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

TimeLogSegment
0.0 sproduct

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

    critical

    The document has jump, the query asks for jumps, and they never meet.

    Wrong
    index[stem(word)]lookup(query_word)
    Right
    index[stem(word)]lookup(stem(query_word))
  • Counting an update as a new document

    major

    The old postings stay, and df and tf are wrong.

    Wrong
    index.add(doc)
    Right
    index.remove(doc.id)index.add(doc)
  • Refreshing after every write

    major

    Thousands of tiny segments, endless merges and cold caches.

    Wrong
    write(doc); refresh()
    Right
    write(doc)# refresh runs every second
  • A home-made stemmer

    minor

    Cutting endings blindly turns red into r and shoes into sho.

    Wrong
    w[:-2] if w.endswith("ed") else w
    Right
    porter.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.