bearing 0.1.0-alpha.5

A Rust port of Apache Lucene
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
// SPDX-License-Identifier: Apache-2.0

//! Integration tests for BooleanQuery, cross-validated against Java Lucene 10.3.2.
//!
//! Uses the golden-docs corpus (15 documents from `testdata/golden-docs/`), indexed with
//! field name "contents" using `text_field` (DOCS_AND_FREQS_AND_POSITIONS, tokenized,
//! no term vectors). Files are indexed in sorted filename order for deterministic doc IDs.
//!
//! Expected results were obtained by indexing the same documents in the same order with
//! Java Lucene 10.3.2 (StandardAnalyzer, BM25Similarity default k1=1.2 b=0.75) and
//! running the same BooleanQuery with two MUST TermQuery clauses.

use std::fs;
use std::path::Path;
use std::sync::Arc;

use assertables::*;
use bearing::document::DocumentBuilder;
use bearing::index::config::IndexWriterConfig;
use bearing::index::directory_reader::DirectoryReader;
use bearing::index::field::text;
use bearing::index::writer::IndexWriter;
use bearing::search::*;
use bearing::store::{MemoryDirectory, SharedDirectory};

/// Indexes golden-docs into an in-memory directory, returning the directory and reader.
/// Files are indexed in sorted filename order to match the Java indexing order.
fn build_golden_docs_index() -> (SharedDirectory, DirectoryReader) {
    let config = IndexWriterConfig::default().num_threads(1);
    let directory: SharedDirectory = MemoryDirectory::create();
    let writer = IndexWriter::new(config, Arc::clone(&directory));

    let docs_dir = Path::new("testdata/golden-docs");
    let mut paths: Vec<_> = fs::read_dir(docs_dir)
        .unwrap()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().is_some_and(|ext| ext == "txt"))
        .map(|e| e.path())
        .collect();
    paths.sort();

    for path in &paths {
        let contents = fs::read_to_string(path).unwrap();
        let doc = DocumentBuilder::new()
            .add_field(text("contents").value(contents.as_str()))
            .build();
        writer.add_document(doc).unwrap();
    }

    writer.commit().unwrap();

    let reader = DirectoryReader::open(&*directory).unwrap();
    (directory, reader)
}

/// Helper to build a two-MUST boolean query.
fn must_must_query(field: &str, term1: &[u8], term2: &[u8]) -> BooleanQuery {
    let mut builder = BooleanQuery::builder();
    builder.add_query(Box::new(TermQuery::new(field, term1)), Occur::Must);
    builder.add_query(Box::new(TermQuery::new(field, term2)), Occur::Must);
    builder.build()
}

// -------------------------------------------------------------------------
// Query 1: +algorithms +data → 4 hits
// Java results:
//   doc=11 score=0.9744643
//   doc=0  score=0.8020670
//   doc=14 score=0.8000477
//   doc=3  score=0.6710463
// -------------------------------------------------------------------------

#[test]
fn test_boolean_must_algorithms_data() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = must_must_query("contents", b"algorithms", b"data");

    let top_docs = searcher.search(&query, 10).unwrap();

    assert_eq!(top_docs.total_hits.value, 4);
    assert_eq!(top_docs.score_docs.len(), 4);

    assert_eq!(top_docs.score_docs[0].doc, 11);
    assert_in_delta!(top_docs.score_docs[0].score, 0.9744643_f32, 1e-5);

    assert_eq!(top_docs.score_docs[1].doc, 0);
    assert_in_delta!(top_docs.score_docs[1].score, 0.802067_f32, 1e-5);

    assert_eq!(top_docs.score_docs[2].doc, 14);
    assert_in_delta!(top_docs.score_docs[2].score, 0.8000477_f32, 1e-5);

    assert_eq!(top_docs.score_docs[3].doc, 3);
    assert_in_delta!(top_docs.score_docs[3].score, 0.6710463_f32, 1e-5);
}

