oxirs-vec 0.2.4

Vector index abstractions for semantic similarity and AI-augmented querying
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! Diversity-aware re-ranking strategies
//!
//! Diversity re-ranking ensures that search results cover different aspects
//! of the query rather than redundant similar documents.
//!
//! ## Strategies
//! - **MMR (Maximal Marginal Relevance)**: Balances relevance and diversity
//! - **Cluster-based**: Groups similar documents and selects from each cluster
//! - **Topic-based**: Ensures topical diversity across results

use crate::reranking::types::{RerankingResult, ScoredCandidate};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

/// Diversity strategy for re-ranking
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DiversityStrategy {
    /// Maximal Marginal Relevance (MMR)
    MaximalMarginalRelevance,
    /// Cluster-based diversity
    ClusterBased,
    /// Topic-based diversity
    TopicBased,
    /// No diversity (baseline)
    None,
}

/// Diversity re-ranker
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiversityReranker {
    /// Diversity weight (0.0 = relevance only, 1.0 = diversity only)
    weight: f32,
    /// Strategy to use
    strategy: DiversityStrategy,
    /// Similarity threshold for considering documents as similar
    similarity_threshold: f32,
}

impl DiversityReranker {
    /// Create new diversity re-ranker with default strategy
    pub fn new(weight: f32) -> Self {
        Self {
            weight: weight.clamp(0.0, 1.0),
            strategy: DiversityStrategy::MaximalMarginalRelevance,
            similarity_threshold: 0.85,
        }
    }

    /// Create with specific strategy
    pub fn with_strategy(weight: f32, strategy: DiversityStrategy) -> Self {
        Self {
            weight: weight.clamp(0.0, 1.0),
            strategy,
            similarity_threshold: 0.85,
        }
    }

    /// Set similarity threshold
    pub fn set_similarity_threshold(mut self, threshold: f32) -> Self {
        self.similarity_threshold = threshold.clamp(0.0, 1.0);
        self
    }

    /// Apply diversity re-ranking to candidates
    pub fn apply_diversity(
        &self,
        candidates: &[ScoredCandidate],
    ) -> RerankingResult<Vec<ScoredCandidate>> {
        if candidates.is_empty() || self.weight == 0.0 {
            return Ok(candidates.to_vec());
        }

        match self.strategy {
            DiversityStrategy::MaximalMarginalRelevance => self.mmr_rerank(candidates),
            DiversityStrategy::ClusterBased => self.cluster_based_rerank(candidates),
            DiversityStrategy::TopicBased => self.topic_based_rerank(candidates),
            DiversityStrategy::None => Ok(candidates.to_vec()),
        }
    }

    /// MMR (Maximal Marginal Relevance) re-ranking
    ///
    /// Selects documents that maximize:
    /// MMR = λ * Relevance(d) - (1-λ) * max Similarity(d, already_selected)
    fn mmr_rerank(&self, candidates: &[ScoredCandidate]) -> RerankingResult<Vec<ScoredCandidate>> {
        let lambda = 1.0 - self.weight; // Convert weight to λ parameter
        let mut selected = Vec::new();
        let mut remaining: Vec<_> = candidates.to_vec();

        // Select first candidate (highest relevance)
        if let Some(first) = remaining.first().cloned() {
            selected.push(first);
            remaining.remove(0);
        }

        // Iteratively select documents maximizing MMR
        while !remaining.is_empty() && selected.len() < candidates.len() {
            let mut best_idx = 0;
            let mut best_mmr = f32::NEG_INFINITY;

            for (idx, candidate) in remaining.iter().enumerate() {
                // Relevance component
                let relevance = candidate.effective_score();

                // Diversity component: max similarity to already selected
                let max_similarity = selected
                    .iter()
                    .map(|sel| self.compute_similarity(candidate, sel))
                    .fold(0.0f32, f32::max);

                // MMR score
                let mmr = lambda * relevance - (1.0 - lambda) * max_similarity;

                if mmr > best_mmr {
                    best_mmr = mmr;
                    best_idx = idx;
                }
            }

            // Select best MMR candidate
            if best_idx < remaining.len() {
                selected.push(remaining.remove(best_idx));
            } else {
                break;
            }
        }

        Ok(selected)
    }

