• About Us
  • Disclaimer
  • Contact Us
  • Privacy Policy
Wednesday, June 17, 2026
mGrowTech
No Result
View All Result
  • Technology And Software
    • Account Based Marketing
    • Channel Marketing
    • Marketing Automation
      • Al, Analytics and Automation
      • Ad Management
  • Digital Marketing
    • Social Media Management
    • Google Marketing
  • Direct Marketing
    • Brand Management
    • Marketing Attribution and Consulting
  • Mobile Marketing
  • Event Management
  • PR Solutions
  • Technology And Software
    • Account Based Marketing
    • Channel Marketing
    • Marketing Automation
      • Al, Analytics and Automation
      • Ad Management
  • Digital Marketing
    • Social Media Management
    • Google Marketing
  • Direct Marketing
    • Brand Management
    • Marketing Attribution and Consulting
  • Mobile Marketing
  • Event Management
  • PR Solutions
No Result
View All Result
mGrowTech
No Result
View All Result
Home Al, Analytics and Automation

Implementing Hybrid Semantic-Lexical Search in RAG

Josh by Josh
June 17, 2026
in Al, Analytics and Automation
0


In this article, you will learn how to implement a hybrid search strategy for RAG systems by combining BM25 lexical search with semantic search, fused together using Reciprocal Rank Fusion.

Topics we will cover include:

  • Why hybrid search outperforms either lexical or semantic search alone in retrieval-augmented generation systems.
  • How to implement BM25 lexical search and dense vector semantic search as independent retrieval engines in Python.
  • How to merge both rankings using Reciprocal Rank Fusion (RRF) to produce a final, balanced retrieval result.

Let’s get straight to it.

Implementing Hybrid Semantic-Lexical Search in RAG

Implementing Hybrid Semantic-Lexical Search in RAG

Introduction

Implementing hybrid search strategies is a critical step in building modern RAG (Retrieval-Augmented Generation) systems, especially when shifting from prototype to production-ready solutions.

READ ALSO

MIT’s Initiative for New Manufacturing builds momentum | MIT News

Meet Qwen-RobotSuite: Three Embodied AI Models for VLA Manipulation, Video World Modeling, and Navigation

There is little argument against semantic search — fueled by dense vectors or embeddings, which are numerical representations of text — being incredibly useful at understanding semantics, synonyms, and context. However, lexical, keyword-based search with approaches like BM25 covers a small blind spot neglected by semantic search. Combining the best of both worlds is therefore the perfect recipe to take your RAG system’s retrieval mechanism the extra mile.

Let’s explore how to implement such a hybrid search strategy through a gentle coding example, guiding you through every step of the process!

Note: If you are unfamiliar with RAG systems, you may find the “Understanding RAG” article series remarkably insightful for getting the most out of this read. In particular, I recommend acquiring an understanding of vector databases first through this article.

Step-by-Step Implementation

The first step is to ensure all the necessary external Python libraries are installed, in particular these three:

!pip install rank_bm25 sentence–transformers requests

  • rank_bm25: an implementation of the BM25 lexical search algorithm for information retrieval (BM stands for “Best Matching”).
  • sentence-transformers: provides pre-trained language models for generating text embeddings. In a real setting, you may already have your own vector database containing many document embeddings and not need this, but we will use it here to simulate the construction of a toy vector database and illustrate hybrid search on it.
  • requests: used to fetch the raw dataset package from a public GitHub datasets repository prepared for this example.

With these ingredients at hand, we start by loading the dataset and storing the raw texts in a list (we do so because it is a small dataset).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

import requests

import zipfile

import io

import os

 

# Downloading and extracting the dataset from the compressed file

url = “https://github.com/gakudo-ai/open-datasets/raw/refs/heads/main/asia_documents.zip”

response = requests.get(url)

with zipfile.ZipFile(io.BytesIO(response.content)) as z:

    z.extractall(“asia_data”)

 

# Loading documents and getting their filenames

documents = []

doc_names = []

for file in os.listdir(“asia_data”):

    if file.endswith(“.txt”):

        with open(f“asia_data/{file}”, “r”, encoding=“utf-8”) as f:

            documents.append(f.read())

            doc_names.append(file)

 

print(f“Loaded {len(documents)} documents for the knowledge base.”)

The hybrid search process is divided into three stages: two of them take place in parallel, or independently from each other. The third is where the fusion of both approaches happens, using a merging method called Reciprocal Rank Fusion (RRF).

Let’s cover lexical search with BM25 first:

from rank_bm25 import BM25Okapi

 

# BM25 requires that each text is tokenized as a (sub)list of words

