jpx-engine 0.3.5

JMESPath query engine with introspection, discovery, and advanced features
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
//! BM25 search indexing for MCP tool discovery.
//!
//! This module provides JSON-in/JSON-out search indexing primitives using
//! the BM25 ranking algorithm. Designed for tool discovery across MCP servers.
//!
//! # Design
//!
//! - Pure JSON serialization for index portability
//! - BM25 chosen over TF-IDF for better term saturation and length normalization
//! - Session-scoped indices by default, but can be saved/restored
//!
//! # BM25 Formula
//!
//! ```text
//! score(D,Q) = Σ IDF(qi) * (f(qi,D) * (k1 + 1)) / (f(qi,D) + k1 * (1 - b + b * |D|/avgdl))
//! ```
//!
//! Where:
//! - f(qi,D) = term frequency of qi in document D
//! - |D| = document length
//! - avgdl = average document length
//! - k1 = term frequency saturation parameter (default 1.2)
//! - b = length normalization parameter (default 0.75)

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// BM25 index structure - fully JSON serializable
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bm25Index {
    /// Type marker for JSON identification
    #[serde(rename = "_type")]
    pub type_marker: String,

    /// Version for future compatibility
    #[serde(rename = "_version")]
    pub version: String,

    /// Index configuration
    pub options: IndexOptions,

    /// Total number of documents
    pub doc_count: usize,

    /// Average document length (in tokens)
    pub avg_doc_length: f64,

    /// Document metadata: id -> DocInfo
    pub docs: HashMap<String, DocInfo>,

    /// Inverted index: term -> TermInfo
    pub terms: HashMap<String, TermInfo>,
}

/// Index configuration options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexOptions {
    /// Fields to index (empty = treat input as text)
    #[serde(default)]
    pub fields: Vec<String>,

    /// Field to use as document ID (default: array index)
    #[serde(default)]
    pub id_field: Option<String>,

    /// Normalize case (default: true)
    #[serde(default = "default_true")]
    pub lowercase: bool,

    /// Terms to exclude from indexing
    #[serde(default)]
    pub stopwords: Vec<String>,

    /// BM25 k1 parameter (term frequency saturation)
    #[serde(default = "default_k1")]
    pub k1: f64,

    /// BM25 b parameter (length normalization)
    #[serde(default = "default_b")]
    pub b: f64,
}

fn default_true() -> bool {
    true
}

fn default_k1() -> f64 {
    1.2
}

fn default_b() -> f64 {
    0.75
}

impl Default for IndexOptions {
    fn default() -> Self {
        Self {
            fields: Vec::new(),
            id_field: None,
            lowercase: true,
            stopwords: Vec::new(),
            k1: 1.2,
            b: 0.75,
        }
    }
}

/// Document metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocInfo {
    /// Document length in tokens
    pub length: usize,

    /// Per-field token counts (for multi-field indices)
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub field_lengths: HashMap<String, usize>,

    /// Original document (optional, for returning with results)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<serde_json::Value>,
}

/// Term information in the inverted index
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TermInfo {
    /// Document frequency (number of documents containing this term)
    pub df: usize,

    /// Postings: doc_id -> term frequency in that document
    pub postings: HashMap<String, usize>,
}

/// Search result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    /// Document ID
    pub id: String,

    /// BM25 score
    pub score: f64,

    /// Matched terms and their locations
    pub matches: HashMap<String, Vec<String>>,

    /// Original document (if stored in index)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub doc: Option<serde_json::Value>,
}

/// Score explanation for debugging
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScoreExplanation {
    /// Document ID
    pub id: String,

    /// Total score
    pub total_score: f64,

    /// Per-term breakdown
    pub term_scores: Vec<TermScoreDetail>,
}

/// Per-term score breakdown
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TermScoreDetail {
    /// The term
    pub term: String,

    /// Term frequency in document
    pub tf: usize,

    /// Document frequency (corpus-wide)
    pub df: usize,

    /// IDF component
    pub idf: f64,

    /// TF saturation component
    pub tf_component: f64,

    /// Final score contribution
    pub score: f64,
}