// -------------------------------------------------------------------------
// Query 2: +distributed +systems → 6 hits
// Java results:
//   doc=13 score=0.6466637
//   doc=2  score=0.5788049
//   doc=7  score=0.5369343
//   doc=8  score=0.5369343
//   doc=6  score=0.5074845
//   doc=0  score=0.4957356
// -------------------------------------------------------------------------

#[test]
fn test_boolean_must_distributed_systems() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = must_must_query("contents", b"distributed", b"systems");

    let top_docs = searcher.search(&query, 10).unwrap();

    assert_eq!(top_docs.total_hits.value, 6);
    assert_eq!(top_docs.score_docs.len(), 6);

    assert_eq!(top_docs.score_docs[0].doc, 13);
    assert_in_delta!(top_docs.score_docs[0].score, 0.6466637_f32, 1e-5);

    assert_eq!(top_docs.score_docs[1].doc, 2);
    assert_in_delta!(top_docs.score_docs[1].score, 0.5788049_f32, 1e-5);

    assert_eq!(top_docs.score_docs[2].doc, 7);
    assert_in_delta!(top_docs.score_docs[2].score, 0.5369343_f32, 1e-5);

    assert_eq!(top_docs.score_docs[3].doc, 8);
    assert_in_delta!(top_docs.score_docs[3].score, 0.5369343_f32, 1e-5);

    assert_eq!(top_docs.score_docs[4].doc, 6);
    assert_in_delta!(top_docs.score_docs[4].score, 0.5074845_f32, 1e-5);

    assert_eq!(top_docs.score_docs[5].doc, 0);
    assert_in_delta!(top_docs.score_docs[5].score, 0.4957356_f32, 1e-5);
}

// -------------------------------------------------------------------------
// Query 3: +memory +performance → 1 hit
// Java results:
//   doc=0 score=1.1177642
// -------------------------------------------------------------------------

#[test]
fn test_boolean_must_memory_performance() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = must_must_query("contents", b"memory", b"performance");

    let top_docs = searcher.search(&query, 10).unwrap();

    assert_eq!(top_docs.total_hits.value, 1);
    assert_eq!(top_docs.score_docs.len(), 1);

    assert_eq!(top_docs.score_docs[0].doc, 0);
    assert_in_delta!(top_docs.score_docs[0].score, 1.1177642_f32, 1e-5);
}

// -------------------------------------------------------------------------
// Query 4: +quantum +computing → 1 hit
// Java results:
//   doc=9 score=1.7729266
// -------------------------------------------------------------------------

#[test]
fn test_boolean_must_quantum_computing() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = must_must_query("contents", b"quantum", b"computing");

    let top_docs = searcher.search(&query, 10).unwrap();

    assert_eq!(top_docs.total_hits.value, 1);
    assert_eq!(top_docs.score_docs.len(), 1);

    assert_eq!(top_docs.score_docs[0].doc, 9);
    assert_in_delta!(top_docs.score_docs[0].score, 1.7729266_f32, 1e-5);
}

// -------------------------------------------------------------------------
// Query 5: +nonexistent1 +nonexistent2 → 0 hits
// -------------------------------------------------------------------------

#[test]
fn test_boolean_must_nonexistent_terms() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = must_must_query("contents", b"nonexistent1", b"nonexistent2");

    let top_docs = searcher.search(&query, 10).unwrap();

    assert_eq!(top_docs.total_hits.value, 0);
    assert_is_empty!(top_docs.score_docs);
}

// -------------------------------------------------------------------------
// SHOULD queries — pure disjunction
// Cross-validated against Java Lucene 10.3.2. Java index built with sorted
// filenames, StandardAnalyzer(EMPTY_SET), text_field("contents").
// Java queries via QueryParser default operator (OR = SHOULD).
// -------------------------------------------------------------------------

/// Helper to build a two-SHOULD boolean query.
fn should_should_query(field: &str, term1: &[u8], term2: &[u8]) -> BooleanQuery {
    let mut builder = BooleanQuery::builder();
    builder.add_query(Box::new(TermQuery::new(field, term1)), Occur::Should);
    builder.add_query(Box::new(TermQuery::new(field, term2)), Occur::Should);
    builder.build()
}