tokenized_corpus = [doc.lower().split() for doc in documents]

bm25 = BM25Okapi(tokenized_corpus)

 

def search_bm25(query, top_k=3):

    tokenized_query = query.lower().split()

    

    # Getting scores (lexical relevance to the query) for all documents

    scores = bm25.get_scores(tokenized_query)

    

    # Ranking documents by score

    ranked_indices = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)

    return ranked_indices[:top_k], scores

The lexical search process has been encapsulated in a function called search_bm25(). This function takes two input arguments: a string containing the user’s query to the RAG system, and the number of top results to retrieve. The rank_bm25 library provides a get_scores() method that computes, for each document — treated as a collection of tokens — a lexical relevance score. We then rank documents by decreasing score, select the top-k, and return them.

Meanwhile, the semantic search engine first uses a sentence transformer model to obtain embedding vectors for the texts and the user query, then applies a vector similarity metric like cosine similarity to rank texts by semantic relevance and retrieve the most relevant k:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

from sentence_transformers import SentenceTransformer, util

import torch

 

# Loading the pre-trained embedding model

model = SentenceTransformer(‘all-MiniLM-L6-v2’)

 

# Pre-compute embeddings for our corpus (our “Vector DB”)

# You do not need this step if you already have an external vector database:

# you may read and import your document vectors instead

doc_embeddings = model.encode(documents, convert_to_tensor=True)

 

def search_semantic(query, top_k=3):

    # Embedding the user’s query into a vector

    query_embedding = model.encode(query, convert_to_tensor=True)

    

    # Calculating cosine similarity between the query and all documents

    cosine_scores = util.cos_sim(query_embedding, doc_embeddings)[0]

    

    # Ranking documents by similarity

    ranked_indices = torch.argsort(cosine_scores, descending=True).tolist()

    return ranked_indices[:top_k], cosine_scores.tolist()

Time to put it all together. The two scores calculated for each document cannot simply be added, because they operate on very different numeric scales. Instead, we perform the fusion based on ranks rather than raw similarity or relevance scores. For this, RRF is the gold industry standard for fusing ranking information: it calculates an overall ranking for each document by rewarding those that appear in high positions across both lists. The underlying logic is somewhat similar to that of the harmonic mean operator in statistics.

The overarching hybrid search process is implemented as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

def hybrid_search(query, top_k=3):

    # 1. Obtaining the two standalone search rankings

    bm25_ranks, _ = search_bm25(query, top_k=len(documents))

    semantic_ranks, _ = search_semantic(query, top_k=len(documents))

    

    # 2. Applying RRF formula: RRF_score = 1 / (k + rank)

    rrf_scores = {i: 0.0 for i in range(len(documents))}

    k_constant = 60  # The value of 60 is a standard academic convention

    

    # Adding RRF scores from BM25

    for rank, doc_idx in enumerate(bm25_ranks):

        rrf_scores[doc_idx] += 1.0 / (k_constant + rank + 1)

        

    # Adding RRF scores from semantic search

    for rank, doc_idx in enumerate(semantic_ranks):

        rrf_scores[doc_idx] += 1.0 / (k_constant + rank + 1)

    

    # 3. Sorting documents by their final fused RRF score

    final_ranked_indices = sorted(rrf_scores.keys(), key=lambda idx: rrf_scores[idx], reverse=True)

    

    return final_ranked_indices[:top_k], rrf_scores

Now it’s time to try it all out. Let’s formulate a user query and see what results we get.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

query = “Which nation is best known for rice fields and paddies?”

 

print(f“— Query: ‘{query}’ —“)

 

# Testing Semantic (good at understanding aspects like “nation-wise nuances” and conceptual titles)

print(“\nTop Semantic Results:”)

sem_indices, _ = search_semantic(query)

for idx in sem_indices:

    print(f“- {doc_names[idx]}”)

 

# Testing BM25 (good at finding exact keyword-based matches like “rice”, “field”, “paddy”)

print(“\nTop BM25 Results:”)

bm25_indices, _ = search_bm25(query)

for idx in bm25_indices:

    print(f“- {doc_names[idx]}”)

 

# Testing Hybrid (balances both)

print(“\nTop Hybrid (RRF) Results:”)

hybrid_indices, _ = hybrid_search(query)

for idx in hybrid_indices:

    print(f“- {doc_names[idx]}”)

The results are not excellent compared to a production RAG system, but bear in mind we tested this on a tiny, nine-document dataset. With that context, the outcome is quite reasonable.

—– Query: ‘Which nation is best known for rice fields and paddies?’ —–

 

Top Semantic Results:

– Vietnam.txt

– South_Korea.txt

