The retrieval half of RAG: classical ranking models, indexing strategies for unstructured data, and the readability measures used to judge what comes back.
Retrieval-augmented generation is only as good as its retrieval step. Before a language model can answer from private or current data, the system must find the few passages that actually contain the answer among thousands or millions of candidates. The pages in this section cover that half of the problem from first principles: how unstructured content is prepared and indexed, how classical ranking functions score documents against a query, how a small Python search library implements them, and how the RAG workflow combines retrieval with generation.
TF-IDF is the foundation: a term matters to a document if it is frequent there and rare elsewhere. BM25 refines it with term-frequency saturation and document-length normalisation and remains the default lexical ranking function in Lucene-based engines such as Elasticsearch and OpenSearch. Python Whoosh shows these ideas in a pure-Python full-text index. Dense vector retrieval with embeddings captures meaning rather than exact words, and most production RAG systems combine both in hybrid search. The Gunning Fog Index sits at the end of the pipeline as a simple readability measure for the text that is retrieved or generated.
| Method | Strength | Weakness | Read |
|---|---|---|---|
| TF-IDF | simple, transparent, fast | no length normalisation or saturation | TF-IDF |
| BM25 | strong lexical baseline, exact terms and codes | misses synonyms and paraphrase | BM25 |
| Full-text library | index and query in Python | single-process scale | Python Whoosh |
| Embeddings + vectors | semantic matches | weaker on rare exact terms, needs a model | RAG with pgvector |
| Indexing strategy | summaries, metadata, chunking | extra preprocessing cost | Unstructured Data Indexing |
Start with BM25 as a baseline, add embeddings when users phrase questions differently from the documents, and evaluate retrieval on real questions before tuning prompts. The end-to-end flow is described in the RAG workflow page, and readability scoring in the Gunning Fog Index page.