Build a hybrid-search retrieval-augmented generation (RAG) agent in 30 minutes by indexing a small set of documents, retrieving with both keyword and vector search, combining the results, and passing the best evidence to a language model. The starter gets a working question-answering loop; production requires additional checks for relevance, permissions, reliability, cost, and safe handling of source content.
What hybrid search adds to RAG
RAG retrieves relevant material at question time and supplies it to a generative model as context. It can ground answers in a changing or private document collection, but it does not guarantee correctness: retrieval may miss the needed passage, and the model may misread or overstate what it finds.
Keyword search is useful when a query contains exact terms such as an error code, product name, or policy phrase. Vector search represents text as numerical embeddings and can find passages with related meaning even when wording differs. Hybrid search uses both signals. A common approach is to retrieve candidates separately and combine their rankings, rather than directly adding raw scores that may have different scales.
A 30-minute starter build
Keep the first build deliberately small: a handful of representative files, one collection, one retrieval pipeline, and a simple command-line or web interface. Use a framework or search service you already know; APIs and configuration differ, so treat the steps below as an architecture rather than copy-paste instructions.
- Minutes 0–5: choose a test corpus. Select a few documents you are allowed to use. Include realistic examples and at least one document with exact terms, dates, or identifiers. Record five questions and the passages that should answer them.
- Minutes 5–12: extract and chunk. Convert files to text while preserving useful headings and source metadata. Split long text into overlapping chunks so a relevant passage is not cut off at a boundary. Start with chunks of a few hundred tokens and modest overlap, then adjust after inspecting results; there is no universally best size.
- Minutes 12–18: index both representations. Store chunk text, document identifiers, and metadata. Create an embedding for each chunk and make the same text searchable with a lexical index. Confirm that indexing failures and empty documents are visible rather than silently ignored.
- Minutes 18–23: implement retrieval and fusion. For each question, retrieve a small candidate set from both indexes. Combine rankings with a method such as reciprocal rank fusion (RRF), which rewards documents ranked highly in either list without assuming their scores are comparable. Remove duplicate chunks and retain source identifiers.
- Minutes 23–27: generate a grounded answer. Send the question and selected passages to the model. Ask it to answer from those passages, say when evidence is insufficient, and cite the source identifiers. The application should show those sources and avoid presenting unsupported claims as sourced facts.
- Minutes 27–30: test the loop. Run the five recorded questions, inspect retrieved passages as well as final answers, and note failures. Add a question with no answer in the corpus; the system should express uncertainty rather than invent a citation.
How retrieval and ranking fit together
A basic request passes through query handling, retrieval, ranking, context assembly, generation, and response checks. For example, “What does ERR-184 mean?” may be best served by lexical matching, while “How do I restore access after losing a device?” may match semantically phrased recovery guidance. The two searches can surface different but complementary evidence.
RRF is one practical fusion method. For each result, it adds a contribution based on its rank in each search list, commonly written as 1/(k + rank), where k is a smoothing constant. The exact constant and candidate counts are tuning choices, not universal standards. Keep the original retrieval scores and ranks in logs so you can see why a passage was selected.
Generation should receive only the most useful context that fits the model’s limit. More retrieved text is not automatically better: irrelevant passages consume capacity and can distract the model. Preserve document titles, section names, and stable citations so users can verify the answer.
Production checklist
A demo shows that components connect; production readiness means the system behaves predictably as data, users, and traffic change. Before launch, check the following:
- Corpus and updates: define supported file types, extraction rules, duplicate handling, deletion behavior, and an indexing schedule. Track when each source was indexed and detect stale or failed jobs.
- Evaluation: maintain a representative, versioned question set. Measure retrieval quality separately from answer quality: check whether relevant evidence appears in the candidate set, then whether the answer is supported and complete. Include unanswerable questions and difficult exact-match queries.
- Access control: enforce document permissions during retrieval, not merely in the interface after generation. Test with users who have different access rights, and ensure cached results cannot cross those boundaries.
- Observability: record latency, errors, retrieval ranks, selected source IDs, model usage, and feedback with appropriate data minimization. Avoid logging sensitive prompt or document text unless there is a justified, protected need.
- Failure handling: set timeouts and sensible limits for retrieval and generation. Provide a useful response when an index or model is unavailable; do not silently fall back to an ungrounded answer.
- Operations: version prompts, embedding models, chunking settings, and index configurations. Plan migrations and re-indexing when these change, and compare evaluation results before releasing changes.
Common problems and trade-offs
Good answers with bad evidence: a fluent response can hide retrieval failure. Inspect sources and require citations to map to actual retrieved chunks. Too many irrelevant results: reduce candidate volume, improve metadata filters, or tune chunking and fusion against a test set. Exact terms still go missing: check tokenization, punctuation, identifiers, and whether the lexical index includes the relevant fields.
Hybrid search adds indexing and tuning work compared with a single retriever, but can improve coverage across different query styles. It is not automatically superior: a clean, narrow corpus or a workload dominated by one query type may perform well with simpler search. Compare approaches using the same evaluation questions and operational constraints.
RAG is also not model fine-tuning. Retrieval supplies current source text at inference time; fine-tuning changes model behavior through training. A RAG system can make sources inspectable and easier to update, but it does not eliminate hallucinations, prompt injection, or data-access risks. Treat retrieved text as untrusted input: documents may contain instructions that should not override application rules.
Frequently Asked Questions
Can I build a hybrid RAG agent in 30 minutes?
Yes, a small proof of concept is realistic if the tools and sample documents are ready. Production evaluation, permissions, monitoring, and robust ingestion take longer.
How should I combine keyword and vector results?
Rank fusion such as RRF is a useful starting point because it combines positions rather than incomparable raw scores. Validate it against your own questions and corpus.
What chunk size should I use?
There is no universal setting. Begin with a few hundred tokens and modest overlap, then inspect whether chunks preserve complete answers and whether retrieval returns focused passages.
Does hybrid search prevent hallucinations?
No. It can improve the evidence available to the model, but the model may still misinterpret it or make unsupported claims. Use evaluation, citations, and an explicit insufficient-evidence behavior.
What should I measure before launch?
Measure retrieval relevance and answer support separately, along with latency, failures, and cost. Include questions that should not be answerable from the corpus.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Philips DST7060/20 7000 Series Iron: Specs & Fit
- Coal Fueled Jet Aircraft: Could It Work?675 L
- Remote Desktop on Raspberry Pi: How to Enable It
- Word Select Last Line in Paragraph?
- Google Assistant IFTTT Recipes: 7 Useful Automations
- OLED TVs in Dark Rooms: Why They Look Better and When They Don’t