Hybrid Search

Learn how to implement and use hybrid search with R2R

Introduction

R2R’s hybrid search combines traditional keyword-based searching with modern semantic understanding, providing more accurate and contextually relevant results. This approach is particularly effective for complex queries where both specific terms and overall meaning are crucial.

How R2R Hybrid Search Works

  1. Full-Text Search: Utilizes PostgreSQL’s full-text search with ts_rank_cd and websearch_to_tsquery.
  2. Semantic Search: Performs vector similarity search using the query’s embedded representation.
  3. Reciprocal Rank Fusion (RRF): Merges results from full-text and semantic searches using the formula:
    COALESCE(1.0 / (rrf_k + full_text.rank_ix), 0.0) * full_text_weight +
    COALESCE(1.0 / (rrf_k + semantic.rank_ix), 0.0) * semantic_weight
  4. Result Ranking: Orders final results based on the combined RRF score.

Key Features

The full-text search component incorporates:

  • PostgreSQL’s tsvector for efficient text searching
  • websearch_to_tsquery for parsing user queries
  • ts_rank_cd for ranking full-text search results

The semantic search component uses:

  • Vector embeddings for storing and querying semantic representations
  • Cosine similarity for measuring the relevance of documents to the query

Configuration

VectorSearchSettings

Key settings for vector search configuration:

1class VectorSearchSettings(BaseModel):
2 use_hybrid_search: bool
3 search_limit: int
4 filters: dict[str, Any]
5 hybrid_search_settings: Optional[HybridSearchSettings]
6 # ... other settings

HybridSearchSettings

Specific parameters for hybrid search:

1class HybridSearchSettings(BaseModel):
2 full_text_weight: float
3 semantic_weight: float
4 full_text_limit: int
5 rrf_k: int

Usage Example

1from r2r import R2RClient
2
3client = R2RClient()
4
5vector_settings = {
6 "use_hybrid_search": True,
7 "search_limit": 20,
8 # Can add logical filters, as shown:
9 # "filters":{"category": {"$eq": "technology"}},
10 "hybrid_search_settings" : {
11 "full_text_weight": 1.0,
12 "semantic_weight": 5.0,
13 "full_text_limit": 200,
14 "rrf_k": 50
15 }
16}
17
18results = client.search(
19 query="Who was Aristotle?",
20 vector_search_settings=vector_settings
21)
22print(results)

Results Comparison

1{
2 "results": {
3 "vector_search_results": [
4 {
5 "score": 0.780314067545999,
6 "text": "Aristotle[A] (Greek: Ἀριστοτέλης Aristotélēs, pronounced [aristotélɛːs]; 384–322 BC) was an Ancient Greek philosopher and polymath...",
7 "metadata": {
8 "title": "aristotle.txt",
9 "version": "v0",
10 "chunk_order": 0
11 }
12 }, ...
13 ]
14 }
15}

Hybrid Search with RRF

1{
2 "results": {
3 "vector_search_results": [
4 {
5 "score": 0.0185185185185185,
6 "text": "Aristotle[A] (Greek: Ἀριστοτέλης Aristotélēs, pronounced [aristotélɛːs]; 384–322 BC) was an Ancient Greek philosopher and polymath...",
7 "metadata": {
8 "title": "aristotle.txt",
9 "version": "v0",
10 "chunk_order": 0,
11 "semantic_rank": 1,
12 "full_text_rank": 3
13 }, ...
14 }
15 ]
16 }
17}

Best Practices

  1. Optimize PostgreSQL indexing for both full-text and vector searches
  2. Regularly update search indices
  3. Monitor performance and adjust weights as needed
  4. Use appropriate vector dimensions and embedding models for your use case

Conclusion

R2R’s hybrid search offers a powerful solution for complex information retrieval needs, combining the strengths of keyword matching and semantic understanding. Its flexible configuration and use of Reciprocal Rank Fusion make it adaptable to a wide range of use cases, from technical documentation to broad, context-dependent queries.