// -------------------------------------------------------------------------
// SHOULD Query 1: algorithms data → 10 hits
// Java results:
//   doc=11  score=0.9744643
//   doc=0   score=0.8020670
//   doc=14  score=0.8000477
//   doc=3   score=0.6710463
//   doc=5   score=0.2621497
//   doc=1   score=0.2506270
//   doc=10  score=0.2121821
//   doc=12  score=0.1924969
//   doc=6   score=0.1707188
//   doc=9   score=0.1577401
// -------------------------------------------------------------------------

#[test]
fn test_boolean_should_algorithms_data() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = should_should_query("contents", b"algorithms", b"data");

    let top_docs = searcher.search(&query, 15).unwrap();

    assert_eq!(top_docs.total_hits.value, 10);
    assert_eq!(top_docs.score_docs.len(), 10);

    let expected = [
        (11, 0.9744643_f32),
        (0, 0.802067),
        (14, 0.8000477),
        (3, 0.6710463),
        (5, 0.2621497),
        (1, 0.250627),
        (10, 0.2121821),
        (12, 0.1924969),
        (6, 0.1707188),
        (9, 0.1577401),
    ];
    for (i, &(doc, score)) in expected.iter().enumerate() {
        assert_eq!(top_docs.score_docs[i].doc, doc);
        assert_in_delta!(top_docs.score_docs[i].score, score, 1e-5);
    }
}

// -------------------------------------------------------------------------
// SHOULD Query 2: distributed systems → 12 hits
// Java results:
//   doc=13  score=0.6466637
//   doc=2   score=0.5788049
//   doc=7   score=0.5369343
//   doc=8   score=0.5369343
//   doc=6   score=0.5074845
//   doc=0   score=0.4957356
//   doc=12  score=0.1548606
//   doc=3   score=0.1403393
//   doc=4   score=0.1385187
//   doc=9   score=0.1345176
//   doc=10  score=0.1243533
//   doc=11  score=0.0942376
// -------------------------------------------------------------------------

#[test]
fn test_boolean_should_distributed_systems() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = should_should_query("contents", b"distributed", b"systems");

    let top_docs = searcher.search(&query, 15).unwrap();

    assert_eq!(top_docs.total_hits.value, 12);
    assert_eq!(top_docs.score_docs.len(), 12);

    let expected = [
        (13, 0.6466637_f32),
        (2, 0.5788049),
        (7, 0.5369343),
        (8, 0.5369343),
        (6, 0.5074845),
        (0, 0.4957356),
        (12, 0.1548606),
        (3, 0.1403393),
        (4, 0.1385187),
        (9, 0.1345176),
        (10, 0.1243533),
        (11, 0.0942376),
    ];
    for (i, &(doc, score)) in expected.iter().enumerate() {
        assert_eq!(top_docs.score_docs[i].doc, doc);
        assert_in_delta!(top_docs.score_docs[i].score, score, 1e-5);
    }
}

// -------------------------------------------------------------------------
// SHOULD Query 3: memory performance → 7 hits
// Java results:
//   doc=0   score=1.1177642
//   doc=8   score=0.7110608
//   doc=1   score=0.6353776
//   doc=9   score=0.5691590
//   doc=14  score=0.5055991
//   doc=2   score=0.4995965
//   doc=12  score=0.4880089
// -------------------------------------------------------------------------

#[test]
fn test_boolean_should_memory_performance() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = should_should_query("contents", b"memory", b"performance");

    let top_docs = searcher.search(&query, 15).unwrap();

    assert_eq!(top_docs.total_hits.value, 7);
    assert_eq!(top_docs.score_docs.len(), 7);

    let expected = [
        (0, 1.1177642_f32),
        (8, 0.7110608),
        (1, 0.6353776),
        (9, 0.569159),
        (14, 0.5055991),
        (2, 0.4995965),
        (12, 0.4880089),
    ];
    for (i, &(doc, score)) in expected.iter().enumerate() {
        assert_eq!(top_docs.score_docs[i].doc, doc);
        assert_in_delta!(top_docs.score_docs[i].score, score, 1e-5);
    }
}

