<aside> š
Agument Code Seriesļ¼
How to build your Agent: 11 prompting techniques for better AI agents - Augment Code
šĀ Integrate BM25:
š¤Ā Combining Embeddings with BM25 for Chunk Indexing
PostgreSQL BM25 Full-Text Search: Speed Up Performance with These Tips
šĀ ANN
š¤Ā Implementing ANN-Based Semantic Search with Go and PostgreSQL
https://github.com/pgvector/pgvector
šĀ RAG Related:
https://blog.laisky.com/p/llm-rag/
Contextual Retrieval in AI Systems \ Anthropic
</aside>
Combining Embeddings with BM25 for Chunk Indexing: Concepts, Implementations, and Hybrid Strategies
Retrieval-Augmented Generation (RAG) systems, designed to augment large language models (LLMs) by injecting relevant external knowledge, stand or fall on their information retrieval component. Recent advances have seen the rise of hybrid retrieval strategies that combine dense vector embeddings and classical lexical search, especially with BM25. Anthropicās "Contextual Retrieval" methodology highlights how mixing dense and sparse retrieval over intelligently chunked, contextualized text can dramatically improve retrieval success ratesāreducing failures by up to 67% when paired with reranking models. This report presents a thorough, research-driven exploration of BM25ās theoretical and practical foundation, its integration in LLM-oriented pipelines, hybrid strategies for marrying embeddings and keyword scoring, best practices around chunking and indexing, and actionable implementation examples in Go, including a minimal working hybrid retriever.
BM25 (Best Matching 25) is a probabilistic ranking function rooted in the probabilistic information retrieval framework developed over several decades by Robertson, SpƤrck Jones, and colleagues. Born as the default scoring function in the Okapi Information Retrieval System, BM25 improved on TF-IDF by refining the treatment of term rarity, saturation effects in term frequency, and length normalizationāin effect blending statistical rigor with practical adaptability.
BM25 has been pivotal in both academic and industrial search, powering anything from small-scale document retrieval to the backbone ranking in engines like Elasticsearch, Apache Lucene, and OpenSearch.
The BM25 score for a document D with respect to a query Q composed of terms qā, ..., qā is:
$$ Score(D,Q)=āi=1nIDF(qi)ā f(qi,D)ā (k1+1)f(qi,D)+k1ā (1āb+bā ā£Dā£avgdl)\mathrm{Score}(D, Q) = \sum_{i=1}^n IDF(q_i) \cdot \frac{f(q_i, D) \cdot (k_1 + 1)}{f(q_i, D) + k_1 \cdot (1 - b + b \cdot \frac{|D|}{\mathrm{avgdl}})} $$
$$ IDF(qi)=lnā”(Nān(qi)+0.5n(qi)+0.5+1)IDF(q_i) = \ln \left( \frac{N - n(q_i) + 0.5}{n(q_i) + 0.5} + 1 \right) $$
Where: