hermes-core 1.8.118

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
//! Chunked text fields: every value of the field is its own BM25 unit and
//! results carry per-chunk ordinals (`docs/chunked-text-fields.md`).

use crate::directories::RamDirectory;
use crate::dsl::{Document, Field, PositionMode, Schema, SchemaBuilder};
use crate::index::{Index, IndexConfig, IndexWriter};
use crate::query::{
    BooleanQuery, FusionMethod, MultiValueCombiner, PhraseQuery, PrefixQuery, SearchResult,
    SparseVectorQuery, TermQuery,
};

struct Fields {
    schema: Schema,
    content: Field,
    kind: Field,
    sparse: Field,
}

fn chunked_schema() -> Fields {
    let mut sb = SchemaBuilder::default();
    let languages = sb.add_text_field_with_tokenizer("languages", false, true, "raw_ci");
    sb.set_fast(languages, true);
    let kind = sb.add_text_field_with_tokenizer("kind", true, true, "raw_ci");
    sb.set_fast(kind, true);
    let content = sb.add_text_field_with_tokenizer(
        "content",
        true,
        false,
        "lex(by: languages, segmenter: simple, stem: snowball, variants: false)",
    );
    sb.set_chunked(content, true);
    sb.set_positions(content, PositionMode::TokenPosition);
    let sparse = sb.add_sparse_vector_field("sparse", true, false);
    Fields {
        schema: sb.build(),
        content,
        kind,
        sparse,
    }
}

fn doc(fields: &Fields, kind: &str, chunks: &[&str]) -> Document {
    let mut d = Document::new();
    d.add_text(fields.kind, kind);
    for chunk in chunks {
        d.add_text(fields.content, *chunk);
    }
    d
}

/// Ordinals reported for a hit, ascending.
fn ordinals(result: &SearchResult) -> Vec<u32> {
    let mut ordinals: Vec<u32> = result
        .positions
        .iter()
        .flat_map(|(_, scored)| scored.iter().map(|sp| sp.position))
        .collect();
    ordinals.sort_unstable();
    ordinals
}

fn by_doc(results: &[SearchResult], doc_id: u32) -> &SearchResult {
    results
        .iter()
        .find(|r| r.doc_id == doc_id)
        .unwrap_or_else(|| panic!("doc {doc_id} missing from {results:?}"))
}

async fn open(dir: RamDirectory) -> Index<RamDirectory> {
    Index::open(dir, IndexConfig::default()).await.unwrap()
}

#[tokio::test]
async fn chunked_match_scores_chunks_and_reports_ordinals() {
    let f = chunked_schema();
    let dir = RamDirectory::new();
    let mut writer = IndexWriter::create(dir.clone(), f.schema.clone(), IndexConfig::default())
        .await
        .unwrap();
    writer
        .add_document(doc(
            &f,
            "article",
            &["alpha beta gamma", "delta epsilon", "zeta eta theta needle"],
        ))
        .unwrap();
    writer
        .add_document(doc(&f, "article", &["needle needle here", "other words"]))
        .unwrap();
    writer
        .add_document(doc(&f, "article", &["nothing relevant", "still nothing"]))
        .unwrap();
    writer.commit().await.unwrap();

    let index = open(dir).await;
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();

    // Multi-term OR → chunked MaxScore. Every matching chunk is an ordinal.
    let or_query = BooleanQuery::new()
        .should(TermQuery::text(f.content, "needle"))
        .should(TermQuery::text(f.content, "gamma"));
    let (results, _) = searcher.search_with_positions(&or_query, 10).await.unwrap();
    assert_eq!(results.len(), 2, "doc 2 has no matching chunk: {results:?}");
    let doc0 = by_doc(&results, 0);
    assert_eq!(
        ordinals(doc0),
        vec![0, 2],
        "gamma in chunk 0, needle in chunk 2"
    );
    let doc1 = by_doc(&results, 1);
    assert_eq!(ordinals(doc1), vec![0]);
    // Document score is the best chunk, not the sum over chunks.
    let chunk_score = |result: &SearchResult, ordinal: u32| {
        result.positions[0]
            .1
            .iter()
            .find(|sp| sp.position == ordinal)
            .map(|sp| sp.score)
            .unwrap()
    };
    let best_chunk = chunk_score(doc0, 0).max(chunk_score(doc0, 2));
    assert!((doc0.score - best_chunk).abs() < 1e-6, "{doc0:?}");
    // Each chunk is scored on its own: doc 1's tf=2 needle chunk outranks
    // doc 0's single-occurrence needle chunk.
    assert!(
        chunk_score(doc1, 0) > chunk_score(doc0, 2),
        "tf=2 short chunk must outrank a single occurrence: {results:?}"
    );

    // Single term → same ordinal reporting.
    let term = TermQuery::text(f.content, "needle");
    let (results, _) = searcher.search_with_positions(&term, 10).await.unwrap();
    assert_eq!(ordinals(by_doc(&results, 0)), vec![2]);
    assert_eq!(ordinals(by_doc(&results, 1)), vec![0]);

    // The positions-free API yields the same documents and scores.
    let (plain, _) = searcher.search_with_count(&term, 10).await.unwrap();
    assert_eq!(plain.len(), 2);
    assert!(plain.iter().all(|r| r.positions.is_empty()));
    for hit in &plain {
        assert_eq!(hit.score, by_doc(&results, hit.doc_id).score);
    }
}