// -------------------------------------------------------------------------
// MUST_NOT queries — exclusion
// Cross-validated against Java Lucene 10.3.2. Same index setup as SHOULD tests.
// Java queries via QueryParser: `+word1 -word2` and `word1 word2 -word3`.
// -------------------------------------------------------------------------

/// Helper to build a MUST + single MUST_NOT boolean query.
fn must_must_not_query(field: &str, must_term: &[u8], not_term: &[u8]) -> BooleanQuery {
    let mut builder = BooleanQuery::builder();
    builder.add_query(Box::new(TermQuery::new(field, must_term)), Occur::Must);
    builder.add_query(Box::new(TermQuery::new(field, not_term)), Occur::MustNot);
    builder.build()
}

// -------------------------------------------------------------------------
// MUST_NOT Query 1: +distributed -security → 6 hits
// Java results:
//   doc=13  score=0.4596379
//   doc=2   score=0.4214391
//   doc=7   score=0.4214391
//   doc=8   score=0.4214391
//   doc=0   score=0.3891023
//   doc=6   score=0.3650910
// -------------------------------------------------------------------------

#[test]
fn test_boolean_must_not_distributed_security() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = must_must_not_query("contents", b"distributed", b"security");

    let top_docs = searcher.search(&query, 15).unwrap();

    assert_eq!(top_docs.total_hits.value, 6);
    assert_eq!(top_docs.score_docs.len(), 6);

    let expected = [
        (13, 0.4596379_f32),
        (2, 0.4214391),
        (7, 0.4214391),
        (8, 0.4214391),
        (0, 0.3891023),
        (6, 0.365091),
    ];
    for (i, &(doc, score)) in expected.iter().enumerate() {
        assert_eq!(top_docs.score_docs[i].doc, doc);
        assert_in_delta!(top_docs.score_docs[i].score, score, 1e-5);
    }
}

// -------------------------------------------------------------------------
// MUST_NOT Query 2: +memory -quantum → 2 hits
// Java results:
//   doc=8   score=0.7110608
//   doc=0   score=0.6565015
// -------------------------------------------------------------------------

#[test]
fn test_boolean_must_not_memory_quantum() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = must_must_not_query("contents", b"memory", b"quantum");

    let top_docs = searcher.search(&query, 15).unwrap();

    assert_eq!(top_docs.total_hits.value, 2);
    assert_eq!(top_docs.score_docs.len(), 2);

    let expected = [(8, 0.7110608_f32), (0, 0.6565015)];
    for (i, &(doc, score)) in expected.iter().enumerate() {
        assert_eq!(top_docs.score_docs[i].doc, doc);
        assert_in_delta!(top_docs.score_docs[i].score, score, 1e-5);
    }
}

// -------------------------------------------------------------------------
// MUST_NOT Query 3: distributed -security → 6 hits (SHOULD + single MUST_NOT)
// Java results:
//   doc=13  score=0.4596379
//   doc=2   score=0.4214391
//   doc=7   score=0.4214391
//   doc=8   score=0.4214391
//   doc=0   score=0.3891023
//   doc=6   score=0.3650910
// -------------------------------------------------------------------------

#[test]
fn test_boolean_should_must_not_distributed_security() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);

    let mut builder = BooleanQuery::builder();
    builder.add_query(
        Box::new(TermQuery::new("contents", b"distributed")),
        Occur::Should,
    );
    builder.add_query(
        Box::new(TermQuery::new("contents", b"security")),
        Occur::MustNot,
    );
    let query = builder.build();

    let top_docs = searcher.search(&query, 15).unwrap();

    assert_eq!(top_docs.total_hits.value, 6);
    assert_eq!(top_docs.score_docs.len(), 6);

    let expected = [
        (13, 0.4596379_f32),
        (2, 0.4214391),
        (7, 0.4214391),
        (8, 0.4214391),
        (0, 0.3891023),
        (6, 0.365091),
    ];
    for (i, &(doc, score)) in expected.iter().enumerate() {
        assert_eq!(top_docs.score_docs[i].doc, doc);
        assert_in_delta!(top_docs.score_docs[i].score, score, 1e-5);
    }
}

