Most of us have spent years asking a search engine to find something without having to think much about how it does that. We type a question, inspect the results, and change the wording if they are unhelpful. Behind that search box are decisions about how to index information, interpret the question, find candidates, and decide which deserve our attention. Google makes those decisions for its service. When we add retrieval to an application, many of those decisions become ours.

RAG brings the machinery of a search engine into the application. What feels new to many teams is the responsibility for building and operating search, a discipline they previously consumed through a search box. Enterprise document systems have been doing this work for years, and the underlying techniques are available in libraries and managed services. We can now assemble them around a collection of policies or product documentation without operating a web-scale search company. The smaller collection reduces the scale of the work, but it still contains competing matches, inconsistent vocabulary, and questions whose useful answers are spread across several places.

In Before the Model Can Answer, we prepared an invented travel policy through ETL, preserving the hotel limits together with their units and approval conditions. Now someone asks, “Do I need permission to book a London hotel above the nightly limit?” We have the material that answers that question. Following how we find it gives us a way to understand the search infrastructure we are putting inside the application, and where inference participates in it.

The travel policy is part of a larger collection that might include old policies, guidance for other countries, and instructions for claiming expenses. Several documents could contain the words in our question. Some could even give a clearer textual match than the applicable rule. Before ranking any passages, we need to establish which sources belong in this search: the policy that applies to this employee and this trip, at the relevant date.

That decision uses the metadata carried through preparation. Document identity, effective dates, and access rules restrict the collection, while the question supplies what we want to find within it. An archived policy should not win because it repeats “London hotel” more often. The actor can use inference to interpret a request for last year’s policy, but the retrieval service still applies the resulting date constraint and the caller’s permissions. Those constraints apply to every search route we provide.

Once we have that eligible collection, relevance becomes a more useful question. We are looking for passages that explain the London limit and what happens when a booking exceeds it. We can start with the words the person supplied, which gives us a straightforward first implementation and a way to see why search engines need more than a text match.

Find the words, then decide which matches matter

A search engine does not have to read every policy from beginning to end for each request. During indexing, it can record which passages contain each word. Looking up “London” and “hotel” then leads directly to lists of passages containing those terms. This inverted index is a basic structure behind full-text search, with text processing that can account for capitalization and word forms. It gets us to candidates quickly, but we still have to decide their order.

Imagine a long travel guide that mentions hotels throughout and a short policy passage that states the nightly limit once. Counting matching words rewards the longer guide for having more opportunities to mention them. Treating every word equally also gives “book” the same influence as a term that appears in very few passages and identifies our subject much more precisely. The ranking needs to account for the distribution of words across the collection and within each passage. BM25 handles those problems by combining three considerations. A term appearing in fewer passages carries more weight; repeating a term earns progressively less additional credit; and passage length adjusts for how much opportunity there was to contain it. Together, those adjustments let the short rule compete with the long travel guide. We are still doing keyword search, but with a ranking that accounts for more than raw occurrence counts.

This works especially well when the wording matters. A policy identifier, product code, or named place can be a much stronger clue than a broad resemblance in meaning. Our question also includes “permission,” though, while the policy might say “prior authorization for accommodation exceeding the allowance.” We can maintain synonyms or rewrite the query, but the number of ways people express the same need grows quickly. That is the next search problem to solve: finding the rule when the question uses different language.

Search for the meaning as well

Semantic search lets us look for related meaning when matching words is insufficient. An embedding model represents text as a vector, a set of numerical coordinates in a shared space. Picture that vector as an arrow from the origin to those coordinates. A retrieval model can be trained so that relevant questions and passages produce arrows pointing in similar directions, giving us a way to compare their meaning through that alignment. Our question about permission to exceed a hotel limit can then lead us to a rule about authorization for accommodation. The Dense Passage Retrieval paper explains how models learn relationships between questions and relevant passages.

During loading, we used the embedding model to produce and store a vector for each passage. When the question arrives, we encode it into the same space using the model’s corresponding query path. We can now compare its arrow with the stored passage arrows: closer alignment indicates a stronger semantic relationship according to the model. This gives “direction” a specific job in our search. We are using it to find passages that address the question, even when their vocabulary differs, without processing all the passage text through the model again.