impl Bm25Index {
    /// Create a new empty index with the given options
    pub fn new(options: IndexOptions) -> Self {
        Self {
            type_marker: "jpx:bm25_index".to_string(),
            version: "1.0".to_string(),
            options,
            doc_count: 0,
            avg_doc_length: 0.0,
            docs: HashMap::new(),
            terms: HashMap::new(),
        }
    }

    /// Build an index from an array of documents
    pub fn build(docs: &[serde_json::Value], options: IndexOptions) -> Self {
        let mut index = Self::new(options);
        let mut total_length = 0usize;

        for (i, doc) in docs.iter().enumerate() {
            let doc_id = index.get_doc_id(doc, i);
            let (tokens, field_lengths) = index.tokenize_doc(doc);
            let doc_length = tokens.len();
            total_length += doc_length;

            // Store document info
            index.docs.insert(
                doc_id.clone(),
                DocInfo {
                    length: doc_length,
                    field_lengths,
                    source: Some(doc.clone()),
                },
            );

            // Update inverted index
            let mut term_freqs: HashMap<String, usize> = HashMap::new();
            for token in tokens {
                *term_freqs.entry(token).or_insert(0) += 1;
            }

            for (term, freq) in term_freqs {
                let term_info = index.terms.entry(term).or_insert(TermInfo {
                    df: 0,
                    postings: HashMap::new(),
                });
                term_info.df += 1;
                term_info.postings.insert(doc_id.clone(), freq);
            }

            index.doc_count += 1;
        }

        // Calculate average document length
        if index.doc_count > 0 {
            index.avg_doc_length = total_length as f64 / index.doc_count as f64;
        }

        index
    }

    /// Get document ID from a document
    fn get_doc_id(&self, doc: &serde_json::Value, index: usize) -> String {
        if let Some(id) = self
            .options
            .id_field
            .as_ref()
            .and_then(|id_field| doc.get(id_field))
        {
            return match id {
                serde_json::Value::String(s) => s.clone(),
                serde_json::Value::Number(n) => n.to_string(),
                _ => format!("{}", index),
            };
        }
        format!("{}", index)
    }

    /// Tokenize a document into terms
    fn tokenize_doc(&self, doc: &serde_json::Value) -> (Vec<String>, HashMap<String, usize>) {
        let mut tokens = Vec::new();
        let mut field_lengths = HashMap::new();

        if self.options.fields.is_empty() {
            // Treat entire doc as text
            let text = self.extract_text(doc);
            tokens = self.tokenize_text(&text);
        } else {
            // Index specific fields
            for field in &self.options.fields {
                if let Some(value) = doc.get(field) {
                    let text = self.extract_text(value);
                    let field_tokens = self.tokenize_text(&text);
                    field_lengths.insert(field.clone(), field_tokens.len());
                    tokens.extend(field_tokens);
                }
            }
        }

        (tokens, field_lengths)
    }

    /// Extract text from a JSON value
    fn extract_text(&self, value: &serde_json::Value) -> String {
        match value {
            serde_json::Value::String(s) => s.clone(),
            serde_json::Value::Array(arr) => arr
                .iter()
                .filter_map(|v| {
                    if let serde_json::Value::String(s) = v {
                        Some(s.as_str())
                    } else {
                        None
                    }
                })
                .collect::<Vec<_>>()
                .join(" "),
            serde_json::Value::Object(obj) => obj
                .values()
                .map(|v| self.extract_text(v))
                .collect::<Vec<_>>()
                .join(" "),
            _ => String::new(),
        }
    }

    /// Tokenize text into terms
    fn tokenize_text(&self, text: &str) -> Vec<String> {
        let text = if self.options.lowercase {
            text.to_lowercase()
        } else {
            text.to_string()
        };

        text.split(|c: char| !c.is_alphanumeric() && c != '_')
            .filter(|s| !s.is_empty())
            .filter(|s| !self.options.stopwords.contains(&s.to_string()))
            .map(stem_simple)
            .collect()
    }
}

