Skip to main content
Diversity filtering post-processes search results to ensure the returned documents cover different aspects of the query, reducing redundancy and improving information coverage. When search returns many near-duplicates, diversity filtering selects representative documents to maximize information coverage.

How it works

Diversity filtering over-fetches candidates from the vector database, then applies post-processing to select diverse results.

Pipeline architecture

  1. Query embedding - Convert query text to dense vector
  2. Over-fetch - Retrieve 3x top_k candidates from database
  3. Re-embedding - Generate embeddings for retrieved documents
  4. Diversity filtering - Apply MMR or clustering method
  5. Limit - Return top_k diverse documents
  6. Optional RAG - Generate answer using diverse documents

Why diversity matters

Standard semantic search returns the k most similar documents, which often results in redundant information (e.g., 5 similar paragraphs from the same source). Diversity filtering ensures results cover different perspectives, sources, or aspects of the query topic.

Diversity methods

MMR (Maximal Marginal Relevance)

How it works: Balances query relevance with inter-document diversity using lambda parameter.Formula: MMR(d) = λ × sim(d, query) - (1-λ) × max_sim(d, selected)Configuration:
  • max_documents - Maximum documents to return
  • lambda_param - Relevance-diversity trade-off (default: 0.5)
Best for: Retrieval where both relevance and diversity matterSpeed: Fast (greedy algorithm)

Clustering-based

How it works: Groups retrieved documents into N clusters using embeddings, then samples M documents from each cluster.Configuration:
  • num_clusters - Number of topic clusters (default: 3)
  • samples_per_cluster - Docs per cluster (default: 2)
Best for: Ensuring coverage of distinct topic areasSpeed: Moderate (K-means clustering)

Key features

  • Two diversity methods: MMR and clustering-based selection
  • Over-fetching with configurable multiplier (default 3x)
  • Re-embedding ensures consistent similarity calculations
  • Works with all vector databases
  • Optional RAG integration

Implementation

Configuration

Required settings

string
required
Vector database API authentication
string
required
Target index name for search

Diversity configuration

string
default:"mmr"
Diversity method: "mmr" or "clustering"
integer
default:3
Over-fetch multiplier (retrieves top_k × multiplier candidates)

MMR-specific

integer
default:10
Maximum documents to return for MMR method
float
Relevance-diversity trade-off (0.0-1.0)
  • 1.0 = pure relevance
  • 0.5 = balanced
  • 0.0 = pure diversity

Clustering-specific

integer
default:3
Number of clusters for clustering method
integer
default:2
Documents to sample from each cluster

Example configurations

Search parameters

string
required
Search query text to embed and match against documents
integer
default:10
Number of diverse documents to return. Pipeline retrieves 3x this amount for diversity selection.
dict
Optional metadata filters to apply during retrieval

Use cases

When users need to see different perspectives:

Multi-document summarization

Provide diverse context to LLMs:

News aggregation

Show articles from different sources:

Research literature review

Cover different research approaches:

Method comparison

Choosing a method

1

Default: Use MMR

MMR is query-aware and provides explicit relevance-diversity control. Start with lambda_param=0.5.
2

Topic coverage: Use clustering

When you need guaranteed coverage of N distinct topics, use clustering with num_clusters=N.
3

Tune parameters

  • MMR: Adjust lambda (↑ relevance, ↓ diversity)
  • Clustering: Adjust num_clusters and samples_per_cluster
4

Evaluate

Measure diversity with metrics like average pairwise similarity or topic coverage.

Diversity helpers

The diversity filtering pipeline uses helper methods that can be used independently:

Over-fetching strategy

Over-fetching provides the diversity algorithm with more options for selecting diverse results. A 3x multiplier is recommended - it provides enough candidates without excessive latency.
Example:
Trade-offs:
  • Higher multiplier - More diversity options, higher latency
  • Lower multiplier - Faster, but limited diversity options

Performance considerations

Time complexity

  • MMR: O(k × n) where k=top_k, n=candidates
  • Clustering: O(n × d × iterations) for K-means

Optimization tips

  1. Cache embeddings - Store document embeddings to avoid recomputation
  2. Limit over-fetch - Balance diversity quality with latency (3-5x multiplier)
  3. Use metadata filters - Reduce candidate pool before diversity filtering
  4. Batch processing - Process multiple queries together for efficiency

Evaluation metrics

Measure diversity effectiveness:

Average pairwise similarity

Topic coverage

Count distinct topics/sources in results:

MMR

Maximal marginal relevance algorithm details

Semantic search

Initial retrieval before diversity filtering

Hybrid search

Dense + sparse retrieval

Reranking

Cross-encoder second-stage scoring