// -------------------------------------------------------------------------
// MUST_NOT Query 4: memory -quantum → 2 hits (SHOULD + single MUST_NOT)
// Java results:
//   doc=8   score=0.7110608
//   doc=0   score=0.6565015
// -------------------------------------------------------------------------

#[test]
fn test_boolean_should_must_not_memory_quantum() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);

    let mut builder = BooleanQuery::builder();
    builder.add_query(
        Box::new(TermQuery::new("contents", b"memory")),
        Occur::Should,
    );
    builder.add_query(
        Box::new(TermQuery::new("contents", b"quantum")),
        Occur::MustNot,
    );
    let query = builder.build();

    let top_docs = searcher.search(&query, 15).unwrap();

    assert_eq!(top_docs.total_hits.value, 2);
    assert_eq!(top_docs.score_docs.len(), 2);

    let expected = [(8, 0.7110608_f32), (0, 0.6565015)];
    for (i, &(doc, score)) in expected.iter().enumerate() {
        assert_eq!(top_docs.score_docs[i].doc, doc);
        assert_in_delta!(top_docs.score_docs[i].score, score, 1e-5);
    }
}

// -------------------------------------------------------------------------
// Mixed MUST + SHOULD queries
// Cross-validated against Java Lucene 10.3.2. Same index setup.
// Java queries via QueryParser: `+word1 word2` (MUST + SHOULD).
// -------------------------------------------------------------------------

/// Helper to build a MUST + SHOULD boolean query.
fn must_should_query(field: &str, must_term: &[u8], should_term: &[u8]) -> BooleanQuery {
    let mut builder = BooleanQuery::builder();
    builder.add_query(Box::new(TermQuery::new(field, must_term)), Occur::Must);
    builder.add_query(Box::new(TermQuery::new(field, should_term)), Occur::Should);
    builder.build()
}

// -------------------------------------------------------------------------
// Mixed Query 1: +algorithms data → 4 hits (only docs with "algorithms")
// Scores: docs that also contain "data" get boosted
// Java results:
//   doc=11  score=0.9744643
//   doc=0   score=0.8020670
//   doc=14  score=0.8000477
//   doc=3   score=0.6710463
// -------------------------------------------------------------------------

#[test]
fn test_boolean_mixed_algorithms_data() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = must_should_query("contents", b"algorithms", b"data");

    let top_docs = searcher.search(&query, 15).unwrap();

    assert_eq!(top_docs.total_hits.value, 4);
    assert_eq!(top_docs.score_docs.len(), 4);

    let expected = [
        (11, 0.9744643_f32),
        (0, 0.802067),
        (14, 0.8000477),
        (3, 0.6710463),
    ];
    for (i, &(doc, score)) in expected.iter().enumerate() {
        assert_eq!(top_docs.score_docs[i].doc, doc);
        assert_in_delta!(top_docs.score_docs[i].score, score, 1e-5);
    }
}

// -------------------------------------------------------------------------
// Mixed Query 2: +distributed systems → 6 hits
// Java results:
//   doc=13  score=0.6466637
//   doc=2   score=0.5788049
//   doc=7   score=0.5369343
//   doc=8   score=0.5369343
//   doc=6   score=0.5074845
//   doc=0   score=0.4957356
// -------------------------------------------------------------------------

#[test]
fn test_boolean_mixed_distributed_systems() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = must_should_query("contents", b"distributed", b"systems");

    let top_docs = searcher.search(&query, 15).unwrap();

    assert_eq!(top_docs.total_hits.value, 6);
    assert_eq!(top_docs.score_docs.len(), 6);

    let expected = [
        (13, 0.6466637_f32),
        (2, 0.5788049),
        (7, 0.5369343),
        (8, 0.5369343),
        (6, 0.5074845),
        (0, 0.4957356),
    ];
    for (i, &(doc, score)) in expected.iter().enumerate() {
        assert_eq!(top_docs.score_docs[i].doc, doc);
        assert_in_delta!(top_docs.score_docs[i].score, score, 1e-5);
    }
}