/// Simple plural stemmer for search indexing.
///
/// Handles common English plural forms:
/// - "databases" -> "database" (strip -s after vowel+consonant+e pattern)
/// - "ACLs" -> "ACL" (strip -s)
/// - "queries" -> "query" (ies -> y)
/// - "boxes" -> "box" (strip -es after x/z)
///
/// This is intentionally simple - it improves recall for plural/singular
/// matching without the complexity of a full Porter stemmer.
fn stem_simple(term: &str) -> String {
    let t = term.to_string();
    let len = t.len();

    // Skip very short terms
    if len < 3 {
        return t;
    }

    // Handle -ies -> -y (queries -> query, entries -> entry)
    if len > 3 && t.ends_with("ies") {
        return format!("{}y", &t[..len - 3]);
    }

    // Handle -xes -> -x and -zes -> -z (boxes -> box, buzzes handled by -ss check)
    if len > 3 && (t.ends_with("xes") || t.ends_with("zes")) {
        return t[..len - 2].to_string();
    }

    // Handle -sses -> -ss (classes -> class, but keep the ss)
    if len > 4 && t.ends_with("sses") {
        return t[..len - 2].to_string();
    }

    // Handle -shes -> -sh (dishes -> dish)
    if len > 4 && t.ends_with("shes") {
        return t[..len - 2].to_string();
    }

    // Handle simple -s (but not -ss like "lass", "class", "boss")
    // This covers: databases -> database, caches -> cache, shards -> shard
    if t.ends_with('s') && !t.ends_with("ss") {
        return t[..len - 1].to_string();
    }

    t
}

impl Bm25Index {
    /// Calculate IDF for a term
    fn idf(&self, term: &str) -> f64 {
        let df = self.terms.get(term).map(|t| t.df as f64).unwrap_or(0.0);

        if df == 0.0 {
            return 0.0;
        }

        let n = self.doc_count as f64;
        // IDF formula: ln((N - df + 0.5) / (df + 0.5) + 1)
        ((n - df + 0.5) / (df + 0.5) + 1.0).ln()
    }

    /// Calculate BM25 score for a document given query terms
    fn score_doc(&self, doc_id: &str, query_terms: &[String]) -> f64 {
        let doc_info = match self.docs.get(doc_id) {
            Some(info) => info,
            None => return 0.0,
        };

        let doc_length = doc_info.length as f64;
        let k1 = self.options.k1;
        let b = self.options.b;
        let avgdl = self.avg_doc_length;

        let mut score = 0.0;

        for term in query_terms {
            let idf = self.idf(term);
            let tf = self
                .terms
                .get(term)
                .and_then(|t| t.postings.get(doc_id))
                .copied()
                .unwrap_or(0) as f64;

            if tf > 0.0 {
                // BM25 formula
                let numerator = tf * (k1 + 1.0);
                let denominator = tf + k1 * (1.0 - b + b * doc_length / avgdl);
                score += idf * numerator / denominator;
            }
        }

        score
    }

    /// Search the index
    pub fn search(&self, query: &str, top_k: usize) -> Vec<SearchResult> {
        let query_terms = self.tokenize_text(query);

        if query_terms.is_empty() {
            return Vec::new();
        }

        // Find candidate documents (those containing at least one query term)
        let mut candidates: HashMap<String, f64> = HashMap::new();

        for term in &query_terms {
            if let Some(term_info) = self.terms.get(term) {
                for doc_id in term_info.postings.keys() {
                    candidates.entry(doc_id.clone()).or_insert(0.0);
                }
            }
        }

        // Score all candidates
        let mut results: Vec<SearchResult> = candidates
            .keys()
            .map(|doc_id| {
                let score = self.score_doc(doc_id, &query_terms);
                let matches = self.get_matches(doc_id, &query_terms);
                let doc = self.docs.get(doc_id).and_then(|d| d.source.clone());

                SearchResult {
                    id: doc_id.clone(),
                    score,
                    matches,
                    doc,
                }
            })
            .filter(|r| r.score > 0.0)
            .collect();

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

        // Return top_k results
        results.truncate(top_k);
        results
    }

