Introduction

R2R’s hybrid search combines the strengths of traditional keyword-based searching and modern semantic understanding. This approach provides more contextually accurate and comprehensive results, particularly for complex queries. Here’s an example demonstrating how R2R’s hybrid search outperforms traditional RAG:

Example: UK Exports in 2023

Query: “What were the UK’s top exports in 2023?”

R2R’s hybrid search identifies documents mentioning “UK exports” and “2023”, whereas semantic search alone might typically find related concepts like trade balance and economic reports. The hybrid approach ensures that the results are both relevant to the specific year and focused on exports, rather than broader economic topics.

Setup

This guide assumes R2R is already installed and the basic quickstart has been completed.

Executing a Hybrid Search

To execute a hybrid search, simply pass the additional flag do_hybrid_search the quickstart search command:

python -m r2r.examples.quickstart search --query="What is a fierce nerd?" --do_hybrid_search
Expected Output:
{
  ...
  'text': 'in taking the results personally.Fierce nerds also tend to be somewhat overconfident, especially\nwhen young. It might seem like it would be a disadvantage to be\nmistaken about one's abilities, but empirically it isn't. Up to a\npoint, confidence is a self-fullfilling prophecy.Another quality you find in most fierce nerds is intelligence. Not\nall nerds are smart, but the fierce ones are always at least\nmoderately so. If they weren't, they wouldn't have the confidence\nto be fierce.'
},
{
  ...
  'text': 'over a certain size without being fierce, so now that nerds can win\nat business, fierce nerds will increasingly monopolize the really\nbig successes.The bad news is that if it's not exercised, your fierceness will\nturn to bitterness, and you will become an intellectual playground\nbully: the grumpy sysadmin, the forum troll, the \nhater, the shooter\ndown of new ideas.How do you avoid this fate? Work on ambitious projects. If you\nsucceed, it will bring you a kind of satisfaction that neutralizes'
},
{
  ...
  'text': 'nerds have traditionally solved. As the world progresses, the number\nof things you can win at by getting the right answer increases.\nRecently getting rich became \none of them: 7 of the 8 richest people\nin America are now fierce nerds.Indeed, being a fierce nerd is probably even more helpful in business\nthan in nerds' original territory of scholarship. Fierceness seems\noptional there. Darwin for example doesn't seem to have been\nespecially fierce. Whereas it's impossible to be the CEO of a company'
},
...

The do_hybrid_search flag can also be passed as an argument for the RAG method.

Sample Code

Passing the flag above modifies downstream a downstream VectorSearchSettings object, which is used as when searching with R2R as shown:

from r2r import R2RBuilder, VectorSearchSettings

app = R2R()

vector_search_settings = VectorSearchSettings(
    do_hybrid_search = True,
    ...
)

search_results = app.search(
    query="What is AI?", vector_search_settings=vector_search_settings
)

How Hybrid Search Works

Summary

Hybrid search in R2R combines the strengths of traditional keyword-based searching and modern semantic understanding. By integrating full-text and vector-based searches, it provides more contextually accurate and comprehensive results, particularly for complex queries.

For detailed setup and basic functionality, refer back to the R2R Quickstart. For more advanced usage and customization options, join the R2R Discord community.