hermes-core 1.4.20

Core async search engine library with WASM support
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
//! Comprehensive tests for WAND scoring and document retrieval
//!
//! Tests cover:
//! - Document retrievability (all matching docs found)
//! - Score correctness (BM25 scores match expected values)
//! - No missed documents (exhaustive verification)
//! - Multi-segment scenarios
//! - Edge cases (empty results, single term, many terms)

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use crate::directories::RamDirectory;
    use crate::dsl::{Document, Field, SchemaBuilder};
    use crate::index::{Index, IndexConfig, IndexWriter};
    use crate::query::{bm25_idf, bm25_score};

    /// Helper to create a standard test schema with content field
    fn create_schema() -> (crate::dsl::Schema, Field) {
        let mut schema_builder = SchemaBuilder::default();
        let content = schema_builder.add_text_field("content", true, true);
        (schema_builder.build(), content)
    }

    /// Generate synthetic documents with controlled term distributions
    ///
    /// Returns (documents as text content, expected doc_ids for each term)
    fn generate_test_documents(
        num_docs: usize,
        terms: &[&str],
    ) -> (Vec<String>, std::collections::HashMap<String, HashSet<u32>>) {
        let mut docs = Vec::with_capacity(num_docs);
        let mut term_to_docs: std::collections::HashMap<String, HashSet<u32>> =
            std::collections::HashMap::new();

        for term in terms {
            term_to_docs.insert(term.to_string(), HashSet::new());
        }

        for i in 0..num_docs {
            let mut content_parts = Vec::new();

            for (term_idx, term) in terms.iter().enumerate() {
                // Document i contains term j if (i % (j+2)) == 0
                // This creates overlapping but predictable distributions
                if i % (term_idx + 2) == 0 {
                    // Add term multiple times based on position for varying TF
                    let tf = 1 + (i % 3);
                    for _ in 0..tf {
                        content_parts.push(*term);
                    }
                    term_to_docs.get_mut(*term).unwrap().insert(i as u32);
                }
            }

            // Always add some filler content
            content_parts.push("filler");
            content_parts.push("content");

            docs.push(content_parts.join(" "));
        }

        (docs, term_to_docs)
    }

    /// Compute expected BM25 score for a document
    #[allow(dead_code)]
    fn compute_expected_bm25(tf: f32, doc_freq: f32, total_docs: f32, avg_field_len: f32) -> f32 {
        let idf = bm25_idf(doc_freq, total_docs);
        bm25_score(tf, idf, tf, avg_field_len)
    }

    // ==================== Basic Retrievability Tests ====================

    #[tokio::test]
    async fn test_single_term_all_docs_retrieved() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Doc 0: rust programming (tf=1)
        let mut doc = Document::new();
        doc.add_text(content, "rust programming");
        writer.add_document(doc).unwrap();

        // Doc 1: python programming (no rust)
        let mut doc = Document::new();
        doc.add_text(content, "python programming");
        writer.add_document(doc).unwrap();

        // Doc 2: rust rust rust (tf=3)
        let mut doc = Document::new();
        doc.add_text(content, "rust rust rust");
        writer.add_document(doc).unwrap();

        // Doc 3: java code (no rust)
        let mut doc = Document::new();
        doc.add_text(content, "java code");
        writer.add_document(doc).unwrap();

        // Doc 4: rust systems (tf=1)
        let mut doc = Document::new();
        doc.add_text(content, "rust systems");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();
        let results = index.query("content:rust", 10).await.unwrap();

        let found_ids: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();
        let expected_ids: HashSet<u32> = [0, 2, 4].into_iter().collect();

        assert_eq!(
            found_ids, expected_ids,
            "Expected docs {:?}, found {:?}",
            expected_ids, found_ids
        );

        // Doc 2 should have highest score (tf=3)
        assert_eq!(
            results.hits[0].address.doc_id, 2,
            "Doc with highest TF should be first"
        );
    }

    #[tokio::test]
    async fn test_or_query_all_matching_docs_retrieved() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Doc 0: rust programming
        let mut doc = Document::new();
        doc.add_text(content, "rust programming");
        writer.add_document(doc).unwrap();

        // Doc 1: python programming
        let mut doc = Document::new();
        doc.add_text(content, "python programming");
        writer.add_document(doc).unwrap();

        // Doc 2: rust python (both)
        let mut doc = Document::new();
        doc.add_text(content, "rust python");
        writer.add_document(doc).unwrap();

        // Doc 3: java code (neither)
        let mut doc = Document::new();
        doc.add_text(content, "java code");
        writer.add_document(doc).unwrap();

        // Doc 4: python only
        let mut doc = Document::new();
        doc.add_text(content, "python only");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // OR query: rust OR python - should find docs 0, 1, 2, 4
        let results = index
            .query("content:rust OR content:python", 10)
            .await
            .unwrap();

        let found_ids: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();
        let expected_ids: HashSet<u32> = [0, 1, 2, 4].into_iter().collect();

        assert_eq!(
            found_ids, expected_ids,
            "OR query should find all docs with either term. Expected {:?}, found {:?}",
            expected_ids, found_ids
        );

        // Doc 2 matches both terms, should have highest score
        assert_eq!(
            results.hits[0].address.doc_id, 2,
            "Doc matching both terms should be first"
        );
    }

    #[tokio::test]
    async fn test_synthetic_documents_exhaustive() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Use generate_test_documents for predictable distribution
        let terms = ["alpha", "beta", "gamma", "delta"];
        let (docs, expected_term_docs) = generate_test_documents(50, &terms);

        for doc_content in &docs {
            let mut doc = Document::new();
            doc.add_text(content, doc_content.clone());
            writer.add_document(doc).unwrap();
        }

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // Verify each term finds exactly the expected documents
        for term in &terms {
            let expected = expected_term_docs.get(*term).unwrap();
            let results = index
                .query(&format!("content:{}", term), 100)
                .await
                .unwrap();

            let found: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();

            let missed: Vec<_> = expected.difference(&found).collect();
            let extra: Vec<_> = found.difference(expected).collect();

            assert!(
                missed.is_empty() && extra.is_empty(),
                "Term '{}': missed {:?}, extra {:?}",
                term,
                missed,
                extra
            );
        }
    }

    #[tokio::test]
    async fn test_no_missed_documents_large_corpus() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Create 100 documents with predictable term distribution
        let mut expected_alpha = HashSet::new();
        let mut expected_beta = HashSet::new();

        for i in 0..100u32 {
            let mut doc = Document::new();
            let mut terms = vec!["filler"];

            // Alpha appears in docs where i % 2 == 0
            if i % 2 == 0 {
                terms.push("alpha");
                expected_alpha.insert(i);
            }

            // Beta appears in docs where i % 3 == 0
            if i % 3 == 0 {
                terms.push("beta");
                expected_beta.insert(i);
            }

            doc.add_text(content, terms.join(" "));
            writer.add_document(doc).unwrap();
        }

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // Test alpha - should find 50 docs
        let results = index.query("content:alpha", 200).await.unwrap();
        let found: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();

        let missed: Vec<_> = expected_alpha.difference(&found).collect();
        assert!(
            missed.is_empty(),
            "Missed {} docs for 'alpha': {:?}",
            missed.len(),
            missed
        );

        let extra: Vec<_> = found.difference(&expected_alpha).collect();
        assert!(
            extra.is_empty(),
            "Extra {} docs for 'alpha': {:?}",
            extra.len(),
            extra
        );

        // Test beta - should find 34 docs (0, 3, 6, ..., 99)
        let results = index.query("content:beta", 200).await.unwrap();
        let found: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();

        let missed: Vec<_> = expected_beta.difference(&found).collect();
        assert!(
            missed.is_empty(),
            "Missed {} docs for 'beta': {:?}",
            missed.len(),
            missed
        );
    }

    // ==================== Score Correctness Tests ====================

    #[tokio::test]
    async fn test_bm25_score_ordering_by_tf() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Create docs with varying TF for "test"
        let tfs = [1, 2, 3, 5, 10];
        for tf in tfs {
            let mut doc = Document::new();
            let text = vec!["test"; tf].join(" ");
            doc.add_text(content, text);
            writer.add_document(doc).unwrap();
        }

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();
        let results = index.query("content:test", 10).await.unwrap();

        assert_eq!(results.hits.len(), 5);

        // Verify scores are in descending order
        for i in 1..results.hits.len() {
            assert!(
                results.hits[i - 1].score >= results.hits[i].score,
                "Scores not in descending order at position {}: {} < {}",
                i,
                results.hits[i - 1].score,
                results.hits[i].score
            );
        }

        // Doc 4 (tf=10) should be first
        assert_eq!(
            results.hits[0].address.doc_id, 4,
            "Highest TF doc should be first"
        );
    }

    #[tokio::test]
    async fn test_multi_term_score_accumulation() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Doc 0: only rust
        let mut doc = Document::new();
        doc.add_text(content, "rust");
        writer.add_document(doc).unwrap();

        // Doc 1: only systems
        let mut doc = Document::new();
        doc.add_text(content, "systems");
        writer.add_document(doc).unwrap();

        // Doc 2: both rust and systems (tf=1 each)
        let mut doc = Document::new();
        doc.add_text(content, "rust systems");
        writer.add_document(doc).unwrap();

        // Doc 3: both with higher TF (tf=2 each)
        let mut doc = Document::new();
        doc.add_text(content, "rust rust systems systems");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();
        let results = index
            .query("content:rust OR content:systems", 10)
            .await
            .unwrap();

        // Doc 3 matches both with tf=2 each, should have highest score
        assert_eq!(
            results.hits[0].address.doc_id, 3,
            "Doc with multiple term matches (higher TF) should be first"
        );

        // Doc 2 matches both with tf=1 each, should be second
        assert_eq!(
            results.hits[1].address.doc_id, 2,
            "Doc matching both terms once should be second"
        );
    }

    // ==================== Multi-Segment Tests ====================

    #[tokio::test]
    async fn test_multi_segment_retrieval() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig {
            max_indexing_memory_bytes: 1024, // Small to trigger frequent flushes
            ..Default::default()
        };

        // Single writer, multiple commits to create segments
        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        for batch in 0..3 {
            for i in 0..5 {
                let doc_id = batch * 5 + i;
                let mut doc = Document::new();

                if doc_id % 2 == 0 {
                    doc.add_text(content, format!("searchterm doc{}", doc_id));
                } else {
                    doc.add_text(content, format!("otherword doc{}", doc_id));
                }

                writer.add_document(doc).unwrap();
            }
            writer.commit().await.unwrap();
        }

        let index = Index::open(dir, config).await.unwrap();
        let seg_count = index.segment_readers().await.unwrap().len();
        assert!(
            seg_count >= 2,
            "Should have multiple segments, got {}",
            seg_count
        );

        let results = index.query("content:searchterm", 50).await.unwrap();

        // Should find 8 docs with "searchterm" (docs 0,2,4,6,8,10,12,14 by creation order)
        assert_eq!(
            results.hits.len(),
            8,
            "Should find 8 docs with searchterm across segments"
        );

        // All scores should be positive
        for hit in &results.hits {
            assert!(hit.score > 0.0, "All hits should have positive scores");
        }
    }

    #[tokio::test]
    async fn test_multi_segment_score_consistency() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        // Single writer, multiple commits
        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        for _segment in 0..2 {
            let mut doc = Document::new();
            doc.add_text(content, "identical content here");
            writer.add_document(doc).unwrap();
            writer.commit().await.unwrap();
        }

        let index = Index::open(dir, config).await.unwrap();
        let results = index.query("content:identical", 10).await.unwrap();

        assert_eq!(results.hits.len(), 2, "Should find docs in both segments");

        let score_diff = (results.hits[0].score - results.hits[1].score).abs();
        assert!(
            score_diff < 0.1,
            "Identical docs should have similar scores, got diff={}",
            score_diff
        );
    }

    // ==================== Edge Cases ====================

    #[tokio::test]
    async fn test_empty_results() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        let mut doc = Document::new();
        doc.add_text(content, "hello world");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();
        let results = index.query("content:nonexistent", 10).await.unwrap();

        assert_eq!(
            results.hits.len(),
            0,
            "Should return empty for non-matching term"
        );
    }

    #[tokio::test]
    async fn test_limit_respected() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        for _i in 0..50 {
            let mut doc = Document::new();
            doc.add_text(content, "common term here");
            writer.add_document(doc).unwrap();
        }

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // Request only 5 results
        let results = index.query("content:common", 5).await.unwrap();
        assert_eq!(results.hits.len(), 5, "Should respect limit of 5");

        // All returned should have valid scores
        for hit in &results.hits {
            assert!(hit.score > 0.0, "Score should be positive");
        }
    }

    #[tokio::test]
    async fn test_many_terms_or_query() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        let terms = [
            "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta",
        ];

        // Create docs with one term each
        for (i, term) in terms.iter().enumerate() {
            let mut doc = Document::new();
            doc.add_text(content, format!("{} content", term));
            writer.add_document(doc).unwrap();

            // Doc IDs will be 0-7
            assert_eq!(i, i); // placeholder
        }

        // Add one doc that matches all terms (doc_id = 8)
        let mut doc = Document::new();
        doc.add_text(content, terms.join(" "));
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // OR query with all terms
        let query_str = terms
            .iter()
            .map(|t| format!("content:{}", t))
            .collect::<Vec<_>>()
            .join(" OR ");

        let results = index.query(&query_str, 20).await.unwrap();

        // Should find all 9 documents
        assert_eq!(results.hits.len(), 9, "Should find all 9 docs");

        // Doc 8 (matches all terms) should be first
        assert_eq!(
            results.hits[0].address.doc_id, 8,
            "Doc matching all terms should have highest score"
        );
    }

    #[tokio::test]
    async fn test_high_tf_document() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Doc 0: very high TF
        let mut doc = Document::new();
        doc.add_text(content, vec!["repeat"; 100].join(" "));
        writer.add_document(doc).unwrap();

        // Doc 1: normal TF
        let mut doc = Document::new();
        doc.add_text(content, "repeat once");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();
        let results = index.query("content:repeat", 10).await.unwrap();

        assert_eq!(results.hits.len(), 2);
        // High TF doc should have higher score
        assert_eq!(
            results.hits[0].address.doc_id, 0,
            "High TF doc should be first"
        );
        assert!(
            results.hits[0].score > results.hits[1].score,
            "High TF should yield higher score"
        );
    }

    // ==================== Regression Tests ====================

    #[tokio::test]
    async fn test_no_duplicate_doc_ids_in_results() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Create docs with overlapping terms
        for i in 0..20 {
            let mut doc = Document::new();
            doc.add_text(content, format!("term1 term2 term3 doc{}", i));
            writer.add_document(doc).unwrap();
        }

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();
        let results = index
            .query("content:term1 OR content:term2 OR content:term3", 50)
            .await
            .unwrap();

        // Check for duplicates
        let mut seen = HashSet::new();
        for hit in &results.hits {
            assert!(
                seen.insert(hit.address.doc_id),
                "Duplicate doc_id {} in results",
                hit.address.doc_id
            );
        }
    }

    #[tokio::test]
    async fn test_scores_are_positive_and_finite() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        for _i in 0..10 {
            let mut doc = Document::new();
            doc.add_text(content, "test content here");
            writer.add_document(doc).unwrap();
        }

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();
        let results = index.query("content:test", 20).await.unwrap();

        for hit in &results.hits {
            assert!(
                hit.score > 0.0,
                "Score should be positive, got {}",
                hit.score
            );
            assert!(
                hit.score.is_finite(),
                "Score should be finite, got {}",
                hit.score
            );
            assert!(!hit.score.is_nan(), "Score should not be NaN");
        }
    }

    #[tokio::test]
    async fn test_single_doc_single_term() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        let mut doc = Document::new();
        doc.add_text(content, "unique");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();
        let results = index.query("content:unique", 10).await.unwrap();

        assert_eq!(results.hits.len(), 1);
        assert_eq!(results.hits[0].address.doc_id, 0);
        assert!(results.hits[0].score > 0.0);
    }

    #[tokio::test]
    async fn test_idf_impact_on_scoring() {
        let (schema, content) = create_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Create 10 docs: all have "common", only doc 0 has "rare"
        for i in 0..10 {
            let mut doc = Document::new();
            if i == 0 {
                doc.add_text(content, "common rare");
            } else {
                doc.add_text(content, "common");
            }
            writer.add_document(doc).unwrap();
        }

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // Search for "rare" (high IDF) - only doc 0 matches
        let rare_results = index.query("content:rare", 10).await.unwrap();
        assert_eq!(rare_results.hits.len(), 1);
        let rare_score = rare_results.hits[0].score;

        // Search for "common" (low IDF) - all 10 docs match
        let common_results = index.query("content:common", 10).await.unwrap();
        assert_eq!(common_results.hits.len(), 10);
        let common_score = common_results.hits[0].score;

        // Rare term should have higher IDF and thus higher score
        assert!(
            rare_score > common_score,
            "Rare term (IDF={:.4}) should score higher than common (IDF={:.4})",
            rare_score,
            common_score
        );
    }

    // ==================== Multi-Field BM25F Tests ====================

    /// Create a multi-field schema with title, body, and tags
    fn create_multifield_schema() -> (crate::dsl::Schema, Field, Field, Field) {
        let mut schema_builder = SchemaBuilder::default();
        let title = schema_builder.add_text_field("title", true, true);
        let body = schema_builder.add_text_field("body", true, true);
        let tags = schema_builder.add_text_field("tags", true, true);
        (schema_builder.build(), title, body, tags)
    }

    #[tokio::test]
    async fn test_multifield_basic_retrieval() {
        let (schema, title, body, tags) = create_multifield_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Doc 0: term in title only
        let mut doc = Document::new();
        doc.add_text(title, "rust programming guide");
        doc.add_text(body, "this is about software development");
        doc.add_text(tags, "tutorial beginner");
        writer.add_document(doc).unwrap();

        // Doc 1: term in body only
        let mut doc = Document::new();
        doc.add_text(title, "software guide");
        doc.add_text(body, "learn rust programming here");
        doc.add_text(tags, "tutorial");
        writer.add_document(doc).unwrap();

        // Doc 2: term in tags only
        let mut doc = Document::new();
        doc.add_text(title, "programming tutorial");
        doc.add_text(body, "general software development");
        doc.add_text(tags, "rust systems");
        writer.add_document(doc).unwrap();

        // Doc 3: term in all fields
        let mut doc = Document::new();
        doc.add_text(title, "rust mastery");
        doc.add_text(body, "advanced rust programming");
        doc.add_text(tags, "rust expert");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // Search in title field
        let results = index.query("title:rust", 10).await.unwrap();
        let found: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();
        assert!(found.contains(&0), "Doc 0 should match title:rust");
        assert!(found.contains(&3), "Doc 3 should match title:rust");
        assert!(!found.contains(&1), "Doc 1 should not match title:rust");
        assert!(!found.contains(&2), "Doc 2 should not match title:rust");

        // Search in body field
        let results = index.query("body:rust", 10).await.unwrap();
        let found: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();
        assert!(found.contains(&1), "Doc 1 should match body:rust");
        assert!(found.contains(&3), "Doc 3 should match body:rust");

        // Search in tags field
        let results = index.query("tags:rust", 10).await.unwrap();
        let found: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();
        assert!(found.contains(&2), "Doc 2 should match tags:rust");
        assert!(found.contains(&3), "Doc 3 should match tags:rust");
    }

    #[tokio::test]
    async fn test_multifield_or_across_fields() {
        let (schema, title, body, _tags) = create_multifield_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Doc 0: searchterm in title
        let mut doc = Document::new();
        doc.add_text(title, "searchterm in title");
        doc.add_text(body, "other content");
        writer.add_document(doc).unwrap();

        // Doc 1: searchterm in body
        let mut doc = Document::new();
        doc.add_text(title, "different title");
        doc.add_text(body, "searchterm in body");
        writer.add_document(doc).unwrap();

        // Doc 2: searchterm in both
        let mut doc = Document::new();
        doc.add_text(title, "searchterm title");
        doc.add_text(body, "searchterm body");
        writer.add_document(doc).unwrap();

        // Doc 3: no searchterm
        let mut doc = Document::new();
        doc.add_text(title, "unrelated");
        doc.add_text(body, "nothing here");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // OR across fields
        let results = index
            .query("title:searchterm OR body:searchterm", 10)
            .await
            .unwrap();

        assert_eq!(results.hits.len(), 3, "Should find 3 docs with searchterm");

        let found: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();
        assert!(found.contains(&0));
        assert!(found.contains(&1));
        assert!(found.contains(&2));
        assert!(!found.contains(&3));

        // Doc 2 (matches both fields) should have highest score
        assert_eq!(
            results.hits[0].address.doc_id, 2,
            "Doc matching both fields should score highest"
        );
    }

    #[tokio::test]
    async fn test_multifield_tf_accumulation() {
        let (schema, title, body, tags) = create_multifield_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Doc 0: low TF in one field
        let mut doc = Document::new();
        doc.add_text(title, "rust");
        doc.add_text(body, "programming");
        doc.add_text(tags, "code");
        writer.add_document(doc).unwrap();

        // Doc 1: high TF in one field
        let mut doc = Document::new();
        doc.add_text(title, "rust rust rust rust rust");
        doc.add_text(body, "programming");
        doc.add_text(tags, "code");
        writer.add_document(doc).unwrap();

        // Doc 2: medium TF spread across fields
        let mut doc = Document::new();
        doc.add_text(title, "rust rust");
        doc.add_text(body, "rust rust");
        doc.add_text(tags, "rust");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // Search for rust in title
        let results = index.query("title:rust", 10).await.unwrap();

        // Doc 1 (tf=5) should have higher score than Doc 0 (tf=1) and Doc 2 (tf=2)
        assert_eq!(
            results.hits[0].address.doc_id, 1,
            "Highest TF should score highest"
        );

        // Verify score ordering
        for i in 1..results.hits.len() {
            assert!(
                results.hits[i - 1].score >= results.hits[i].score,
                "Scores should be in descending order"
            );
        }
    }

    #[tokio::test]
    async fn test_multifield_different_field_lengths() {
        let (schema, title, body, _tags) = create_multifield_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Doc 0: short title with term, long body without
        let mut doc = Document::new();
        doc.add_text(title, "rust guide");
        doc.add_text(body, "this is a very long body with lots of words that dilute the term frequency and should result in lower BM25 scores for terms that appear here because length normalization penalizes longer documents");
        writer.add_document(doc).unwrap();

        // Doc 1: short title with term
        let mut doc = Document::new();
        doc.add_text(title, "rust");
        doc.add_text(body, "short body");
        writer.add_document(doc).unwrap();

        // Doc 2: long title with term
        let mut doc = Document::new();
        doc.add_text(
            title,
            "the comprehensive rust programming language tutorial guide for beginners",
        );
        doc.add_text(body, "content");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        let results = index.query("title:rust", 10).await.unwrap();

        // All 3 docs should be found
        assert_eq!(results.hits.len(), 3);

        // Scores should reflect length normalization
        // Shorter title with same TF should score higher
        for hit in &results.hits {
            assert!(hit.score > 0.0, "All scores should be positive");
            assert!(hit.score.is_finite(), "All scores should be finite");
        }
    }

    #[tokio::test]
    async fn test_multifield_cross_field_or_query() {
        let (schema, title, body, tags) = create_multifield_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Create docs with different terms in different fields
        // Doc 0: alpha in title, beta in body
        let mut doc = Document::new();
        doc.add_text(title, "alpha");
        doc.add_text(body, "beta");
        doc.add_text(tags, "gamma");
        writer.add_document(doc).unwrap();

        // Doc 1: beta in title, gamma in body
        let mut doc = Document::new();
        doc.add_text(title, "beta");
        doc.add_text(body, "gamma");
        doc.add_text(tags, "alpha");
        writer.add_document(doc).unwrap();

        // Doc 2: gamma in title, alpha in body
        let mut doc = Document::new();
        doc.add_text(title, "gamma");
        doc.add_text(body, "alpha");
        doc.add_text(tags, "beta");
        writer.add_document(doc).unwrap();

        // Doc 3: all terms in title
        let mut doc = Document::new();
        doc.add_text(title, "alpha beta gamma");
        doc.add_text(body, "other");
        doc.add_text(tags, "misc");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // Complex OR across multiple fields and terms
        let results = index
            .query("title:alpha OR body:beta OR tags:gamma", 10)
            .await
            .unwrap();

        // Should find docs 0 (title:alpha, body:beta), 1 (tags:gamma... wait, no)
        // Let me reconsider:
        // Doc 0: title:alpha YES, body:beta YES, tags:gamma YES -> 3 matches
        // Doc 1: title:alpha NO (beta), body:beta NO (gamma), tags:gamma NO (alpha) -> 0
        // Doc 2: title:alpha NO, body:beta NO, tags:gamma NO (beta) -> 0
        // Doc 3: title:alpha YES, body:beta NO, tags:gamma NO -> 1 match

        // Actually:
        // Doc 0: title has "alpha" -> YES
        // Doc 1: none match
        // Doc 2: none match
        // Doc 3: title has "alpha" -> YES

        let found: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();

        // Doc 0 matches title:alpha
        assert!(found.contains(&0), "Doc 0 should match (title:alpha)");
        // Doc 3 matches title:alpha
        assert!(found.contains(&3), "Doc 3 should match (title:alpha)");
    }

    #[tokio::test]
    async fn test_multifield_no_cross_contamination() {
        let (schema, title, body, _tags) = create_multifield_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Doc 0: "secret" only in body
        let mut doc = Document::new();
        doc.add_text(title, "public information");
        doc.add_text(body, "this contains secret data");
        writer.add_document(doc).unwrap();

        // Doc 1: "secret" only in title
        let mut doc = Document::new();
        doc.add_text(title, "secret document");
        doc.add_text(body, "public information here");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // Search title:secret should NOT find doc 0
        let results = index.query("title:secret", 10).await.unwrap();
        assert_eq!(results.hits.len(), 1, "Only one doc has secret in title");
        assert_eq!(
            results.hits[0].address.doc_id, 1,
            "Only doc 1 has secret in title"
        );

        // Search body:secret should NOT find doc 1
        let results = index.query("body:secret", 10).await.unwrap();
        assert_eq!(results.hits.len(), 1, "Only one doc has secret in body");
        assert_eq!(
            results.hits[0].address.doc_id, 0,
            "Only doc 0 has secret in body"
        );
    }

    #[tokio::test]
    async fn test_multifield_combined_scoring() {
        let (schema, title, body, tags) = create_multifield_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        // Doc 0: target term appears once in each field
        let mut doc = Document::new();
        doc.add_text(title, "rust");
        doc.add_text(body, "rust");
        doc.add_text(tags, "rust");
        writer.add_document(doc).unwrap();

        // Doc 1: target term appears multiple times in one field only
        let mut doc = Document::new();
        doc.add_text(title, "rust rust rust");
        doc.add_text(body, "other content");
        doc.add_text(tags, "misc");
        writer.add_document(doc).unwrap();

        // Doc 2: no target term
        let mut doc = Document::new();
        doc.add_text(title, "python");
        doc.add_text(body, "java");
        doc.add_text(tags, "go");
        writer.add_document(doc).unwrap();

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // OR query across all fields
        let results = index
            .query("title:rust OR body:rust OR tags:rust", 10)
            .await
            .unwrap();

        assert_eq!(results.hits.len(), 2, "Should find 2 docs with rust");

        // Both docs 0 and 1 should be found
        let found: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();
        assert!(found.contains(&0));
        assert!(found.contains(&1));
        assert!(!found.contains(&2));

        // Doc 0 appears in 3 fields, Doc 1 has higher TF in title
        // The relative ranking depends on BM25F weights
        // Just verify both are found and have positive scores
        for hit in &results.hits {
            assert!(hit.score > 0.0);
        }
    }

    #[tokio::test]
    async fn test_multifield_large_document_set() {
        let (schema, title, body, tags) = create_multifield_schema();
        let dir = RamDirectory::new();
        let config = IndexConfig::default();

        let writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
            .await
            .unwrap();

        let mut expected_title_match = HashSet::new();
        let mut expected_body_match = HashSet::new();
        let mut expected_any_match = HashSet::new();

        // Create 100 documents with predictable distribution
        for i in 0..100u32 {
            let mut doc = Document::new();

            // "target" in title for docs where i % 3 == 0
            if i % 3 == 0 {
                doc.add_text(title, format!("target document {}", i));
                expected_title_match.insert(i);
                expected_any_match.insert(i);
            } else {
                doc.add_text(title, format!("other document {}", i));
            }

            // "target" in body for docs where i % 5 == 0
            if i % 5 == 0 {
                doc.add_text(body, format!("contains target word {}", i));
                expected_body_match.insert(i);
                expected_any_match.insert(i);
            } else {
                doc.add_text(body, format!("regular content {}", i));
            }

            doc.add_text(tags, format!("tag{}", i % 10));
            writer.add_document(doc).unwrap();
        }

        writer.commit().await.unwrap();

        let index = Index::open(dir, config).await.unwrap();

        // Test title:target
        let results = index.query("title:target", 200).await.unwrap();
        let found: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();

        let missed: Vec<_> = expected_title_match.difference(&found).collect();
        assert!(
            missed.is_empty(),
            "Missed {} docs for title:target: {:?}",
            missed.len(),
            missed
        );

        // Test body:target
        let results = index.query("body:target", 200).await.unwrap();
        let found: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();

        let missed: Vec<_> = expected_body_match.difference(&found).collect();
        assert!(
            missed.is_empty(),
            "Missed {} docs for body:target: {:?}",
            missed.len(),
            missed
        );

        // Test OR across fields
        let results = index
            .query("title:target OR body:target", 200)
            .await
            .unwrap();
        let found: HashSet<u32> = results.hits.iter().map(|h| h.address.doc_id).collect();

        let missed: Vec<_> = expected_any_match.difference(&found).collect();
        assert!(
            missed.is_empty(),
            "Missed {} docs for OR query: {:?}",
            missed.len(),
            missed
        );

        // Verify no extra docs
        let extra: Vec<_> = found.difference(&expected_any_match).collect();
        assert!(
            extra.is_empty(),
            "Found {} extra docs for OR query: {:?}",
            extra.len(),
            extra
        );
    }
}