Engineering
November 11, 2024

Using Hybrid Search to Deliver Fast and Contextually Relevant Results

Mohammed Rafiq
,
Co-Founder and CTO

In search-centric applications like knowledge bases and customer support portals, delivering quick, precise, and contextually accurate information is essential. However, developers often face challenges balancing semantic search, which captures the intent behind queries, with keyword-based search retrieving exact term matches. Hybrid Search solves this by combining the strengths of both approaches, making it an ideal solution for search-centric applications.

Why Hybrid Search?

With Hybrid Search, you get the best of both worlds:

  • Semantic search captures the context and intent behind user queries, even if the wording isn’t precise.
  • Keyword search finds exact term matches, ensuring that critical keywords are prioritized in the results.

This dual approach enhances precision and captures nuanced meanings and context that traditional keyword searches often miss. Developers using Hybrid Search benefit from:

  • Higher Recall: Retrieving a broader range of relevant data.
  • Greater Precision: Surfacing precisely the right data to meet the user’s query.

Key Use Cases for Hybrid Search

Hybrid Search adds value across various fields:

  1. Legal and Compliance: Accuracy and context are equally crucial in legal research. Hybrid Search ensures that relevant legal documents or compliance records are retrieved with precise terminology and essential contextual information.
  2. Customer Support: For support teams addressing user queries, Hybrid Search delivers exact instructions while considering broader context. This leads to faster, more accurate answers and an improved user experience.
  3. Knowledge Management: For large data sources (like Google Drive or Notion) where both exact terms and contextual relevance matter, Hybrid Search enables efficient document retrieval, even for ambiguous terms.
  4. Enterprise Search: Hybrid Search streamlines data retrieval across various departments by handling various document types and formats. This adaptability offers teams more targeted, comprehensive results and enhances internal data accessibility.

Technical Overview of Ragie’s Hybrid Search System

In a standard semantic search system, we take document chunks, convert them into dense embeddings, and store these embeddings in a vector database. During retrieval, the system uses cosine similarity to rank chunks based on relevance to the query. This approach is powerful for finding semantically related content, even when there’s no exact keyword match.

To build Ragie's hybrid search, we combined semantic retrieval with keyword-based retrieval using BM25 scoring. Every document chunk in Ragie’s pipeline is indexed in both the semantic (vector) index and a full-text (keyword) index, enabling powerful and flexible searches that balance understanding and precision.

During retrieval, hybrid search begins with a semantic search on the vector database’s chunk index and a keyword search on the full-text chunk index. Any filters applied at query time are evaluated against both indexes, ensuring that all returned results are scoped precisely to the user’s requirements.

Weighting, Merging, and Reranking the Results

We apply a weighting mechanism to balance these two result sets: 75% weight to semantic results and 25% to keyword results. We then combine these weighted results using a reciprocal rank fusion (RRF) method to yield a unified result set. The final step involves reranking the combined result using an LLM-based reranker, which optimizes relevance based on a learned understanding of content.

In a prior blog post, we introduced Ragie’s advanced RAG setup with hierarchical document summarization (hierarchical semantic search). Our hybrid search ensures seamless integration with this system, enabling customers to leverage hierarchical summarization alongside hybrid search. This introduces our Hybrid Hierarchical Search feature. With this method, queries pass through a multi-stage process: first, a hybrid search on the summary index to identify top documents, then a hybrid search for chunks within each document, with final chunk selection using reciprocal rank fusion (RRF) across multiple document chunk lists.

Additionally, we recently launched Partitions, a feature that allows customers to logically separate data sent to Ragie. Hybrid search is fully partition-aware, ensuring that tf and idf scores for BM25 are calculated independently within each partition. This guarantees accuracy in keyword retrieval and prevents score interference across partitions.

Implementation Guide for Developers

Ragie uses Hybrid Search by default, here’s an example of how to configure it:


curl --request POST \     
	--url https://api.ragie.ai/retrievals \     
	--header 'accept: application/json' \     
	--header 'content-type: application/json' \    
	--data '
{  
	"query": "How do I export my data?",  
	"rerank": true,  
	"top_k": 6,  
	"partition": "customer-support"
}
'

In this example:

  • query specifies the user's question.
  • rerank enhances semantic relevance within the "customer-support" partition.
  • top_k limits retrieved results, refining the search.

You can adjust the parameters to refine the scope of the retrieval and results, visit our API docs to get started with Hybrid Search. Please ping us on Discord if you have any feedback as you begin to integrate Ragie.