# Test RAG Retrieval Before Generation with SQLite FTS5

> Run a small labeled search baseline, distinguish missing matches from wrong answers, and identify where synonyms or embeddings need testing.

Canonical article: https://modelrovia.com/guides/sqlite-fts5-retrieval-check/
Author: Modelrovia Editorial
Published: 2026-11-09
Updated: 2026-11-09

## Key takeaway

Check whether the correct source is retrieved before debugging the answer prompt or adding a vector database.

When a RAG answer is wrong, inspect the retrieved passages before changing the prompt. This small SQLite exercise gives you a reproducible retrieval baseline. On three labeled questions, our direct keyword queries found one expected source. Two deliberate vocabulary mappings brought the same fixture to three. The result explains the misses in this fixture and serves as a starting point for testing your own data.

## Run the labeled cases

Download the [retrieval check script](/examples/sqlite-retrieval-check.py) and run it with Python 3 and an SQLite build that includes FTS5. The script creates an in-memory database of four fictional support snippets and prints the first source ID for each question.

~~~sh
python sqlite-retrieval-check.py
~~~

On November 9, 2026, the run produced this result. None means the query matched no row.

| Reader question | Expected source | Direct | Mapped |
| --- | --- | --- | --- |
| ERR-812 | export | export | export |
| Forgot my login secret | reset | None | reset |
| Get a receipt | invoice | None | invoice |

The error code is deliberately quoted in the FTS expression, so its punctuation is parsed as part of a phrase rather than query syntax. In the other cases, the source uses “password” and “invoice,” while the direct queries use different vocabulary. The mapped queries add the words actually present in the documents. This implementation follows SQLite's documented MATCH syntax and ranking.

## Use the result to locate the failing layer

For a real investigation, save the original question, expected document ID, top retrieved IDs and extracted passage text. If the correct document is absent, check ingestion, query formulation and ranking. If it is present but its passage is incomplete, look at chunk boundaries. If the right evidence is available and the answer still misstates it, focus on generation or evidence selection.

The [RAGFlow parsing guide](/guides/ragflow-document-parsing/) covers an earlier failure mode where tables and layout do not survive ingestion. This example begins after text snippets already exist.

## Build a fair follow-up

Use product codes, paraphrases and ambiguous questions from your workflow. Assign expected document IDs before tuning. Split questions into a set for developing synonym rules and a separate set for checking them. The three mappings here were written for the fixture, so the improvement cannot estimate retrieval quality on unseen questions.

For a larger test, record top-1 and top-3 separately and retain misses. Consider evaluating embeddings on the same labeled set if vocabulary mismatches persist. Qdrant's linked FastEmbed tutorial describes one CPU embedding route. We exercised the SQLite baseline here. The embedding checkpoint download failed during this session, so there is no dense-search result in the comparison.

## Keep query handling explicit

The sample sends authored FTS expressions as bound SQL values. Bound parameters protect the SQL statement, while FTS still interprets the expression's own syntax. If your application accepts arbitrary user text, define quoting or tokenization deliberately and handle invalid expressions before comparing retrieval performance.

## Sources & references

- [SQLite FTS5 query syntax and ranking](https://www.sqlite.org/fts5.html)
- [Qdrant FastEmbed tutorial for a later dense baseline](https://qdrant.tech/documentation/tutorials-basics/generate-embeddings-fastembed/)

[Editorial standards](https://modelrovia.com/methodology/)
