Skip to main content
LlamaIndex.TS supports multiple retrieval modes and strategies to find the most relevant documents for your queries.

Retrieval Strategy Overview

Retrieval modes determine how documents are searched and ranked:
  • DEFAULT - Standard semantic similarity search using embeddings
  • HYBRID - Combines semantic and keyword search
  • SPARSE - Keyword-based search (e.g., BM25)
  • MMR - Maximum Marginal Relevance for diverse results
  • SEMANTIC_HYBRID - Provider-specific semantic hybrid (e.g., Azure AI Search)

Vector Store Query Modes

Standard semantic search using vector embeddings:
Combines semantic similarity with keyword matching (requires vector store support):
Note: Not all vector stores support hybrid search. Check your vector store documentation.

Maximum Marginal Relevance (MMR)

MMR balances relevance with diversity to avoid redundant results:
Use Case: When you want diverse results that cover different aspects of the query. Uses embeddings to find conceptually similar content:
Strengths:
  • Understands synonyms and related concepts
  • Works across languages
  • Captures semantic meaning
Weaknesses:
  • May miss exact keyword matches
  • Requires quality embeddings
  • More computationally expensive

Keyword Search (BM25)

Traditional keyword-based retrieval using BM25 algorithm:
Strengths:
  • Excellent for exact matches
  • Fast and efficient
  • No embedding required
  • Good for technical terms and proper nouns
Weaknesses:
  • Doesn’t understand synonyms
  • Language-dependent
  • Misses semantic relationships

Hybrid Approach

Combine both for best results:

Custom Retrievers

Create custom retrieval logic by extending BaseRetriever:

Multi-Stage Retrieval

Retrieve many candidates, then rerank:

Retrieval with Filters

Combine retrieval modes with metadata filtering:

Comparing Retrieval Modes

BM25 Retriever Example

Standalone BM25 retriever for keyword search:

Best Practices

  1. Start with Default: Begin with semantic search, then experiment
  2. Use Hybrid for Production: Combines strengths of both approaches
  3. Tune Top-K: Retrieve more candidates (20-50) then rerank
  4. Apply Filters: Use metadata filters to narrow search space
  5. Monitor Performance: Track retrieval quality and latency
  6. Consider Domain: Technical docs may benefit from keyword search