Sub topic 8
Retrieving and ranking candidate pages
This chapter covers the step between query rewriting and passage extraction. The engine has a set of search queries. It must now turn them into a short ranked list of pages whose passages can go into the model's context. It assumes you know what a search index is, how HTTP requests work, and the retrieve-then-generate pipeline from earlier chapters.
Where retrieval sits in the pipeline
Retrieval takes the queries produced by rewriting and returns candidates. Ranking orders those candidates. Only the top few survive to passage extraction, because the model's context window is limited and every extra passage adds cost and noise.
Public documentation says little about the exact ranking systems of ChatGPT search, Perplexity, and Copilot. Treat the stages below as the common design in retrieval systems, not as a published recipe for any one engine.
Stage 1: Candidate retrieval
Each rewritten query is sent to a search backend. Which backend depends on the engine. As the earlier chapter on sources explained, some engines query their own index, and some use a third-party search service. Copilot draws on Bing's index. Others combine their own crawl with partner data.
Two retrieval methods are common, and many systems combine them:
- Lexical retrieval matches the words in the query against words in documents. BM25 is a standard scoring function of this kind. It rewards pages that contain the query terms, especially rare terms, and it adjusts for page length.
- Semantic retrieval converts the query and the documents into vectors, called embeddings, and finds documents whose vectors are close to the query vector. It can match a page that uses different words from the query but means the same thing.
Combining the two is called hybrid retrieval. Lexical matching is good at exact names, error codes, and version numbers. Semantic matching is good at paraphrase. A page can be missed by one method and found by the other.
Because the engine runs several queries per question, a page only needs to appear in the results of one of them. A page that ranks tenth for the user's literal question may rank first for a narrower rewritten query.
Stage 2: First-pass scoring
The merged candidate set may contain dozens to hundreds of URLs. A fast scorer reduces it. Signals at this stage are cheap to compute and usually come from the index itself:
- Text relevance to the query (lexical, semantic, or both).
- Page-level quality and authority signals, such as link-based measures or site reputation.
- Freshness, when the query implies recency.
- Language and region match.
- Duplicate detection, so that many copies of the same text do not crowd out other sources.
Pages must already be in the index to be scored. A page blocked by robots.txt, returning errors, or never discovered cannot reach this stage no matter how well it is written.
Stage 3: Reranking
A reranker takes the surviving candidates and scores each one more carefully against the query. A common design is a cross-encoder, a model that reads the query and a document's text together and outputs a relevance score. It is more accurate than comparing precomputed vectors, but slower, so it runs on tens of candidates, not thousands.
Some systems also use the language model itself to judge relevance or to choose which results to open. This matters because it changes what a page is judged on. A reranker reads text, so a page is judged by the passages it contains, not only by page-level signals.
Stage 4: Selection and diversity
The final list is not simply the top scores. Engines tend to want several distinct sources so the answer can be checked against more than one page. Practical effects:
- Near-duplicate pages are collapsed to one.
- A single domain may be limited to a few slots.
- Different query rewrites may each contribute their best results.
- Some engines fetch the live page for top candidates, instead of relying only on the stored snapshot. That is a user-initiated fetch, which earlier chapters separated from search crawling.
A small ranking simulation
The script below combines a lexical score and a semantic score into one ranking, then applies a per-domain cap. The numbers are invented. Its purpose is to show why a page can win under hybrid scoring even when it loses under one method.
from urllib.parse import urlparse
candidates = [
{"url": "https://docs.example.com/rotate-keys", "lexical": 0.82, "semantic": 0.55},
{"url": "https://docs.example.com/key-faq", "lexical": 0.60, "semantic": 0.58},
{"url": "https://blog.example.org/secrets-hygiene", "lexical": 0.35, "semantic": 0.88},
{"url": "https://forum.example.net/thread/991", "lexical": 0.70, "semantic": 0.40},
]
LEXICAL_WEIGHT = 0.5
SEMANTIC_WEIGHT = 0.5
MAX_PER_DOMAIN = 1
def hybrid_score(item):
return LEXICAL_WEIGHT * item["lexical"] + SEMANTIC_WEIGHT * item["semantic"]
def select(items, limit=3):
ranked = sorted(items, key=hybrid_score, reverse=True)
per_domain = {}
chosen = []
for item in ranked:
domain = urlparse(item["url"]).netloc
if per_domain.get(domain, 0) >= MAX_PER_DOMAIN:
continue
per_domain[domain] = per_domain.get(domain, 0) + 1
chosen.append(item)
if len(chosen) == limit:
break
return chosen
for item in select(candidates):
print(round(hybrid_score(item), 3), item["url"])Running it prints three pages. The blog post has the lowest lexical score (0.35) but ranks first overall at 0.615, because its semantic score is the highest. The second documentation page is dropped by the domain cap even though it outscores the forum thread on lexical score.
What this means for page authors
You cannot see an engine's scores. You can still reason about which of your pages are likely to be candidates and which are likely to survive reranking.
- Be indexable first. Allowed by robots.txt, returns a 200 status, has a canonical URL, and is in the sitemap. No later stage can rescue a page that is missing from the index.
- Cover the vocabulary and the meaning. Use the exact terms people search for, such as product names and error codes, so lexical retrieval matches. Explain the idea in plain words too, so semantic retrieval matches.
- Make passages stand alone. A reranker reads text. A section that answers one question clearly, with its subject named, scores better than one that depends on a pronoun reference to the previous section.
- Avoid duplicates. Several near-identical pages on your own site compete with each other and may be collapsed into one.
- Keep dates honest. Freshness helps on time-sensitive queries, but only when the date signals are real, such as an accurate
dateModifiedin your markup.
Common errors
- Assuming ranking in classic search equals ranking in an answer engine. Rewritten queries and reranking mean the order can differ from what you see for the user's literal question.
- Optimizing for one query. The engine issues several. Check which narrower questions your page answers.
- Treating the top rank as a guarantee of citation. A page that is retrieved can still be dropped at selection, or not used when the answer is written.
- Blocking the wrong crawler. If the search crawler for an engine is disallowed, its index may lack your page, even if you allowed a different crawler from the same company.
- Reading tool behavior as ranking evidence. A single test prompt that cites your page shows one run. Results vary between runs, as a later chapter covers.
Exercise
In the simulation, change MAXPERDOMAIN to 2 and run the script again. Which page set is returned, and what does the change show about diversity limits?
Show answer
With a cap of 2, the second docs.example.com page can be selected. The scores are blog 0.615, rotate-keys 0.685, key-faq 0.59, forum 0.55. The sorted order is rotate-keys (0.685), blog (0.615), key-faq (0.59), forum (0.55). The top three are rotate-keys, blog, and key-faq, so the forum thread is dropped. With a cap of 1, key-faq was skipped and the forum thread was included. The diversity cap decides whether a second page from the same site displaces a page from another site, even though the scores do not change.
Exercise
A page ranks well for the literal question in classic search but is never cited by an answer engine. List three retrieval-stage reasons this could happen.
Show answer
Possible reasons: the engine's search crawler is blocked or has not indexed the page; the rewritten queries target narrower phrasing that the page does not cover; the page lost to a near-duplicate or to a domain cap at the selection stage; the reranker found no passage that answered the question on its own.