    /// Cluster-based diversity re-ranking
    ///
    /// Groups similar documents into clusters and selects
    /// representatives from each cluster to ensure diversity.
    fn cluster_based_rerank(
        &self,
        candidates: &[ScoredCandidate],
    ) -> RerankingResult<Vec<ScoredCandidate>> {
        if candidates.len() <= 2 {
            return Ok(candidates.to_vec());
        }

        // Simple greedy clustering
        let mut clusters: Vec<Vec<ScoredCandidate>> = Vec::new();
        let mut assigned = HashSet::new();

        for (idx, candidate) in candidates.iter().enumerate() {
            if assigned.contains(&idx) {
                continue;
            }

            // Start new cluster
            let mut cluster = vec![candidate.clone()];
            assigned.insert(idx);

            // Find similar candidates
            for (other_idx, other) in candidates.iter().enumerate() {
                if assigned.contains(&other_idx) {
                    continue;
                }

                let similarity = self.compute_similarity(candidate, other);
                if similarity > self.similarity_threshold {
                    cluster.push(other.clone());
                    assigned.insert(other_idx);
                }
            }

            clusters.push(cluster);
        }

        // Select best candidate from each cluster
        let mut result = Vec::new();
        let num_per_cluster = (candidates.len() / clusters.len().max(1)).max(1);

        for cluster in clusters {
            // Sort cluster by score
            let mut sorted_cluster = cluster;
            sorted_cluster.sort_by(|a, b| {
                b.effective_score()
                    .partial_cmp(&a.effective_score())
                    .unwrap_or(std::cmp::Ordering::Equal)
            });

            // Take top candidates from this cluster
            result.extend(sorted_cluster.into_iter().take(num_per_cluster));
        }

        // Sort final result by score
        result.sort_by(|a, b| {
            b.effective_score()
                .partial_cmp(&a.effective_score())
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(result)
    }

    /// Topic-based diversity re-ranking
    ///
    /// Ensures results cover different topics by analyzing
    /// keyword distributions and selecting diverse documents.
    fn topic_based_rerank(
        &self,
        candidates: &[ScoredCandidate],
    ) -> RerankingResult<Vec<ScoredCandidate>> {
        // Extract topics (keywords) from each candidate
        let mut doc_topics: Vec<HashSet<String>> = Vec::new();

        for candidate in candidates {
            let content = candidate.content.as_deref().unwrap_or("");
            let topics = self.extract_topics(content);
            doc_topics.push(topics);
        }

        // Select documents to maximize topic coverage
        let mut selected = Vec::new();
        let mut covered_topics = HashSet::new();
        let mut remaining_indices: Vec<usize> = (0..candidates.len()).collect();

        while !remaining_indices.is_empty() && selected.len() < candidates.len() {
            let mut best_idx = 0;
            let mut best_score = f32::NEG_INFINITY;

            for (list_idx, &doc_idx) in remaining_indices.iter().enumerate() {
                let candidate = &candidates[doc_idx];
                let topics = &doc_topics[doc_idx];

                // Relevance component
                let relevance = candidate.effective_score();

                // Diversity component: number of new topics
                let new_topics = topics.difference(&covered_topics).count() as f32;
                let total_topics = topics.len().max(1) as f32;
                let topic_novelty = new_topics / total_topics;

                // Combined score
                let score = (1.0 - self.weight) * relevance + self.weight * topic_novelty;

                if score > best_score {
                    best_score = score;
                    best_idx = list_idx;
                }
            }

            // Select best candidate
            if best_idx < remaining_indices.len() {
                let doc_idx = remaining_indices.remove(best_idx);
                selected.push(candidates[doc_idx].clone());

                // Update covered topics
                for topic in &doc_topics[doc_idx] {
                    covered_topics.insert(topic.clone());
                }
            } else {
                break;
            }
        }

        Ok(selected)
    }

    /// Compute similarity between two candidates
    fn compute_similarity(&self, a: &ScoredCandidate, b: &ScoredCandidate) -> f32 {
        // Extract keywords from both documents
        let a_content = a.content.as_deref().unwrap_or("");
        let b_content = b.content.as_deref().unwrap_or("");

        let a_words: HashSet<String> = a_content
            .to_lowercase()
            .split_whitespace()
            .filter(|w| w.len() > 3) // Filter short words
            .map(|w| w.to_string())
            .collect();

        let b_words: HashSet<String> = b_content
            .to_lowercase()
            .split_whitespace()
            .filter(|w| w.len() > 3)
            .map(|w| w.to_string())
            .collect();

        if a_words.is_empty() || b_words.is_empty() {
            return 0.0;
        }

        // Jaccard similarity
        let intersection = a_words.intersection(&b_words).count() as f32;
        let union = a_words.union(&b_words).count() as f32;

        if union == 0.0 {
            0.0
        } else {
            intersection / union
        }
    }

    /// Extract topics (keywords) from document
    fn extract_topics(&self, document: &str) -> HashSet<String> {
        // Simple keyword extraction
        // In production: use TF-IDF, NER, or topic modeling
        document
            .to_lowercase()
            .split_whitespace()
            .filter(|w| w.len() > 4) // Only meaningful words
            .map(|w| w.to_string())
            .collect()
    }
}

impl Default for DiversityReranker {
    fn default() -> Self {
        Self::new(0.3)
    }
}

#[cfg(test)]
mod tests {
    type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
    use super::*;

    fn create_test_candidates() -> Vec<ScoredCandidate> {
        vec![
            ScoredCandidate::new("doc1", 0.9, 0)
                .with_content("machine learning deep neural networks")
                .with_reranking_score(0.85),
            ScoredCandidate::new("doc2", 0.85, 1)
                .with_content("machine learning algorithms classification")
                .with_reranking_score(0.8),
            ScoredCandidate::new("doc3", 0.7, 2)
                .with_content("database management systems SQL queries")
                .with_reranking_score(0.75),
            ScoredCandidate::new("doc4", 0.65, 3)
                .with_content("web development JavaScript frameworks")
                .with_reranking_score(0.7),
        ]
    }

    #[test]
    fn test_mmr_rerank() -> Result<()> {
        let reranker = DiversityReranker::new(0.5);
        let candidates = create_test_candidates();

        let result = reranker.mmr_rerank(&candidates)?;

        // Should have all candidates
        assert_eq!(result.len(), candidates.len());

        // First should be highest scoring
        assert_eq!(result[0].id, "doc1");

        // Should not have all machine learning docs together
        // (diversity should spread them out)
        let first_three_ids: Vec<_> = result.iter().take(3).map(|c| c.id.as_str()).collect();
        let all_ml = first_three_ids
            .iter()
            .all(|id| id.starts_with("doc1") || id.starts_with("doc2"));
        assert!(!all_ml, "MMR should diversify results");
        Ok(())
    }

    #[test]
    fn test_cluster_based_rerank() -> Result<()> {
        let reranker = DiversityReranker::with_strategy(0.5, DiversityStrategy::ClusterBased);
        let candidates = create_test_candidates();

        let result = reranker.cluster_based_rerank(&candidates)?;

        assert!(!result.is_empty());
        assert!(result.len() <= candidates.len());
        Ok(())
    }

    #[test]
    fn test_topic_based_rerank() -> Result<()> {
        let reranker = DiversityReranker::with_strategy(0.6, DiversityStrategy::TopicBased);
        let candidates = create_test_candidates();

        let result = reranker.topic_based_rerank(&candidates)?;

        assert_eq!(result.len(), candidates.len());

        // Verify diversity: first few results should cover different topics
        let first_two = &result[0..2.min(result.len())];
        let similarity = reranker.compute_similarity(&first_two[0], &first_two[1]);

        // Should have lower similarity due to diversity
        assert!(
            similarity < 0.8,
            "Topic-based reranking should increase diversity"
        );
        Ok(())
    }

    #[test]
    fn test_no_diversity() -> Result<()> {
        let reranker = DiversityReranker::new(0.0); // No diversity
        let candidates = create_test_candidates();

        let result = reranker.apply_diversity(&candidates)?;

        // Should return unchanged
        assert_eq!(result.len(), candidates.len());
        for (orig, res) in candidates.iter().zip(result.iter()) {
            assert_eq!(orig.id, res.id);
        }
        Ok(())
    }

    #[test]
    fn test_similarity_computation() {
        let reranker = DiversityReranker::new(0.3);

        let a = ScoredCandidate::new("a", 0.8, 0).with_content("machine learning neural networks");

        let b = ScoredCandidate::new("b", 0.7, 1).with_content("machine learning algorithms");

        let c = ScoredCandidate::new("c", 0.6, 2).with_content("database systems SQL");

        let sim_ab = reranker.compute_similarity(&a, &b);
        let sim_ac = reranker.compute_similarity(&a, &c);

        // a and b should be more similar than a and c
        assert!(sim_ab > sim_ac);
    }

    #[test]
    fn test_topic_extraction() {
        let reranker = DiversityReranker::new(0.3);
        let doc = "machine learning and deep neural networks for classification";

        let topics = reranker.extract_topics(doc);

        assert!(topics.contains("machine"));
        assert!(topics.contains("learning"));
        assert!(topics.contains("neural"));
        assert!(topics.contains("networks"));
        assert!(topics.contains("classification"));

        // Short words should be filtered
        assert!(!topics.contains("and"));
        assert!(!topics.contains("for"));
    }

    #[test]
    fn test_empty_candidates() -> Result<()> {
        let reranker = DiversityReranker::new(0.5);
        let candidates = vec![];

        let result = reranker.apply_diversity(&candidates)?;
        assert!(result.is_empty());
        Ok(())
    }

    #[test]
    fn test_single_candidate() -> Result<()> {
        let reranker = DiversityReranker::new(0.5);
        let candidates = vec![ScoredCandidate::new("doc1", 0.8, 0)
            .with_content("test")
            .with_reranking_score(0.85)];

        let result = reranker.apply_diversity(&candidates)?;
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].id, "doc1");
        Ok(())
    }
}