One common way to measure that alignment is cosine similarity, which compares the angle between the arrows. A smaller angle means closer alignment and a higher similarity score. For this comparison, the semantic signal we want is their direction; extending an arrow makes it longer without making it point more closely toward the question. We therefore remove length from the calculation so magnitude does not give a passage extra influence over its ranking. One way to do that is to normalize both vectors to length one while preserving their directions. Their dot product, calculated by multiplying matching coordinates and adding the results, then gives the cosine similarity. The Faiss documentation explains that calculation in more detail.

With that comparison defined, we can find the passages whose vectors align most closely with the question. For a single travel policy, we can compare every eligible passage and keep the closest few. That is exact nearest-neighbor search; the usual “k” in k-nearest-neighbor simply specifies how many results to keep. As the collection and request volume grow, an exhaustive comparison can become too slow or expensive. An approximate search index organizes the vectors so the query can concentrate on likely matches without visiting every passage. HNSW is one widely used implementation, with the details of its graph structure explained in the linked paper.

Approximation introduces a tradeoff into the search we have just defined. The hotel rule might align closely with the question yet be missed because the search did not explore that part of the index. Spending more time exploring can recover useful candidates, at the cost of a slower response. Start with the collection each request actually searches, after applying its source restrictions: a bounded policy lookup may support exact comparison, while a large document estate may need approximation to meet response-time targets. The embedding model determines which passages are close in meaning; the index determines how thoroughly and efficiently we find them.

Keeping those responsibilities distinct also tells us what a change involves. Changing the search index or its exploration settings changes how we navigate the existing vectors. Changing to an embedding model with a different space requires regenerating the passage vectors and moving the query path with them. Encoding only the question with an unrelated model would leave it in a different coordinate system from the stored passages, even if both produced the same number of coordinates. The model, normalization, and comparison settings therefore belong together in the index configuration. Once they agree, semantic search can return its ranked candidates alongside those found through the words themselves.

Decide which matches to return

For our hotel question, the keyword results may favor a travel guide that repeatedly names London hotels, while semantic search may favor passages about authorization and spending allowances. Each route has found something useful, and each can miss something the other captures. Hybrid retrieval runs both so we can retain candidates from either route. The immediate task is to turn those results into one shortlist without requiring a passage to perform equally well in both searches.

Their scores measure different things, so a keyword score and a semantic score cannot simply be treated as interchangeable measures of relevance. Reciprocal rank fusion offers a common way to reconcile them using where each passage ranks in the lists. A hotel rule near the top of both gains support from that agreement, while a useful passage found by only one route can still participate. We have used the searches’ combined judgment to order the candidates, without another model reading their text. That gives us a shortlist containing the hotel approval rule, perhaps alongside expense-claim instructions and the general travel guide.

A question about permission to exceed the London hotel limit searches applicable policy passages by matching words and by semantic similarity. Each route returns a ranked list. Rank fusion combines them, an optional reranker reads shortlisted passages against the question, and context assembly supplies the selected policy material to the actor.

Those candidates expose the remaining distinction. Instructions for claiming hotel expenses are related to the request, but “permission to book” asks about a decision before spending the money. If the initial ranking does not separate those uses well enough, we can spend a closer reading on the shortlist. A cross-encoder reranker processes the question and each candidate passage together, producing a relevance score for that pair. It can compare “permission to book” directly with “authorization before booking,” considering the wording of both texts together. We then reorder the candidates using those scores.

That paired reading is the reason for the additional model work. The embedding search compared representations prepared independently of one another; the reranker examines this particular question against this particular passage. Doing that for the whole collection would be expensive, so the earlier searches give it a manageable set to consider. The size of that set determines both its cost and its opportunity to improve the result. A hotel rule left out of the shortlist cannot be promoted, which gives us a reason to retrieve more candidates than we intend to return.

Embedding retrieval prepares passage vectors once and compares them with a query vector. A cross-encoder reranker takes the original question and each shortlisted passage together, producing a relevance score for each pair. Only shortlisted passages receive that additional model processing.

This lets us spend work where the selection needs it. If the first ranking reliably finds the required rule, we can select from it directly; if closely related passages compete, reranking may justify its additional latency and capacity. Either route ends with a choice of passages to use for the answer. For our hotel question, that means selecting the approval rule and any other material needed to establish the applicable limit, then carrying their meaning into the context the actor receives.

