Sub topic 1
The big picture: crawl, index, serve
Prerequisites
This chapter assumes you know the following:
- How an HTTP request and response work, including common status codes such as 200, 301, and 404.
- How HTML pages link to each other with
<a href>elements. - How to read short Python snippets.
Three stages, three outputs
A search engine does not look at the live web when you type a query. It works from a copy it built earlier. Building that copy takes three stages, and each stage produces something the next one needs.
- Crawl. A crawler is a program that fetches pages over HTTP and follows the links it finds. Its output is a set of fetched documents and a growing list of newly discovered URLs.
- Index. Indexing processes the fetched documents. It extracts text, removes duplicates, and splits the text into tokens. It stores the result in an inverted index, a data structure that maps each word to the documents containing it.
- Serve. Serving takes a user's query, looks the words up in the index, ranks the matching documents, and returns a results page.
The loop on the crawl stage matters. Every fetched page can contain links to URLs the crawler has not seen yet, so crawling feeds itself.
Why the stages are separate
The stages run at different times and at different speeds. Crawling is slow because it depends on remote servers, and it must be polite so that it does not overload them. Indexing is a batch or streaming computation on the engine's own machines. Serving must answer in a fraction of a second.
Separating them means a slow crawl does not slow down search, and a query never waits for a remote site. The cost is that the index can be out of date. A page you edited an hour ago may still appear in results with its old text until it is recrawled and reindexed.
Where site owners have influence
Each stage has its own controls, and they are not interchangeable.
| Stage | Question it answers | Typical control |
|---|---|---|
| Crawl | May the crawler fetch this URL? | `robots.txt` |
| Crawl | Where are the URLs? | Links and XML sitemaps |
| Index | May this page be stored and shown? | `noindex` directive |
| Serve | How is it ranked and displayed? | Content, links, and other signals |
A common mistake is to use a crawl control for an index problem. For example, a page blocked by robots.txt can still appear in results as a bare URL if other sites link to it, because the engine knows the URL exists even though it never fetched the page. Later chapters cover this in detail.
A first look at the idea in code
The last stage rests on one data structure. This short example builds an inverted index for three tiny documents and answers a query by intersecting sets of document IDs. Later chapters build each part properly.
import re
from collections import 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(set)
for doc_id, text in docs.items():
for token in tokenize(text):
index[token].add(doc_id)
def search_all(query):
sets = [index.get(t, set()) for t in tokenize(query)]
return sorted(set.intersection(*sets)) if sets else []
print(search_all("web pages"))The program prints [1, 2]. Both documents contain "web" and "pages", while document 3 contains only "pages".
What the rest of the tutorial covers
The following chapters follow one page through the pipeline. First you will see how a crawler fetches it and decides whether it is allowed to. Then you will see how URLs are found, normalized, and queued, how JavaScript and duplicates are handled, and how text becomes index entries. The final chapters cover controls, freshness, and diagnosing pages that do not show up.
Exercise
A page you published yesterday does not appear in search results. Name the three pipeline stages in order, and say which stage's output you would check first to learn whether the engine has fetched the page at all.
Show answer
The stages are crawl, index, and serve. Check the crawl stage first, because a page that was never fetched cannot reach the index. If the crawler fetched it, look at the index stage next, for example for a noindex directive or a duplicate.
Exercise
Why can a page blocked by robots.txt still show up in results as a URL with no description?
Show answer
robots.txt controls only the crawl stage. The engine can still learn the URL from links on other pages and list it, but it never fetched the page, so it has no text to show.