    /// Get matched terms for a document
    fn get_matches(&self, doc_id: &str, query_terms: &[String]) -> HashMap<String, Vec<String>> {
        let mut matches: HashMap<String, Vec<String>> = HashMap::new();

        for term in query_terms {
            if self
                .terms
                .get(term)
                .is_some_and(|term_info| term_info.postings.contains_key(doc_id))
            {
                // For now, just note which field matched (if we have field info)
                matches
                    .entry("_matched".to_string())
                    .or_default()
                    .push(term.clone());
            }
        }

        matches
    }

    /// Explain scoring for a specific document
    pub fn explain(&self, query: &str, doc_id: &str) -> Option<ScoreExplanation> {
        let doc_info = self.docs.get(doc_id)?;
        let query_terms = self.tokenize_text(query);

        let doc_length = doc_info.length as f64;
        let k1 = self.options.k1;
        let b = self.options.b;
        let avgdl = self.avg_doc_length;

        let mut total_score = 0.0;
        let mut term_scores = Vec::new();

        for term in &query_terms {
            let idf = self.idf(term);
            let df = self.terms.get(term).map(|t| t.df).unwrap_or(0);
            let tf = self
                .terms
                .get(term)
                .and_then(|t| t.postings.get(doc_id))
                .copied()
                .unwrap_or(0);

            let tf_f64 = tf as f64;
            let tf_component = if tf > 0 {
                let numerator = tf_f64 * (k1 + 1.0);
                let denominator = tf_f64 + k1 * (1.0 - b + b * doc_length / avgdl);
                numerator / denominator
            } else {
                0.0
            };

            let score = idf * tf_component;
            total_score += score;

            term_scores.push(TermScoreDetail {
                term: term.clone(),
                tf,
                df,
                idf,
                tf_component,
                score,
            });
        }

        Some(ScoreExplanation {
            id: doc_id.to_string(),
            total_score,
            term_scores,
        })
    }

    /// Get all indexed terms with their document frequencies
    pub fn terms(&self) -> Vec<(String, usize)> {
        let mut terms: Vec<_> = self
            .terms
            .iter()
            .map(|(t, info)| (t.clone(), info.df))
            .collect();
        terms.sort_by(|a, b| b.1.cmp(&a.1)); // Sort by df descending
        terms
    }