#[tokio::test]
async fn chunked_phrase_never_crosses_a_chunk_boundary() {
    let f = chunked_schema();
    let dir = RamDirectory::new();
    let mut writer = IndexWriter::create(dir.clone(), f.schema.clone(), IndexConfig::default())
        .await
        .unwrap();
    // "brown" ends chunk 0 and "fox" starts chunk 1: adjacent in the document,
    // never adjacent inside one chunk.
    writer
        .add_document(doc(&f, "article", &["quick brown", "fox jumps"]))
        .unwrap();
    writer
        .add_document(doc(&f, "article", &["padding text", "quick brown fox"]))
        .unwrap();
    writer.commit().await.unwrap();

    let index = open(dir).await;
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();

    let phrase = |text: &str| {
        PhraseQuery::new(
            f.content,
            text.split(' ').map(|t| t.as_bytes().to_vec()).collect(),
        )
    };
    let (results, _) = searcher
        .search_with_positions(&phrase("brown fox"), 10)
        .await
        .unwrap();
    assert_eq!(results.len(), 1, "{results:?}");
    assert_eq!(results[0].doc_id, 1);
    assert_eq!(ordinals(&results[0]), vec![1]);

    let (results, _) = searcher
        .search_with_positions(&phrase("quick brown"), 10)
        .await
        .unwrap();
    assert_eq!(results.len(), 2);
    assert_eq!(ordinals(by_doc(&results, 0)), vec![0]);
    assert_eq!(ordinals(by_doc(&results, 1)), vec![1]);
}

#[tokio::test]
async fn chunked_bm25_normalises_by_real_chunk_length() {
    let f = chunked_schema();
    let dir = RamDirectory::new();
    let mut writer = IndexWriter::create(dir.clone(), f.schema.clone(), IndexConfig::default())
        .await
        .unwrap();
    let long = format!("needle {}", "filler ".repeat(40));
    writer.add_document(doc(&f, "article", &[&long])).unwrap();
    writer
        .add_document(doc(&f, "article", &["needle filler filler"]))
        .unwrap();
    writer.commit().await.unwrap();

    let index = open(dir).await;
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();
    let (results, _) = searcher
        .search_with_positions(&TermQuery::text(f.content, "needle"), 10)
        .await
        .unwrap();
    assert_eq!(results.len(), 2);
    assert!(
        by_doc(&results, 1).score > by_doc(&results, 0).score,
        "same tf, shorter chunk must score higher: {results:?}"
    );
}