Return enough material to answer the question

The selected hotel rule needs to arrive with the conditions that make it useful. A matching row might establish the London limit while a linked note says that approval must come from a manager before booking. If preparation carried them together, selecting the passage supplies both. If we indexed smaller units, we can expand a hit to its containing section before returning it. Several selected hits may lead to that same section, which only needs to appear once. This completes the handoff from finding relevant material to assembling something the recipient can use.

Search systems make these choices when they show surrounding text, collapse duplicate results, or provide access to a complete document. In our application, context assembly makes those choices for the material supplied to inference. The actor needs the policy text, its source, and the conditions required to answer, alongside its instructions and conversation. That means the number of results alone is an incomplete budget: five short passages and five expanded policy sections consume different amounts of context, and five near-duplicates may cover less of the question than two complementary passages.

I would therefore set the candidate and context budgets separately. The first gives selection enough material to work with; the second accounts for what the selected passages become after expansion and deduplication. Preserve which passages search found and which the actor actually supplied for inference, so a missing approval condition can be traced to the stage that lost it. This also keeps an increase in search depth from silently becoming an increase in everything sent to the answer model. We can examine more possibilities while still returning a focused result.

Assembling that result may expose a specific gap. If the actor has the London limit but no approval condition, inference can formulate another search for the exception procedure, much as a person would refine a query. The original question and source constraints remain in force, and the actor limits how many searches and model calls the investigation can consume. If the permitted sources do not establish an answer, the application needs to return that limitation or route the question to someone who can resolve it. The retrieval process now gives us a controlled way to find, select, and return information, including a way to recognize when another search is useful and when it is time to stop.

Choose how much of the search engine to operate

Understanding the machinery does not oblige the team to implement each part. It lets us choose a service based on the work we need it to perform and recognize which decisions remain ours. If the business already operates useful enterprise search, I would begin by exposing that capability to the actor. Check whether it can return passages with their source context and enforce the required filters. An existing investment in indexing, relevance tuning, and source integration can be much more valuable than a new vector store assembled for the AI project.

For a team without that infrastructure, start with the managed search path in the platform it already operates. On AWS, Bedrock Managed Knowledge Bases takes on ingestion, storage, indexing, and retrieval, including managed embedding and reranking. Azure AI Search exposes full-text and vector search over an index, combining them through rank fusion. Google’s Agent Search provides managed search over enterprise content, including semantic search. These give us different degrees of control, so the choice should follow whether we need a managed retrieval path or direct control over indexing and ranking. The stages we have just followed give us concrete things to inspect in either case.

Custom preparation does not automatically require custom search. We may need our own transformation to preserve unusual policy structures while still handing ordinary passages and metadata to a managed service. Conversely, a mature search team may need controls over filters, candidate generation, or ranking that a particular managed path does not expose. Own that additional machinery when a requirement justifies it, and retain the parts that already work. The application can depend on a retrieval interface without binding its business actor to the service’s internal search pipeline.

Fund the search capability the application depends on

The business is adopting a search capability along with the agent, even when a managed service supplies most of its implementation. Someone has to decide which questions it should answer, recognize when the applicable rule is missing from the results, and own changes to the configuration that selects them. That work belongs with people who understand the information domain and engineers who can trace a result through the search stages. Treating it as a one-time connection between a model and a document folder leaves that responsibility unassigned.

I would begin with a bounded collection and the questions people actually need answered, including exact references, alternative wording, and requests that need more than one passage. Establish the acceptable response time and cost, then choose the simplest retrieval path that meets those needs. An interactive policy assistant has a different operating budget from an overnight research process. Hybrid search, reranking, and repeated searches each need to earn their place against that purpose. We will examine how to evaluate those decisions in a later essay; the architectural requirement now is to make them separately visible and changeable.

Our hotel question took us through indexing, query representation, candidate selection, ranking, and the assembly of useful results. Those responsibilities have long existed behind the search boxes we use. RAG makes them part of the application we are responsible for delivering, with inference able to formulate questions and use what comes back. Once we recognize that, we can draw on the people, techniques, and infrastructure already built for search, and spend our effort making them work for the business’s collection and questions.