    /// Find similar documents using term overlap
    pub fn similar(&self, doc_id: &str, top_k: usize) -> Vec<SearchResult> {
        let doc_terms: Vec<String> = self
            .terms
            .iter()
            .filter(|(_, info)| info.postings.contains_key(doc_id))
            .map(|(term, _)| term.clone())
            .collect();

        if doc_terms.is_empty() {
            return Vec::new();
        }

        // Score all other documents using the source doc's terms as query
        let mut results: Vec<SearchResult> = self
            .docs
            .keys()
            .filter(|id| *id != doc_id)
            .map(|id| {
                let score = self.score_doc(id, &doc_terms);
                let matches = self.get_matches(id, &doc_terms);
                let doc = self.docs.get(id).and_then(|d| d.source.clone());

                SearchResult {
                    id: id.clone(),
                    score,
                    matches,
                    doc,
                }
            })
            .filter(|r| r.score > 0.0)
            .collect();

        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        results.truncate(top_k);
        results
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_build_index_simple() {
        let docs = vec![
            json!("hello world"),
            json!("hello there"),
            json!("goodbye world"),
        ];

        let index = Bm25Index::build(&docs, IndexOptions::default());

        assert_eq!(index.doc_count, 3);
        assert!(index.terms.contains_key("hello"));
        assert!(index.terms.contains_key("world"));
        assert_eq!(index.terms.get("hello").unwrap().df, 2);
        assert_eq!(index.terms.get("world").unwrap().df, 2);
    }

    #[test]
    fn test_build_index_with_fields() {
        let docs = vec![
            json!({"name": "create_cluster", "description": "Create a new cluster"}),
            json!({"name": "delete_cluster", "description": "Delete an existing cluster"}),
            json!({"name": "list_backups", "description": "List all backups"}),
        ];

        let options = IndexOptions {
            fields: vec!["name".to_string(), "description".to_string()],
            id_field: Some("name".to_string()),
            ..Default::default()
        };

        let index = Bm25Index::build(&docs, options);

        assert_eq!(index.doc_count, 3);
        assert!(index.docs.contains_key("create_cluster"));
        assert!(index.docs.contains_key("delete_cluster"));
        assert!(index.terms.contains_key("cluster"));
        assert_eq!(index.terms.get("cluster").unwrap().df, 2);
    }

    #[test]
    fn test_search_basic() {
        let docs = vec![
            json!({"name": "create_cluster", "description": "Create a new Redis cluster"}),
            json!({"name": "delete_cluster", "description": "Delete an existing cluster"}),
            json!({"name": "create_backup", "description": "Create a backup of data"}),
        ];

        let options = IndexOptions {
            fields: vec!["name".to_string(), "description".to_string()],
            id_field: Some("name".to_string()),
            ..Default::default()
        };

        let index = Bm25Index::build(&docs, options);
        let results = index.search("cluster", 10);

        assert_eq!(results.len(), 2);
        // Both cluster docs should be returned
        let ids: Vec<_> = results.iter().map(|r| r.id.as_str()).collect();
        assert!(ids.contains(&"create_cluster"));
        assert!(ids.contains(&"delete_cluster"));
    }

    #[test]
    fn test_search_ranking() {
        let docs = vec![
            json!({"name": "cluster_manager", "description": "Manage cluster operations"}),
            json!({"name": "backup_tool", "description": "Backup tool for cluster data"}),
            json!({"name": "monitor", "description": "Monitor system health"}),
        ];

        let options = IndexOptions {
            fields: vec!["name".to_string(), "description".to_string()],
            id_field: Some("name".to_string()),
            ..Default::default()
        };

        let index = Bm25Index::build(&docs, options);
        let results = index.search("cluster", 10);

        // cluster_manager should rank higher (has "cluster" in both name and description)
        assert!(!results.is_empty());
        assert_eq!(results[0].id, "cluster_manager");
    }

    #[test]
    fn test_search_multi_term() {
        let docs = vec![
            json!({"name": "create_backup", "description": "Create a backup in a region"}),
            json!({"name": "restore_backup", "description": "Restore from backup"}),
            json!({"name": "list_regions", "description": "List available regions"}),
        ];

        let options = IndexOptions {
            fields: vec!["name".to_string(), "description".to_string()],
            id_field: Some("name".to_string()),
            ..Default::default()
        };

        let index = Bm25Index::build(&docs, options);
        let results = index.search("backup region", 10);

        // create_backup should rank highest (has both terms)
        assert!(!results.is_empty());
        assert_eq!(results[0].id, "create_backup");
    }

    #[test]
    fn test_explain() {
        let docs = vec![json!({"name": "test", "description": "test document with terms"})];

        let options = IndexOptions {
            fields: vec!["name".to_string(), "description".to_string()],
            id_field: Some("name".to_string()),
            ..Default::default()
        };

        let index = Bm25Index::build(&docs, options);
        let explanation = index.explain("test", "test").unwrap();

        assert_eq!(explanation.id, "test");
        assert!(explanation.total_score > 0.0);
        assert!(!explanation.term_scores.is_empty());
    }

    #[test]
    fn test_similar() {
        let docs = vec![
            json!({"name": "create_cluster", "description": "Create a new kubernetes cluster"}),
            json!({"name": "delete_cluster", "description": "Delete an existing kubernetes cluster"}),
            json!({"name": "upload_file", "description": "Upload a file to storage"}),
        ];

        let options = IndexOptions {
            fields: vec!["name".to_string(), "description".to_string()],
            id_field: Some("name".to_string()),
            ..Default::default()
        };

        let index = Bm25Index::build(&docs, options);
        let similar = index.similar("create_cluster", 10);

        // delete_cluster should be most similar (shares "cluster" and "kubernetes")
        assert!(!similar.is_empty());
        assert_eq!(similar[0].id, "delete_cluster");
    }

    #[test]
    fn test_stopwords() {
        let docs = vec![json!("the quick brown fox"), json!("the lazy dog")];

        let options = IndexOptions {
            stopwords: vec!["the".to_string()],
            ..Default::default()
        };

        let index = Bm25Index::build(&docs, options);

        assert!(!index.terms.contains_key("the"));
        assert!(index.terms.contains_key("quick"));
    }

    #[test]
    fn test_case_insensitive() {
        let docs = vec![json!("Hello World"), json!("HELLO THERE")];

        let index = Bm25Index::build(&docs, IndexOptions::default());
        let results = index.search("hello", 10);

        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_json_serialization() {
        let docs = vec![json!({"name": "test", "description": "test doc"})];

        let options = IndexOptions {
            fields: vec!["name".to_string()],
            id_field: Some("name".to_string()),
            ..Default::default()
        };

        let index = Bm25Index::build(&docs, options);

        // Should serialize to JSON without error
        let json = serde_json::to_string(&index).unwrap();
        assert!(json.contains("jpx:bm25_index"));

        // Should deserialize back
        let restored: Bm25Index = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.doc_count, 1);
    }

    #[test]
    fn test_terms_list() {
        let docs = vec![
            json!("hello hello world"),
            json!("hello there"),
            json!("goodbye world"),
        ];

        let index = Bm25Index::build(&docs, IndexOptions::default());
        let terms = index.terms();

        // Should be sorted by df descending
        assert!(!terms.is_empty());
        // "hello" appears in 2 docs, "world" in 2 docs
        assert!(terms[0].1 >= terms.last().unwrap().1);
    }

    #[test]
    fn test_empty_index_search() {
        let index = Bm25Index::new(IndexOptions::default());
        let results = index.search("anything", 10);
        assert!(results.is_empty());
    }

    #[test]
    fn test_empty_query_search() {
        let docs = vec![json!("hello world"), json!("goodbye world")];
        let index = Bm25Index::build(&docs, IndexOptions::default());
        let results = index.search("", 10);
        assert!(results.is_empty());
    }

    #[test]
    fn test_single_document_index() {
        let docs = vec![json!("the rust programming language")];
        let index = Bm25Index::build(&docs, IndexOptions::default());

        assert_eq!(index.doc_count, 1);

        let results = index.search("rust", 10);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, "0");
        assert!(results[0].score > 0.0);
    }

