Tuesday, June 23, 2026
banner
Top Selling Multipurpose WP Theme

On this article, you’ll learn to implement a hybrid search technique for RAG methods by combining BM25 lexical search with semantic search, fused collectively utilizing Reciprocal Rank Fusion.

Subjects we are going to cowl embody:

  • Why hybrid search outperforms both lexical or semantic search alone in retrieval-augmented era methods.
  • Learn how to implement BM25 lexical search and dense vector semantic search as unbiased retrieval engines in Python.
  • Learn how to merge each rankings utilizing Reciprocal Rank Fusion (RRF) to supply a remaining, balanced retrieval consequence.

Let’s get straight to it.

Implementing Hybrid Semantic-Lexical Search in RAG

Introduction

Implementing hybrid search methods is a important step in constructing fashionable RAG (Retrieval-Augmented Era) methods, particularly when shifting from prototype to production-ready options.

There may be little argument towards semantic search — fueled by dense vectors or embeddings, that are numerical representations of textual content — being extremely helpful at understanding semantics, synonyms, and context. Nevertheless, lexical, keyword-based search with approaches like BM25 covers a small blind spot uncared for by semantic search. Combining the very best of each worlds is due to this fact the right recipe to take your RAG system’s retrieval mechanism the additional mile.

Let’s discover tips on how to implement such a hybrid search technique by way of a delicate coding instance, guiding you thru each step of the method!

Be aware: In case you are unfamiliar with RAG methods, chances are you’ll discover the “Understanding RAG” article sequence remarkably insightful for getting essentially the most out of this learn. Specifically, I like to recommend buying an understanding of vector databases first by way of this text.

Step-by-Step Implementation

Step one is to make sure all the mandatory exterior Python libraries are put in, specifically these three:

  • rank_bm25: an implementation of the BM25 lexical search algorithm for info retrieval (BM stands for “Greatest Matching”).
  • sentence-transformers: gives pre-trained language fashions for producing textual content embeddings. In an actual setting, chances are you’ll have already got your individual vector database containing many doc embeddings and never want this, however we are going to use it right here to simulate the development of a toy vector database and illustrate hybrid search on it.
  • requests: used to fetch the uncooked dataset bundle from a public GitHub datasets repository ready for this instance.

With these substances at hand, we begin by loading the dataset and storing the uncooked texts in an inventory (we accomplish that as a result of it’s a small dataset).

The hybrid search course of is split into three phases: two of them happen in parallel, or independently from one another. The third is the place the fusion of each approaches occurs, utilizing a merging methodology referred to as Reciprocal Rank Fusion (RRF).

Let’s cowl lexical search with BM25 first:

The lexical search course of has been encapsulated in a operate referred to as search_bm25(). This operate takes two enter arguments: a string containing the person’s question to the RAG system, and the variety of prime outcomes to retrieve. The rank_bm25 library gives a get_scores() methodology that computes, for every doc — handled as a set of tokens — a lexical relevance rating. We then rank paperwork by lowering rating, choose the top-ok, and return them.

In the meantime, the semantic search engine first makes use of a sentence transformer mannequin to acquire embedding vectors for the texts and the person question, then applies a vector similarity metric like cosine similarity to rank texts by semantic relevance and retrieve essentially the most related ok:

Time to place all of it collectively. The 2 scores calculated for every doc can not merely be added, as a result of they function on very totally different numeric scales. As an alternative, we carry out the fusion based mostly on ranks reasonably than uncooked similarity or relevance scores. For this, RRF is the gold business customary for fusing rating info: it calculates an total rating for every doc by rewarding people who seem in excessive positions throughout each lists. The underlying logic is considerably just like that of the harmonic imply operator in statistics.

The overarching hybrid search course of is carried out as follows:

Now it’s time to strive all of it out. Let’s formulate a person question and see what outcomes we get.

The outcomes should not glorious in comparison with a manufacturing RAG system, however keep in mind we examined this on a tiny, nine-document dataset. With that context, the end result is sort of cheap.

Strive modifying the question and changing it with others associated to temples, seashores, mountains, or anything that involves thoughts when occupied with jap locations. Are you able to discover a state of affairs by which each the semantic outcomes and the BM25 outcomes are extremely in line with one another?

Wrapping Up

This text guided you thru implementing a hybrid search mechanism for the retrieval stage of RAG methods. Selecting to not rely solely on semantic search is a crucial consideration when scaling RAG options to manufacturing environments.

banner
Top Selling Multipurpose WP Theme

Converter

Top Selling Multipurpose WP Theme

Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

banner
Top Selling Multipurpose WP Theme

Leave a Comment

banner
Top Selling Multipurpose WP Theme

Latest

Best selling

22000,00 $
16000,00 $
6500,00 $
900000,00 $

Top rated

6500,00 $
22000,00 $
900000,00 $

Products

Knowledge Unleashed
Knowledge Unleashed

Welcome to Ivugangingo!

At Ivugangingo, we're passionate about delivering insightful content that empowers and informs our readers across a spectrum of crucial topics. Whether you're delving into the world of insurance, navigating the complexities of cryptocurrency, or seeking wellness tips in health and fitness, we've got you covered.