#[tokio::test]
async fn chunked_ordinals_survive_segment_merge() {
    let f = chunked_schema();
    let dir = RamDirectory::new();
    let mut writer = IndexWriter::create(dir.clone(), f.schema.clone(), IndexConfig::default())
        .await
        .unwrap();
    writer
        .add_document(doc(&f, "article", &["first chunk", "second needle"]))
        .unwrap();
    writer.commit().await.unwrap();
    // Second segment: its virtual ids restart at 0 and must be re-based on merge.
    writer
        .add_document(doc(
            &f,
            "article",
            &["another chunk", "more text", "final needle"],
        ))
        .unwrap();
    writer
        .add_document(doc(&f, "article", &["needle first"]))
        .unwrap();
    writer.commit().await.unwrap();
    writer.force_merge().await.unwrap();

    let index = open(dir).await;
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();
    let (results, _) = searcher
        .search_with_positions(&TermQuery::text(f.content, "needle"), 10)
        .await
        .unwrap();
    assert_eq!(results.len(), 3, "{results:?}");
    let segments: std::collections::HashSet<u128> = results.iter().map(|r| r.segment_id).collect();
    assert_eq!(segments.len(), 1, "force_merge must leave one segment");
    assert_eq!(ordinals(by_doc(&results, 0)), vec![1]);
    assert_eq!(ordinals(by_doc(&results, 1)), vec![2]);
    assert_eq!(ordinals(by_doc(&results, 2)), vec![0]);

    // Phrases still resolve per chunk after the merge.
    let phrase = PhraseQuery::new(f.content, vec![b"final".to_vec(), b"needle".to_vec()]);
    let (results, _) = searcher.search_with_positions(&phrase, 10).await.unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].doc_id, 1);
    assert_eq!(ordinals(&results[0]), vec![2]);
}

#[tokio::test]
async fn chunked_text_fuses_with_sparse_vectors_on_shared_ordinals() {
    let f = chunked_schema();
    let dir = RamDirectory::new();
    let mut writer = IndexWriter::create(dir.clone(), f.schema.clone(), IndexConfig::default())
        .await
        .unwrap();
    // Doc 0: text and vector agree on chunk 0.
    let mut d = doc(&f, "article", &["needle words", "hay words"]);
    d.add_sparse_vector(f.sparse, vec![(1, 1.0)]);
    d.add_sparse_vector(f.sparse, vec![(2, 1.0)]);
    writer.add_document(d).unwrap();
    // Doc 1: text and vector agree on chunk 1.
    let mut d = doc(&f, "article", &["hay words", "needle words"]);
    d.add_sparse_vector(f.sparse, vec![(2, 1.0)]);
    d.add_sparse_vector(f.sparse, vec![(1, 1.0)]);
    writer.add_document(d).unwrap();
    // Doc 2: text hits chunk 0 but the vector hits chunk 1 — no corroboration.
    let mut d = doc(&f, "article", &["needle words", "hay words"]);
    d.add_sparse_vector(f.sparse, vec![(2, 1.0)]);
    d.add_sparse_vector(f.sparse, vec![(1, 1.0)]);
    writer.add_document(d).unwrap();
    writer.commit().await.unwrap();

    let index = open(dir).await;
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();

    let text = TermQuery::text(f.content, "needle");
    let sparse = SparseVectorQuery::new(f.sparse, vec![(1, 1.0)]);
    let fused = searcher
        .search_fused(
            &[(&text, 1.0), (&sparse, 1.0)],
            10,
            10,
            FusionMethod::default(),
            MultiValueCombiner::Max,
        )
        .await
        .unwrap();
    assert_eq!(fused.len(), 3, "{fused:?}");
    let doc0 = by_doc(&fused, 0);
    let doc1 = by_doc(&fused, 1);
    let doc2 = by_doc(&fused, 2);
    assert_eq!(ordinals(doc0), vec![0], "both verticals land on chunk 0");
    assert_eq!(ordinals(doc1), vec![1], "both verticals land on chunk 1");
    assert_eq!(
        ordinals(doc2),
        vec![0, 1],
        "disagreeing verticals stay separate chunks"
    );
    assert!(
        doc0.score > doc2.score && doc1.score > doc2.score,
        "same-chunk corroboration must compound: {fused:?}"
    );
}

