Fast Hybrid Document Search with FTS5 and Vectors   [ Skip to content ](#main-content)  [ ~/ bastiaan.dev ](/) 

 [ Home ](/) [ Blog ](/blog) [ Contact ](/contact) 

 [ Home ](/) [ Blog ](/blog) [ Contact ](/contact) 

Fast Hybrid Document Search with FTS5 and Vectors
=================================================

 Bastiaan — November 21, 2026

 [   Back to blog ](https://bastiaan.dev/blog) ![Fast Hybrid Document Search with FTS5 and Vectors](https://bastiaan.dev/images/hybrid-document-search.jpg "Fast Hybrid Document Search with FTS5 and Vectors") 

 Fast Hybrid Document Search with FTS5 and Vectors 
===================================================

   Written on November 21, 2026 @ 21:00 by Bastiaan

    Last updated on November 21, 2026 @ 21:00

   [ development ](https://bastiaan.dev/blog/tag/development) [ vector search ](https://bastiaan.dev/blog/tag/vector-search) [ sqlite ](https://bastiaan.dev/blog/tag/sqlite) [ rag ](https://bastiaan.dev/blog/tag/rag) [ fts5 ](https://bastiaan.dev/blog/tag/fts5) [ bm25 ](https://bastiaan.dev/blog/tag/bm25) [ artificial-intelligence ](https://bastiaan.dev/blog/tag/artificial-intelligence)  

Introduction
------------

Earlier on this blog, I wrote about [turning plain text into vectors](https://bastiaan.dev/blog/from-text-to-vectors) and [querying them locally with SQLite](https://bastiaan.dev/blog/from-vectors-to-answers-local-rag). That works fine when your input starts as clean paragraphs. Real documents are rarely that cooperative.

If you try to search across a folder of 80-page PDF manuals and Word files, stuffing an entire document into one vector falls apart fast. One vector tries to average out dozens of unrelated topics and ends up matching none of them well. Slicing the text every 500 characters tears tables in half and loses track of where a section actually lived in the file.

You can handle both issues inside a single local SQLite file, without spinning up Elasticsearch or a dedicated vector database.

![Flowchart hybrid document search](/images/hybrid-document-search-flowchart.png)

Parsing PDFs and Word files without losing page numbers
-------------------------------------------------------

PDF files only store which characters sit at which X and Y coordinates on a page; they have no internal concept of paragraphs or tables. Simple text extractors read those coordinates top-to-bottom, which scrambles two-column layouts and scatters table cells across unrelated sentences.

Layout-aware parsers solve this by detecting visual structure on the page and converting the content into Markdown. Headings mark natural boundaries between topics, and tables stay together as single blocks so numbers keep their column context.

Word documents have a different quirk. A `.docx` file contains no fixed page numbers in its XML structure because Word calculates page breaks dynamically when you open the file. Converting `.docx` files to PDF before parsing gives every document fixed page boundaries.

Since topics often start near the bottom of one page and finish on the next, each chunk needs both a `page_start` and a `page_end` in its metadata. When a search result comes back, you can point the user straight to the right page span.

Keeping context when splitting multi-topic files
------------------------------------------------

Once a document is converted to Markdown, you can split it along its headings so each section covers one topic and links back to a shared `doc_id`.

Splitting by section still leaves a blind spot. Imagine a paragraph on page 28 of an annual report that reads: *"Operational costs rose by 18% in Q4 due to server expansion."* On its own, that paragraph never mentions which company or product team those numbers belong to. Anyone searching for "CloudSync infrastructure costs 2025" will likely miss it.

You can fix this during indexing by passing the full document to a small language model once and asking it to write a short two-sentence note for each chunk. That note names the document title and the section it belongs to, and gets prepended to the chunk text before indexing. Every isolated paragraph then carries enough context to be found on its own.

Pairing vector search with BM25 in SQLite
-----------------------------------------

Vector embeddings work well when someone searches for "how to handle server outages" and the document calls it "Incident Recovery Procedures." They struggle when someone types an exact error code or SKU, often returning vaguely related paragraphs instead of the literal match.

SQLite's built-in FTS5 extension covers that gap using the BM25 ranking algorithm. If a rare term like an invoice ID shows up in a short paragraph, BM25 gives it a high score. Repeating that word fifty times on a long page won't let it game the ranking, because BM25 caps the bonus for repetition and penalizes bloated text.

Because FTS5 sits right next to vector extensions like sqlite-vec, your metadata and both search indexes all live in one portable database file.

Merging rankings with Reciprocal Rank Fusion
--------------------------------------------

Querying both indexes at the same time gives you two separate lists of candidate chunks. You cannot add a vector distance directly to a BM25 score because they sit on completely different numerical scales.

Reciprocal Rank Fusion (RRF) skips score normalization altogether by looking only at rank order. You pull the top 30 results from vector search and the top 30 from BM25, then score each chunk based on its position in each list. First place gets the highest points, second place gets slightly less, and so on.

A chunk that ranks well in both lists jumps to the top of the final results. A chunk that only shows up in BM25 because of an exact part number still makes the cut, even if its vector match was weak. Since SQLite runs both lookups locally, computing the combined ranking takes a few milliseconds.

Stopping one file from crowding the results
-------------------------------------------

When you scale from a few test files to thousands of pages, a new quirk shows up. One 120-page manual might cover a topic across six different subsections, causing all top five search results to come from that single file while four other relevant documents get pushed off the screen.

Grouping the fused results by `doc_id` keeps one file from taking over. You can either keep only the highest-scoring chunk per document, or combine the scores of matching chunks into a single document-level result that lists all matching page ranges underneath.

 Frequently asked questions

    Why not store a whole document as one vector?   

A single vector for a long, multi-topic document averages out dozens of unrelated topics and ends up matching none of them well. Splitting the document into sections along its headings gives each chunk one clear topic.

    Why convert Word files to PDF before parsing?   

A .docx file has no fixed page numbers in its XML, because Word calculates page breaks when the file is opened. Converting to PDF first gives every document fixed page boundaries, so each chunk can store a page\_start and page\_end.

    Why combine BM25 with vector search?   

Vector search handles synonyms and paraphrases well but struggles with exact terms like error codes, SKUs or invoice IDs. SQLite's FTS5 extension ranks those literal matches with BM25, so combining both covers each other's blind spots.

    What is Reciprocal Rank Fusion?   

Reciprocal Rank Fusion merges multiple ranked result lists by looking only at each item's rank position, not its raw score. That avoids having to normalize vector distances and BM25 scores, which live on completely different scales.

Quick links

- [ Home ](/)
- [ Blog ](/blog)
- [ Contact ](/contact)
- [ Source code ](https://github.com/basst85/bastiaan-dev)

Socials

- [ GitHub ](https://github.com/basst85)
- [ LinkedIn ](https://www.linkedin.com/in/bastiaan-steinmeier-6391a328/)
- [ Discord ](https://discordapp.com/users/837649040316825622)

© 2026 — Bastiaan Steinmeier

 Built with   using Laravel and Tailwind
