Sub topic 15
How the index feeds search results
This chapter assumes you have read the chapters on tokenization and the inverted index, and that you can read short Python snippets. It covers the last step of the pipeline: what happens between a user typing a query and a list of results appearing.
Crawling and serving are separate jobs
Crawlers and indexers work ahead of time. The serving system works when a query arrives. It never fetches web pages. It reads only from the index that earlier stages built.
This separation explains two facts:
- Results are fast, because the work of fetching, parsing, and tokenizing already happened.
- Results can be stale, because the index is a snapshot of the web as of each page's last crawl.
The steps of answering a query
- Tokenize the query with the same rules used at indexing time. If the index lowercases text, the query must be lowercased too. Otherwise terms will not match.
- Look up postings. Each query term maps to a postings list, which is the set of documents containing it.
- Combine the lists. For an all-words query, intersect the lists. For an any-word query, take their union.
- Rank the candidates. Real engines combine many signals. A simple starting signal is how often the query terms appear in each document.
- Build the results page. The engine reads titles, URLs, and snippet text from a document store keyed by document ID.
A small working example
This extends the inverted index from the previous chapter. Each term now maps to document IDs and counts, so the code can rank matches.
import re
from collections import Counter, defaultdict
docs = {
1: "Search engines crawl web pages",
2: "Crawlers follow links to find web pages",
3: "An index maps words to pages",
}
def tokenize(text):
return re.findall(r"[a-z0-9]+", text.lower())
index = defaultdict(dict) # term -> {doc_id: count}
for doc_id, text in docs.items():
for term, count in Counter(tokenize(text)).items():
index[term][doc_id] = count
def search(query):
terms = tokenize(query)
if not terms:
return []
postings = [index.get(t, {}) for t in terms]
matches = set(postings[0]).intersection(*postings[1:])
scored = [(sum(p[d] for p in postings), d) for d in matches]
scored.sort(key=lambda pair: (-pair[0], pair[1]))
return [doc_id for _, doc_id in scored]
print(search("web pages"))
print(search("pages"))The first call prints [1, 2], because only documents 1 and 2 contain both words. The second prints [1, 2, 3], because every document contains "pages" once, so ties are broken by document ID.
Common errors
- Mismatched tokenization. A query processed differently from the documents returns nothing for words that are plainly on the page.
- Expecting live content. A page you just edited will not change in results until it is recrawled and reindexed.
- Blaming ranking for a missing page. If a page is not in the index, no ranking change will make it appear. Check crawling and indexing first, as covered in the diagnosis chapter.
Exercise
A site owner rewrites a page title and searches for the new title text the next day. The page still does not match. Name two reasons that fit what you learned in this chapter.
Show answer
The page has not been recrawled yet, so the index still holds the old text. Or the page was recrawled but has not been reindexed yet. A third possibility is that the query was tokenized differently from the indexed text.