#[tokio::test]
async fn chunked_match_composes_with_document_filters() {
    let f = chunked_schema();
    let dir = RamDirectory::new();
    let mut writer = IndexWriter::create(dir.clone(), f.schema.clone(), IndexConfig::default())
        .await
        .unwrap();
    writer
        .add_document(doc(&f, "article", &["hay", "needle here"]))
        .unwrap();
    writer
        .add_document(doc(&f, "book", &["needle here", "hay"]))
        .unwrap();
    writer
        .add_document(doc(&f, "article", &["hay only"]))
        .unwrap();
    writer.commit().await.unwrap();

    let index = open(dir).await;
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();

    let query = BooleanQuery::new()
        .must(TermQuery::text(f.kind, "book"))
        .should(TermQuery::text(f.content, "needle"))
        .should(TermQuery::text(f.content, "here"));
    let (results, _) = searcher.search_with_positions(&query, 10).await.unwrap();
    assert_eq!(results.len(), 1, "{results:?}");
    assert_eq!(results[0].doc_id, 1);
    assert_eq!(ordinals(&results[0]), vec![0]);

    // Terms spread over different chunks all report their own ordinal.
    let query = BooleanQuery::new()
        .should(TermQuery::text(f.content, "needle"))
        .should(TermQuery::text(f.content, "hay"));
    let (results, _) = searcher.search_with_positions(&query, 10).await.unwrap();
    assert_eq!(ordinals(by_doc(&results, 0)), vec![0, 1]);
    assert_eq!(ordinals(by_doc(&results, 1)), vec![0, 1]);
    assert_eq!(ordinals(by_doc(&results, 2)), vec![0]);
}

/// MUST phrases and filters become one document bitset that the chunked
/// text MaxScore executor applies as a predicate: the scored top-k is exact
/// over the filtered documents, documents matching only the filters fill the
/// tail with score 0, and chunk ordinals are still reported.
#[tokio::test]
async fn filters_and_phrases_push_into_chunked_text_maxscore() {
    let f = chunked_schema();
    let dir = RamDirectory::new();
    let mut writer = IndexWriter::create(dir.clone(), f.schema.clone(), IndexConfig::default())
        .await
        .unwrap();
    for (kind, chunks) in [
        ("article", vec!["quick brown fox", "lazy dog"]),
        ("book", vec!["quick brown fox jumps", "over the lazy dog"]),
        ("book", vec!["brown fox", "quick dog"]),
        ("article", vec!["quick brown", "fox"]),
        ("book", vec!["nothing here"]),
    ] {
        writer.add_document(doc(&f, kind, &chunks)).unwrap();
    }
    writer.commit().await.unwrap();
    let index = open(dir).await;
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();
    let phrase = |text: &str| {
        PhraseQuery::new(
            f.content,
            text.split(' ').map(|t| t.as_bytes().to_vec()).collect(),
        )
    };
    let ids = |results: &[SearchResult]| {
        let mut ids: Vec<u32> = results.iter().map(|r| r.doc_id).collect();
        ids.sort_unstable();
        ids
    };

    // Phrase constraint: doc 3 has "brown" and "fox" in different chunks.
    let query = BooleanQuery::new()
        .must(phrase("brown fox"))
        .should(TermQuery::text(f.content, "quick"))
        .should(TermQuery::text(f.content, "dog"));
    let (results, _) = searcher.search_with_positions(&query, 10).await.unwrap();
    assert_eq!(ids(&results), vec![0, 1, 2], "{results:?}");
    assert!(results.iter().all(|r| r.score > 0.0));
    assert_eq!(ordinals(by_doc(&results, 0)), vec![0, 1]);
    assert_eq!(ordinals(by_doc(&results, 2)), vec![1]);

    // Plus a fast-field filter.
    let query = BooleanQuery::new()
        .must(phrase("brown fox"))
        .must(TermQuery::text(f.kind, "book"))
        .should(TermQuery::text(f.content, "quick"))
        .should(TermQuery::text(f.content, "dog"));
    let (results, _) = searcher.search_with_positions(&query, 10).await.unwrap();
    assert_eq!(ids(&results), vec![1, 2], "{results:?}");

    // An OR of phrases (the shape a client uses to try several fields or
    // hints) is one bitset; a document matching only the phrases and none
    // of the scored terms is still returned, with score 0.
    let either = BooleanQuery::new()
        .should(phrase("brown fox"))
        .should(phrase("nothing here"));
    let query = BooleanQuery::new()
        .must(either)
        .should(TermQuery::text(f.content, "quick"))
        .should(TermQuery::text(f.content, "dog"));
    let (results, _) = searcher.search_with_positions(&query, 10).await.unwrap();
    assert_eq!(ids(&results), vec![0, 1, 2, 4], "{results:?}");
    assert_eq!(by_doc(&results, 4).score, 0.0);
    assert!(by_doc(&results, 1).score > 0.0);

    // With a small limit only scored documents make the cut.
    let (results, _) = searcher.search_with_positions(&query, 2).await.unwrap();
    assert_eq!(results.len(), 2);
    assert!(results.iter().all(|r| r.score > 0.0));
}

