Sikau

Project: A search engine · Day 4 · about 50 min

Final: The search engine

Autocomplete, ranked search and "did you mean" in one engine.

Put it all together

Wire the trie, the index and the speller into one engine: suggestions while typing, ranked results for a query, and when a word isn't known, an automatic correction with a "did you mean" message — exactly how real search boxes behave.

Build once, answer many

Finishing this engine is a good moment to look back: each piece was a lesson on its own, and together they make a feature people use every day.

createSearchEngine(docs) does all the expensive preparation up front: insert every word into the trie with its frequency, and build the inverted index. The returned object keeps them in a closure, so each keystroke and each query reuses the same structures. This is the same build-once idea as the leaderboard and the index, applied to the whole feature.

  • Words: split every document once; reuse the list for both structures.
  • Trie: insert each word with its total count across documents.
  • Index: word → document id → count.
  • Vocabulary for spelling: the trie's (or index's) keys.

If documents change, rebuild — or add update methods later. Start simple.

The rest of this lesson

  • The search flowexplain
  • What you've combinedexplain
  • What happens for "spicy mommo"order
  • Feature to structurematch
  • createSearchEnginechallenge
  • Badge quizquiz
  • Project complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 4 lessons of Project: A search engine