– Thailand.txt

 

Top BM25 Results:

– Indonesia.txt

– Japan.txt

– Philippines.txt

 

Top Hybrid (RRF) Results:

– Vietnam.txt

– Thailand.txt

– Indonesia.txt

Try modifying the query and replacing it with others related to temples, beaches, mountains, or anything else that comes to mind when thinking about eastern destinations. Can you find a scenario in which both the semantic results and the BM25 results are highly consistent with each other?

Wrapping Up

This article guided you through implementing a hybrid search mechanism for the retrieval stage of RAG systems. Choosing not to rely solely on semantic search is an important consideration when scaling RAG solutions to production environments.



Source_link

Related Posts

MIT’s Initiative for New Manufacturing builds momentum | MIT News
Al, Analytics and Automation

MIT’s Initiative for New Manufacturing builds momentum | MIT News

June 16, 2026
Al, Analytics and Automation

Meet Qwen-RobotSuite: Three Embodied AI Models for VLA Manipulation, Video World Modeling, and Navigation

June 16, 2026
Building a Multi-Tool Gemma 4 Agent with Error Recovery
Al, Analytics and Automation

Building a Multi-Tool Gemma 4 Agent with Error Recovery

June 16, 2026
Sakana AI Commercializes AB-MCTS in Sakana Marlin, an Enterprise Agent Generating Up to 100-Page Research Reports With Slides
Al, Analytics and Automation

Sakana AI Commercializes AB-MCTS in Sakana Marlin, an Enterprise Agent Generating Up to 100-Page Research Reports With Slides

June 16, 2026
The Statistics of Token Selection: Logits, Temperature, and Top-P Walkthrough
Al, Analytics and Automation

The Statistics of Token Selection: Logits, Temperature, and Top-P Walkthrough

June 15, 2026
Top Financial Data Labeling Companies for Secure AI Data
Al, Analytics and Automation

Top Financial Data Labeling Companies for Secure AI Data

June 15, 2026
Next Post
Forrester’s B2B Forum EMEA 2026 Awards

Forrester’s B2B Forum EMEA 2026 Awards

POPULAR NEWS

Trump ends trade talks with Canada over a digital services tax

Trump ends trade talks with Canada over a digital services tax

June 28, 2025
15 Trending Songs on TikTok in 2025 (+ How to Use Them)

15 Trending Songs on TikTok in 2025 (+ How to Use Them)

June 18, 2025
Communication Effectiveness Skills For Business Leaders

Communication Effectiveness Skills For Business Leaders

June 10, 2025
App Development Cost in Singapore: Pricing Breakdown & Insights

App Development Cost in Singapore: Pricing Breakdown & Insights

June 22, 2025
Comparing the Top 7 Large Language Models LLMs/Systems for Coding in 2025

Comparing the Top 7 Large Language Models LLMs/Systems for Coding in 2025

November 4, 2025

EDITOR'S PICK

What leaders should say about ‘noise’ outside the company

February 5, 2026
Even before the Xbox layoffs, there was ‘tension’ at Halo Studios

Even before the Xbox layoffs, there was ‘tension’ at Halo Studios

July 3, 2025
Google Think Retail 2025: News and Updates

Google Think Retail 2025: News and Updates

September 15, 2025
Small Business Password Security Keeps Customers: Here’s the Proof

Small Business Password Security Keeps Customers: Here’s the Proof

May 7, 2026

About

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Check our landing page for details.

Follow us

Categories

  • Account Based Marketing
  • Ad Management
  • Al, Analytics and Automation
  • Brand Management
  • Channel Marketing
  • Digital Marketing
  • Direct Marketing
  • Event Management
  • Google Marketing
  • Marketing Attribution and Consulting
  • Marketing Automation
  • Mobile Marketing
  • PR Solutions
  • Social Media Management
  • Technology And Software
  • Uncategorized

Recent Posts

  • Grow a Garden 2 Script (No Key, Auto Steal, Auto Plant)
  • Forrester’s B2B Forum EMEA 2026 Awards
  • Implementing Hybrid Semantic-Lexical Search in RAG
  • What’s the Big Deal and How to Get Started
  • About Us
  • Disclaimer
  • Contact Us
  • Privacy Policy
No Result
View All Result
  • Technology And Software
    • Account Based Marketing
    • Channel Marketing
    • Marketing Automation
      • Al, Analytics and Automation
      • Ad Management
  • Digital Marketing
    • Social Media Management
    • Google Marketing
  • Direct Marketing
    • Brand Management
    • Marketing Attribution and Consulting
  • Mobile Marketing
  • Event Management
  • PR Solutions