    #[test]
    fn test_stem_simple_plural_s() {
        assert_eq!(stem_simple("databases"), "database");
    }

    #[test]
    fn test_stem_simple_plural_ies() {
        assert_eq!(stem_simple("queries"), "query");
    }

    #[test]
    fn test_stem_simple_plural_xes() {
        assert_eq!(stem_simple("boxes"), "box");
    }

    #[test]
    fn test_stem_simple_short_word() {
        assert_eq!(stem_simple("is"), "is");
    }

    #[test]
    fn test_stem_simple_no_change() {
        assert_eq!(stem_simple("data"), "data");
    }

    #[test]
    fn test_idf_zero_for_unknown_term() {
        let docs = vec![json!("hello world"), json!("goodbye world")];
        let index = Bm25Index::build(&docs, IndexOptions::default());
        let idf = index.idf("nonexistent_term");
        assert_eq!(idf, 0.0);
    }

    #[test]
    fn test_similar_nonexistent_doc() {
        let docs = vec![
            json!({"name": "alpha", "description": "first document about rust"}),
            json!({"name": "beta", "description": "second document about python"}),
        ];

        let options = IndexOptions {
            fields: vec!["name".to_string(), "description".to_string()],
            id_field: Some("name".to_string()),
            ..Default::default()
        };

        let index = Bm25Index::build(&docs, options);
        let results = index.similar("nonexistent", 5);
        assert!(results.is_empty());
    }
}