Use case
Use Google search in a RAG pipeline
Search can be the discovery step in a retrieval-augmented generation pipeline. The API returns ranked URLs and snippets, while your application controls fetching, extraction, storage, and citation.
Use search for discovery, not as the full document
Start with a focused query, then select a small set of organic result URLs. Fetch the pages you are allowed to access with a separate content pipeline and apply your own size, type, and safety limits.
The search snippet helps with selection, but it is not the complete source. Do not create a detailed answer from snippets alone when the source page can be checked.
- Search for candidate source pages.
- Filter and deduplicate the returned URLs.
- Fetch allowed pages with strict time and size limits.
- Extract and split useful text.
- Store the source URL with each chunk.
- Generate an answer with visible citations.
Keep source selection explainable
Preserve result position and URL before applying product-specific filters. Remove duplicate canonical URLs and skip file types or domains that your application cannot process safely.
For high-stakes questions, require more than one suitable source and make the user open the original pages before acting on the answer.
Create a source queue ยท javascript
const sourceQueue = searchResponse.organic
.slice(0, 5)
.map(({ title, link, snippet, position }) => ({
title,
url: link,
snippet,
searchPosition: position
}));
Record when and how each source was found
Store the query, country, language, page, request time, and result URL beside the fetched content. This makes later refreshes and audits easier.
Search results and source pages change. Set a refresh rule based on your use case instead of treating one search as permanent evidence. Responses may also be served from cache.
Let partial retrieval fail safely
A search can succeed even when one linked page cannot be fetched. Keep those stages separate so one blocked or slow page does not discard every other source.
If no suitable full source can be read, return a limited answer or say that the available evidence is not enough.