How does a search box find one word in a million pages in the blink of an eye? It certainly isn't reading them all while you wait. I've been coding for 30+ years and I'd never actually built one, so I did.
PySearch is a small full-text search engine in Python, with every idea explained in plain English. It has no dependencies and it's nowhere near Elasticsearch, but you can read the whole thing.
pip install fromscratch-pysearch
Enter fullscreen mode
Exit fullscreen mode
The librarian who read every book
The docs open with a picture I love. Imagine a librarian who has read every book in the library. Ask her "which books mention dinosaurs?" and she doesn't open a single one, because she kept notes while she was reading.
A search engine works the same way: it does the hard work before you ask. Here's a small library:
from pysearch . engine import SearchEngine
engine = SearchEngine ( ) engine . add ( "trex" , "The T. rex was a huge dinosaur with tiny arms and huge teeth" ) engine .…