// -------------------------------------------------------------------------
// Mixed Query 3: +memory performance → 3 hits
// Java results:
//   doc=0   score=1.1177642
//   doc=8   score=0.7110608
//   doc=9   score=0.5691590
// -------------------------------------------------------------------------

#[test]
fn test_boolean_mixed_memory_performance() {
    let (_dir, reader) = build_golden_docs_index();
    let searcher = IndexSearcher::new(&reader);
    let query = must_should_query("contents", b"memory", b"performance");

    let top_docs = searcher.search(&query, 15).unwrap();

    assert_eq!(top_docs.total_hits.value, 3);
    assert_eq!(top_docs.score_docs.len(), 3);

    let expected = [(0, 1.1177642_f32), (8, 0.7110608), (9, 0.569159)];
    for (i, &(doc, score)) in expected.iter().enumerate() {
        assert_eq!(top_docs.score_docs[i].doc, doc);
        assert_in_delta!(top_docs.score_docs[i].score, score, 1e-5);
    }
}

// -------------------------------------------------------------------------
// Conjunction scoring pruning: verify that BooleanQuery conjunctions interact
// correctly with the totalHits threshold (TOTAL_HITS_THRESHOLD = 1000).
//
// Java's BooleanScorerSupplier uses BlockMaxConjunctionBulkScorer for
// TOP_SCORES mode conjunctions with 2+ scoring clauses. This scorer prunes
// non-competitive docs via setMinCompetitiveScore, which means fewer docs
// reach the collector and totalHits is a lower bound (< actual match count).
//
// For TermQuery, BatchScoreBulkScorer already does this pruning correctly.
// This test verifies that BooleanQuery conjunctions prune the same way.
// -------------------------------------------------------------------------

/// Builds an in-memory index with `doc_count` documents, each containing
/// the given terms in a "content" text field.
fn build_large_index(doc_count: usize, terms: &[&str]) -> (SharedDirectory, DirectoryReader) {
    let config = IndexWriterConfig::default().num_threads(1);
    let directory: SharedDirectory = MemoryDirectory::create();
    let writer = IndexWriter::new(config, Arc::clone(&directory));

    for i in 0..doc_count {
        // Each doc gets the common terms plus filler words to vary document length,
        // which produces varying BM25 scores across docs.
        let filler_count = (i % 20) + 1;
        let filler: String = (0..filler_count)
            .map(|j| format!("word{}", j))
            .collect::<Vec<_>>()
            .join(" ");
        let content = format!("{} {}", terms.join(" "), filler);
        let doc = DocumentBuilder::new()
            .add_field(text("content").value(content.as_str()))
            .build();
        writer.add_document(doc).unwrap();
    }

    writer.commit().unwrap();

    let reader = DirectoryReader::open(&*directory).unwrap();
    (directory, reader)
}

#[test]
fn test_boolean_conjunction_pruning_matches_term_query() {
    let doc_count = 1500;
    let (_dir, reader) = build_large_index(doc_count, &["alpha", "beta"]);
    let searcher = IndexSearcher::new(&reader);

    // TermQuery with default threshold (1000): should prune via BatchScoreBulkScorer,
    // giving totalHits < doc_count.
    let term_query = TermQuery::new("content", b"alpha");
    let term_result = searcher.search(&term_query, 10).unwrap();
    assert_lt!(
        term_result.total_hits.value,
        doc_count as i64,
        "TermQuery should prune non-competitive docs when totalHits > threshold"
    );

    // BooleanQuery +alpha +beta with default threshold: should also prune via
    // BlockMaxConjunctionBulkScorer, giving totalHits < doc_count.
    // Currently FAILS because Rust uses DefaultBulkScorer which counts every doc.
    let bool_query = must_must_query("content", b"alpha", b"beta");
    let bool_result = searcher.search(&bool_query, 10).unwrap();
    assert_lt!(
        bool_result.total_hits.value,
        doc_count as i64,
        "BooleanQuery conjunction should prune non-competitive docs like TermQuery does"
    );
}