/// Field-level BP reordering of a chunked text field: the pass permutes the
/// field's virtual ids (visible in the chunk map), document ids and every
/// other file stay put, and every query returns the same documents, scores
/// and ordinals before and after, also after a subsequent merge.
#[tokio::test]
async fn chunked_text_field_reorders_through_its_chunk_map() {
    use crate::query::PhraseQuery;

    let mut sb = SchemaBuilder::default();
    let languages = sb.add_text_field_with_tokenizer("languages", false, true, "raw_ci");
    sb.set_fast(languages, true);
    let kind = sb.add_text_field_with_tokenizer("kind", true, true, "raw_ci");
    sb.set_fast(kind, true);
    let content = sb.add_text_field_with_tokenizer(
        "content",
        true,
        false,
        "lex(by: languages, segmenter: simple, stem: snowball, variants: false)",
    );
    sb.set_chunked(content, true);
    sb.set_positions(content, PositionMode::TokenPosition);
    sb.set_reorder(content, true);
    // Original document number: merge output may place segments in any
    // order, so results are compared by this rather than by doc id.
    let number = sb.add_u64_field("n", false, false);
    sb.set_fast(number, true);
    let schema = sb.build();

    // Two interleaved topical clusters: even documents use vocabulary A,
    // odd documents vocabulary B, so BP has an obvious better order.
    let dir = RamDirectory::new();
    let mut writer = IndexWriter::create(dir.clone(), schema.clone(), IndexConfig::default())
        .await
        .unwrap();
    let vocab_a = [
        "quantum",
        "lattice",
        "photon",
        "spin",
        "boson",
        "qubit",
        "decoherence",
    ];
    let vocab_b = [
        "kernel",
        "scheduler",
        "thread",
        "mutex",
        "syscall",
        "paging",
        "latency",
    ];
    let mut seed = 0x1234_5678_9ABC_DEF1u64;
    let mut rng = move || {
        seed ^= seed << 13;
        seed ^= seed >> 7;
        seed ^= seed << 17;
        seed
    };
    for d in 0..600u32 {
        let vocab = if d % 2 == 0 { &vocab_a } else { &vocab_b };
        let mut chunks: Vec<String> = Vec::new();
        for _ in 0..2 {
            let words: Vec<&str> = (0..6).map(|_| vocab[(rng() % 7) as usize]).collect();
            chunks.push(words.join(" "));
        }
        let mut doc = Document::new();
        doc.add_text(languages, "en");
        doc.add_text(kind, if d % 3 == 0 { "book" } else { "article" });
        doc.add_u64(number, u64::from(d));
        for chunk in &chunks {
            doc.add_text(content, chunk);
        }
        writer.add_document(doc).unwrap();
    }
    writer.commit().await.unwrap();

    let queries: Vec<Box<dyn crate::query::Query>> = vec![
        Box::new(
            BooleanQuery::new()
                .should(TermQuery::text(content, "quantum"))
                .should(TermQuery::text(content, "photon"))
                .should(TermQuery::text(content, "kernel")),
        ),
        Box::new(PhraseQuery::new(
            content,
            vec![b"spin".to_vec(), b"boson".to_vec()],
        )),
        Box::new(
            BooleanQuery::new()
                .must(TermQuery::text(kind, "book"))
                .must(PhraseQuery::new(
                    content,
                    vec![b"thread".to_vec(), b"mutex".to_vec()],
                ))
                .should(TermQuery::text(content, "scheduler"))
                .should(TermQuery::text(content, "latency")),
        ),
    ];
    async fn snapshot(
        index: &Index<RamDirectory>,
        queries: &[Box<dyn crate::query::Query>],
        number: Field,
    ) -> Vec<Vec<(u64, i64, Vec<u32>)>> {
        let reader = index.reader().await.unwrap();
        let searcher = reader.searcher().await.unwrap();
        let mut out = Vec::new();
        for (i, query) in queries.iter().enumerate() {
            let (results, _) = searcher.search_with_positions(&**query, 50).await.unwrap();
            let mut rows: Vec<(u64, i64, Vec<u32>)> = results
                .iter()
                .map(|r| {
                    let segment = searcher
                        .segment_readers()
                        .iter()
                        .find(|s| s.meta().id == r.segment_id)
                        .unwrap();
                    let n = segment.fast_field(number.0).unwrap().get_u64(r.doc_id);
                    // The OR query's ordinal list depends on which of many
                    // equally scored chunks make the over-fetched pool, a
                    // tie broken by virtual-id order that the reorder
                    // legitimately changes; the phrase and filtered queries
                    // return every matching chunk.
                    let ords = if i == 0 { Vec::new() } else { ordinals(r) };
                    (n, (r.score * 1e4).round() as i64, ords)
                })
                .collect();
            rows.sort_by_key(|(n, _, _)| *n);
            out.push(rows);
        }
        out
    }

    let before = snapshot(&open(dir.clone()).await, &queries, number).await;
    assert!(before.iter().all(|r| !r.is_empty()), "{before:?}");

    writer.reorder().await.unwrap();
    let index = open(dir.clone()).await;
    let after = snapshot(&index, &queries, number).await;
    assert_eq!(before, after);

    // The field's virtual ids were permuted: chunk-map doc ids are no longer
    // non-decreasing, while document ids themselves did not move.
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();
    let segments = searcher.segment_readers();
    assert_eq!(segments.len(), 1);
    let map = segments[0].chunk_map(content).unwrap();
    assert_eq!(map.num_chunks(), 1200);
    let doc_ids: Vec<u32> = (0..map.num_chunks()).map(|v| map.doc_id(v)).collect();
    assert!(
        doc_ids.windows(2).any(|w| w[0] > w[1]),
        "chunk map still in indexing order"
    );
    let mut seen: Vec<u32> = doc_ids.clone();
    seen.sort_unstable();
    seen.dedup();
    assert_eq!(seen.len(), 600);

    // Merging a reordered segment keeps working, and results stay equal.
    for d in 600..640u32 {
        let mut doc = Document::new();
        doc.add_text(languages, "en");
        doc.add_text(kind, "article");
        doc.add_u64(number, u64::from(d));
        doc.add_text(
            content,
            if d % 2 == 0 {
                "quantum photon"
            } else {
                "kernel thread"
            },
        );
        writer.add_document(doc).unwrap();
    }
    writer.commit().await.unwrap();
    writer.force_merge().await.unwrap();
    // The new documents change idf and average length, so compare the
    // matched documents (and, for the exact-match queries, their ordinals)
    // rather than scores.
    let merged = snapshot(&open(dir).await, &queries, number).await;
    for (i, (a, b)) in after.iter().zip(&merged).enumerate() {
        let a: Vec<(u64, Vec<u32>)> = a.iter().map(|(d, _, o)| (*d, o.clone())).collect();
        let b: Vec<(u64, Vec<u32>)> = b
            .iter()
            .filter(|(d, _, _)| *d < 600)
            .map(|(d, _, o)| (*d, o.clone()))
            .collect();
        if i < 2 {
            // Top-50 by score can shift with the changed statistics (both
            // the OR and the phrase query have more than 50 matches); the
            // filtered query is an exact set.
            continue;
        }
        assert_eq!(a, b, "query {i}");
    }
    assert!(
        merged[0].iter().any(|(d, _, _)| *d >= 600),
        "{:?}",
        merged[0]
    );
}

#[tokio::test]
async fn chunked_field_rejects_prefix_queries_loudly() {
    let f = chunked_schema();
    let dir = RamDirectory::new();
    let mut writer = IndexWriter::create(dir.clone(), f.schema.clone(), IndexConfig::default())
        .await
        .unwrap();
    writer
        .add_document(doc(&f, "article", &["needle here"]))
        .unwrap();
    writer.commit().await.unwrap();

    let index = open(dir).await;
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();
    let error = searcher
        .search_with_count(&PrefixQuery::text(f.content, "need"), 10)
        .await
        .unwrap_err();
    assert!(
        error.to_string().contains("chunked"),
        "prefix on a chunked field must fail with an actionable message: {error}"
    );
}