Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 07 · From Scratch · Database · about 20 min to read

Play it as a journeyTutor comes with a plan

A search engine of your own: index, rank, query

The Index
The Index · Open board

The index board of the small database. A search engine is the same idea with the arrow turned around: it points from a word to the pages that hold it.

A train ticket app has a help page with a search box. A user types refund cancelled ticket. The first result is a page called “Ticket help”, which says ticket seven times and nothing about refunds. The page that explains refunds is third. The user gives up and phones support.

The search found every page that holds one of the three words. It put them in the wrong order. Putting pages in the right order is called ranking, and it is the real work in search.

By the end you will be able to build a small search engine in Python: it cuts text into tokens, stores an inverted index, ranks with TF-IDF and BM25, reads queries with plus, minus and quotes, and measures whether a new ranking beat the old one.

The history (optional)

In 1972, Karen Spärck Jones published “A statistical interpretation of term specificity and its application in retrieval” in the Journal of Documentation, volume 28, issue 1, pages 11 to 21. Its idea, later called inverse document frequency, is to judge a word by how many documents contain it. On 8 April 2016, Apache Lucene 6.0.0 was released, and its default scoring in IndexSearcher changed to BM25. Solr 6.0.0 came out the same day, with BM25 as the default for fields with no setting.

Before you start. Capstone: Database, Everything ended with a database that finds one key fast, because a small index kept apart from the data says where the key lives. Database 02: The Index built that index, a map from a key to a place in the file. Database 07: The Query Layer showed a typed request being read and turned into a plan before anything is searched. Here the key is a word.

Lesson 54 of 117 in From Scratch

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • The 22 pages we will search
  • Tokens: the words the index stores
  • The inverted index: one list per word
  • TF-IDF: a rare word counts for more
  • BM25: stop a word counting forever, and stop long pages winning
  • Query parsing: plus, minus and quotes
  • Measuring: did the new ranking help?
  • The BM25 working for one word in one page
  • Why Lucene writes the idf with a 1 +
  • Edge cases and limits of this engine
  • Where this lives in real systems

Terms it explains: tokens, inverted index, TF-IDF, BM25, MRR, Recall at 3.

Review From Scratch