lucisearch 0.8.1

Embeddable, in-process search engine — the SQLite/DuckDB of search
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
//! Tests for kNN as a ScoringExpression variant (query-level kNN).
//!
//! See [[feature-knn-query-type]].

use luci::index::Index;
use luci::mapping::{FieldType, Mapping};
use luci::search::expression::parse_search;
use serde_json::json;

fn test_dir(name: &str) -> std::path::PathBuf {
    let dir = std::env::temp_dir().join(format!("luci_knn_query_{}_{name}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    dir
}

fn cleanup(path: &std::path::Path) {
    let _ = std::fs::remove_dir_all(path);
}

fn build_index(name: &str) -> (std::path::PathBuf, Index) {
    let path = test_dir(name);
    let schema = Mapping::builder()
        .field("title", FieldType::Text)
        .field("tag", FieldType::Keyword)
        .field("embedding", FieldType::dense_vector(4))
        .build();
    let index = Index::create_with_mapping(&path, schema).unwrap();

    index.bulk(vec![
        json!({"title": "search engine design", "tag": "tech", "embedding": [0.9, 0.1, 0.0, 0.0]}),
        json!({"title": "search algorithms", "tag": "tech", "embedding": [0.1, 0.9, 0.0, 0.0]}),
        json!({"title": "cute cats", "tag": "animal", "embedding": [0.0, 0.0, 0.9, 0.1]}),
        json!({"title": "search optimization", "tag": "tech", "embedding": [0.0, 0.0, 0.1, 0.9]}),
        json!({"title": "happy dog", "tag": "animal", "embedding": [0.0, 0.0, 0.0, 0.1]}),
    ]).unwrap();

    (path, index)
}

fn search(
    index: &Index,
    query: serde_json::Value,
    size: usize,
) -> luci::search::results::SearchResults {
    let expr = parse_search(query, size).unwrap();
    index.search(&expr).unwrap()
}

// --- 1. Standalone kNN query ---

#[test]
fn knn_query_standalone() {
    let (path, index) = build_index("standalone");

    let results = search(
        &index,
        json!({
            "query": {"knn": {
                "field": "embedding",
                "query_vector": [1.0, 0.0, 0.0, 0.0],
                "k": 3
            }}
        }),
        10,
    );

    assert_eq!(results.len(), 3);
    // Doc 0 (0.9, 0.1, 0, 0) is closest to (1, 0, 0, 0)
    assert_eq!(results.hit(0).unwrap().doc_id().as_u32(), 0);

    cleanup(&path);
}

// --- 2. Threshold filters low-similarity results ---

#[test]
fn knn_query_with_threshold() {
    let (path, index) = build_index("threshold");

    // Without threshold: get all 5
    let all = search(
        &index,
        json!({
            "query": {"knn": {
                "field": "embedding",
                "query_vector": [1.0, 0.0, 0.0, 0.0],
                "k": 5
            }}
        }),
        10,
    );
    assert_eq!(all.len(), 5);

    // With threshold: only close results
    let filtered = search(
        &index,
        json!({
            "query": {"knn": {
                "field": "embedding",
                "query_vector": [1.0, 0.0, 0.0, 0.0],
                "k": 5,
                "threshold": 0.6
            }}
        }),
        10,
    );

    // Doc 0 (0.9, 0.1, 0, 0) is very close → should pass
    // Far docs should be filtered
    assert!(
        filtered.len() < all.len(),
        "threshold should reduce results"
    );
    assert!(filtered.len() >= 1, "closest doc should pass threshold");

    cleanup(&path);
}

// --- 3. Threshold too high excludes all ---

#[test]
fn knn_query_threshold_excludes_all() {
    let (path, index) = build_index("threshold_all");

    let results = search(
        &index,
        json!({
            "query": {"knn": {
                "field": "embedding",
                "query_vector": [1.0, 0.0, 0.0, 0.0],
                "k": 5,
                "threshold": 0.999
            }}
        }),
        10,
    );

    // No exact match exists, threshold 0.999 should exclude everything
    assert_eq!(results.len(), 0);

    cleanup(&path);
}

// --- 4. kNN in bool.should (hybrid search) ---

#[test]
fn knn_query_in_bool_should() {
    let (path, index) = build_index("bool_should");

    let results = search(
        &index,
        json!({
            "query": {"bool": {"should": [
                {"match": {"title": "search engine"}},
                {"knn": {
                    "field": "embedding",
                    "query_vector": [1.0, 0.0, 0.0, 0.0],
                    "k": 3
                }}
            ]}}
        }),
        10,
    );

    assert!(!results.is_empty());
    // Doc 0 matches both text ("search engine design") and kNN (closest vector)
    // so it should be ranked first
    assert_eq!(results.hit(0).unwrap().doc_id().as_u32(), 0);

    cleanup(&path);
}

// --- 5. kNN in bool.must (conjunction) ---

#[test]
fn knn_query_in_bool_must() {
    let (path, index) = build_index("bool_must");

    let results = search(
        &index,
        json!({
            "query": {"bool": {"must": [
                {"match": {"title": "search"}},
                {"knn": {
                    "field": "embedding",
                    "query_vector": [1.0, 0.0, 0.0, 0.0],
                    "k": 5
                }}
            ]}}
        }),
        10,
    );

    // Only docs matching BOTH text "search" AND in kNN top-5
    // "search" matches docs 0, 1, 3. All are in kNN top-5.
    for hit in results.iter() {
        let source = hit.source().unwrap();
        let title = source["title"].as_str().unwrap();
        assert!(
            title.contains("search"),
            "must conjunction: doc should match 'search', got '{title}'"
        );
    }

    cleanup(&path);
}

// --- 6. kNN in bool.filter (vector as filter) ---

#[test]
fn knn_query_in_bool_filter() {
    let (path, index) = build_index("bool_filter");

    let results = search(
        &index,
        json!({
            "query": {"bool": {
                "must": [{"match": {"title": "search"}}],
                "filter": [{"knn": {
                    "field": "embedding",
                    "query_vector": [1.0, 0.0, 0.0, 0.0],
                    "k": 2
                }}]
            }}
        }),
        10,
    );

    // "search" matches docs 0, 1, 3
    // kNN top-2 closest to (1,0,0,0): doc 0 (0.9,0.1,0,0), doc 1 (0.1,0.9,0,0)
    // Intersection: docs 0 and 1
    assert!(results.len() <= 2, "filter should restrict to kNN top-2");
    for hit in results.iter() {
        let id = hit.doc_id().as_u32();
        assert!(id == 0 || id == 1, "expected doc 0 or 1, got {id}");
    }

    cleanup(&path);
}

// --- 7. num_candidates defaults to 1.5 * k ---

#[test]
fn knn_query_num_candidates_default() {
    // Verify the parse succeeds without num_candidates specified
    // (verifies the default path doesn't error)
    let _expr = parse_search(
        json!({
            "query": {"knn": {
                "field": "f",
                "query_vector": [1.0],
                "k": 10
            }}
        }),
        10,
    )
    .unwrap();
}

// --- 8. Scores are descending ---

#[test]
fn knn_query_scores_descending() {
    let (path, index) = build_index("scores_desc");

    let results = search(
        &index,
        json!({
            "query": {"knn": {
                "field": "embedding",
                "query_vector": [1.0, 0.0, 0.0, 0.0],
                "k": 5
            }}
        }),
        10,
    );

    for i in 0..results.len().saturating_sub(1) {
        let a = results.hit(i).unwrap().score();
        let b = results.hit(i + 1).unwrap().score();
        assert!(
            a >= b,
            "scores should be descending: hit[{i}]={a} < hit[{}]={b}",
            i + 1
        );
    }

    cleanup(&path);
}

// --- 9. Scores non-negative ---

#[test]
fn knn_query_score_range() {
    let (path, index) = build_index("score_range");

    let results = search(
        &index,
        json!({
            "query": {"knn": {
                "field": "embedding",
                "query_vector": [1.0, 0.0, 0.0, 0.0],
                "k": 5
            }}
        }),
        10,
    );

    for hit in results.iter() {
        let s = hit.score();
        // Cosine metric: scores are in [0, 1]. Other metrics may exceed 1
        // for unnormalized vectors (matching Lucene behavior).
        assert!(s >= 0.0, "score should be non-negative, got {s}");
    }

    cleanup(&path);
}

// --- 10. Explain works ---

#[test]
fn knn_query_explain() {
    let (path, index) = build_index("explain");

    let results = search(
        &index,
        json!({
            "query": {"knn": {
                "field": "embedding",
                "query_vector": [1.0, 0.0, 0.0, 0.0],
                "k": 3
            }}
        }),
        10,
    );

    let hit = results.hit(0).unwrap();
    let explanation = hit.explain().expect("explain should not error");
    assert!(
        explanation.is_some(),
        "kNN query should produce an explanation"
    );
    let expl = explanation.unwrap();
    assert!(expl.value > 0.0, "explanation score should be > 0");

    cleanup(&path);
}

// --- 12. Non-numeric query_vector is rejected ---

#[test]
fn knn_query_invalid_vector() {
    let result = parse_search(
        json!({
            "query": {"knn": {
                "field": "embedding",
                "query_vector": [1, "bad", 3]
            }}
        }),
        10,
    );

    assert!(
        result.is_err(),
        "non-numeric vector elements should be rejected"
    );
}

// --- 13. k=0 is rejected ---

#[test]
fn knn_query_zero_k() {
    let result = parse_search(
        json!({
            "query": {"knn": {
                "field": "embedding",
                "query_vector": [1.0, 0.0, 0.0, 0.0],
                "k": 0
            }}
        }),
        10,
    );

    assert!(result.is_err(), "k=0 should be rejected");
}

// --- 14. Bool must correctness (validates doc_id sort fix) ---

#[test]
fn knn_query_bool_must_correctness() {
    let path = test_dir("bool_must_correct");
    let schema = Mapping::builder()
        .field("tag", FieldType::Keyword)
        .field("embedding", FieldType::dense_vector(4))
        .build();
    let index = Index::create_with_mapping(&path, schema).unwrap();

    // 20 docs: 5 tagged "target", 15 tagged "other"
    // Vectors are spread across 4D space
    let mut docs = Vec::new();
    for i in 0..20 {
        let tag = if i < 5 { "target" } else { "other" };
        let angle = (i as f32) * 0.3;
        let v = [
            angle.cos(),
            angle.sin(),
            (angle * 0.5).cos(),
            (angle * 0.5).sin(),
        ];
        docs.push(json!({"tag": tag, "embedding": v}));
    }
    index.bulk(docs).unwrap();

    // kNN top-10 should include some "target" and some "other" docs
    let knn_only = search(
        &index,
        json!({
            "query": {"knn": {
                "field": "embedding",
                "query_vector": [1.0, 0.0, 1.0, 0.0],
                "k": 10
            }}
        }),
        10,
    );

    // Term match: only "target" docs
    let term_only = search(
        &index,
        json!({
            "query": {"term": {"tag": "target"}}
        }),
        10,
    );

    // Conjunction: must match both
    let conjunction = search(
        &index,
        json!({
            "query": {"bool": {"must": [
                {"term": {"tag": "target"}},
                {"knn": {
                    "field": "embedding",
                    "query_vector": [1.0, 0.0, 1.0, 0.0],
                    "k": 10
                }}
            ]}}
        }),
        10,
    );

    let knn_ids: std::collections::HashSet<u32> =
        knn_only.iter().map(|h| h.doc_id().as_u32()).collect();
    let term_ids: std::collections::HashSet<u32> =
        term_only.iter().map(|h| h.doc_id().as_u32()).collect();

    // Every conjunction result must be in BOTH kNN and term results
    for hit in conjunction.iter() {
        let id = hit.doc_id().as_u32();
        assert!(
            knn_ids.contains(&id),
            "conjunction doc {id} not in kNN results"
        );
        assert!(
            term_ids.contains(&id),
            "conjunction doc {id} not in term results"
        );
    }

    // Conjunction should not be empty (some target docs should be in kNN top-10)
    assert!(
        !conjunction.is_empty(),
        "conjunction should find at least one doc matching both conditions"
    );

    cleanup(&path);
}

// --- 11. Dimension mismatch at bind time ---
// After [[fix-silent-scorer-errors]], bind errors propagate instead of
// being silently swallowed. A kNN query_vector with the wrong number of
// dimensions fails loudly at search time rather than returning empty.

#[test]
fn knn_query_dimension_mismatch() {
    let (path, index) = build_index("dim_mismatch");

    let expr = parse_search(
        json!({
            "query": {"knn": {
                "field": "embedding",
                "query_vector": [1.0, 2.0],
                "k": 5
            }}
        }),
        10,
    )
    .unwrap();
    let err = match index.search(&expr) {
        Ok(_) => panic!("dimension mismatch must error, not return empty"),
        Err(e) => e,
    };
    let msg = err.to_string();
    assert!(
        msg.contains("2 dimensions") && msg.contains("embedding"),
        "error message should name the dim mismatch: {msg}"
    );

    cleanup(&path);
}

// --- kNN field validation (regression for the silent-empty E4 bug:
//     knn on a non-dense_vector or unknown field must error, not
//     return zero hits — [[code-must-not-lie]]) ---

#[test]
fn knn_query_non_vector_field_errors() {
    // `title` is a Text field. A knn query against it can't run; the
    // engine must say so rather than silently returning zero hits.
    let (path, index) = build_index("non_vector_field");

    let expr = parse_search(
        json!({
            "query": {"knn": {
                "field": "title",
                "query_vector": [1.0, 2.0, 3.0, 4.0],
                "k": 5
            }}
        }),
        10,
    )
    .unwrap();
    let err = match index.search(&expr) {
        Ok(_) => panic!("knn on a non-dense_vector field must error, not return empty"),
        Err(e) => e,
    };
    let msg = err.to_string();
    assert!(
        msg.contains("title") && msg.contains("dense_vector"),
        "error should name the field and that it is not a dense_vector: {msg}"
    );

    cleanup(&path);
}

#[test]
fn knn_query_unknown_field_errors() {
    // A field absent from the mapping must error, not silently empty.
    let (path, index) = build_index("unknown_field");

    let expr = parse_search(
        json!({
            "query": {"knn": {
                "field": "nope",
                "query_vector": [1.0, 2.0, 3.0, 4.0],
                "k": 5
            }}
        }),
        10,
    )
    .unwrap();
    let err = match index.search(&expr) {
        Ok(_) => panic!("knn on an unknown field must error, not return empty"),
        Err(e) => e,
    };
    let msg = err.to_string();
    assert!(
        msg.contains("nope") && msg.contains("unknown"),
        "error should name the unknown field: {msg}"
    );

    cleanup(&path);
}

#[test]
fn knn_dims_zero_builder_rejected() {
    // The programmatic builder does not guard dims == 0 (the JSON parser
    // does). `Mapping::validate()` — invoked by `create_with_mapping` —
    // must reject it, else a knn against the field silently empties
    // (GlobalHnsw skips dims == 0). See [[feature-knn-query-type]] §4.
    let path = test_dir("dims_zero");
    let schema = Mapping::builder()
        .field("v", FieldType::dense_vector(0))
        .build();
    let err = match Index::create_with_mapping(&path, schema) {
        Ok(_) => panic!("dense_vector(0) must be rejected at mapping validation"),
        Err(e) => e,
    };
    let msg = err.to_string();
    assert!(
        msg.contains("v") && (msg.contains("dims") || msg.contains("dimension")),
        "error should name the field and dims: {msg}"
    );

    cleanup(&path);
}

// --- §4b: bind-time errors must propagate through the three call paths
//     that historically swallowed them — `filter` aggs, `filters` aggs,
//     and `inner_hits`. The filter query is now bound once against the
//     real searcher at `AggregationExpression::bind` time, so a bad-field
//     knn surfaces as `Err` instead of a silently empty bucket, and a
//     valid one actually runs. See [[feature-knn-query-type]] §4b and
//     [[code-must-not-lie]]. ---

#[test]
fn knn_bad_field_in_agg_filter_errors() {
    // A bad-field knn inside a `filter` aggregation binds at
    // `AggregationExpression::bind` time; the error must reach the search
    // caller, not collapse to an empty `filter` bucket.
    let (path, index) = build_index("agg_filter_bad_field");
    let expr = parse_search(
        json!({
            "query": {"match_all": {}},
            "aggs": {"f": {"filter": {"knn": {
                "field": "nope",
                "query_vector": [1.0, 0.0, 0.0, 0.0],
                "k": 2
            }}}}
        }),
        10,
    )
    .unwrap();
    let err = match index.search(&expr) {
        Ok(_) => panic!("bad-field knn in a filter agg must error, not return empty buckets"),
        Err(e) => e,
    };
    let msg = err.to_string();
    assert!(
        msg.contains("nope") && msg.contains("unknown"),
        "error should name the unknown field: {msg}"
    );

    cleanup(&path);
}

#[test]
fn knn_valid_field_in_agg_filter_works() {
    // Positive companion: binding against the *real* searcher lets the
    // valid case run. Deterministic single-segment corpus of 4 separated
    // unit vectors; query_vector [1,0,0,0] with k=2 + num_candidates=10
    // (≥ corpus) makes the top-2 = docs {0,1} unambiguous (wide cosine
    // margin over docs {2,3}). The filter bucket counts exactly those 2.
    let path = test_dir("agg_filter_valid");
    let schema = Mapping::builder()
        .field("embedding", FieldType::dense_vector(4))
        .build();
    let index = Index::create_with_mapping(&path, schema).unwrap();
    index
        .bulk(vec![
            json!({"embedding": [1.0, 0.0, 0.0, 0.0]}),
            json!({"embedding": [0.9, 0.1, 0.0, 0.0]}),
            json!({"embedding": [0.0, 1.0, 0.0, 0.0]}),
            json!({"embedding": [0.0, 0.0, 1.0, 0.0]}),
        ])
        .unwrap();

    let expr = parse_search(
        json!({
            "query": {"match_all": {}},
            "aggs": {"f": {"filter": {"knn": {
                "field": "embedding",
                "query_vector": [1.0, 0.0, 0.0, 0.0],
                "k": 2,
                "num_candidates": 10
            }}}}
        }),
        10,
    )
    .unwrap();
    let results = index.search(&expr).unwrap();
    let agg = results.aggregations()["f"].to_json();
    assert_eq!(
        agg["buckets"][0]["doc_count"].as_u64().unwrap(),
        2,
        "filter-agg knn top-2 should count exactly docs {{0,1}}: {agg}"
    );

    cleanup(&path);
}

#[test]
fn knn_bad_field_in_filters_agg_errors() {
    // The multi-filter `filters` factory is a *separate* swallow site
    // (its own per-filter bind loop). A bad-field knn in any named filter
    // must surface as `Err` — the "easy" parallel arm is where an
    // incomplete fix would hide.
    let (path, index) = build_index("filters_agg_bad_field");
    let expr = parse_search(
        json!({
            "query": {"match_all": {}},
            "aggs": {"f": {"filters": {"filters": {
                "a": {"knn": {"field": "nope", "query_vector": [1.0, 0.0, 0.0, 0.0], "k": 2}},
                "b": {"match_all": {}}
            }}}}
        }),
        10,
    )
    .unwrap();
    let err = match index.search(&expr) {
        Ok(_) => panic!("bad-field knn in a filters agg must error, not return empty buckets"),
        Err(e) => e,
    };
    let msg = err.to_string();
    assert!(
        msg.contains("nope") && msg.contains("unknown"),
        "error should name the unknown field: {msg}"
    );

    cleanup(&path);
}

#[test]
fn filters_agg_sub_aggs_refused() {
    // Honest-refusal: `filters` + sub-aggregations is not yet implemented.
    // The historical `..` match silently dropped `sub_aggs`; the bind now
    // returns an explicit "not yet supported" `Err` rather than pretend to
    // honour them. The sub-agg parses fine in isolation — it is the
    // `filters` + `sub_aggs` *combination* that is refused. Implementing it
    // is deferred to phase-7. See [[code-must-not-lie]].
    let (path, index) = build_index("filters_agg_sub_aggs");
    let expr = parse_search(
        json!({
            "query": {"match_all": {}},
            "aggs": {"f": {
                "filters": {"filters": {"a": {"match_all": {}}}},
                "aggs": {"by_tag": {"terms": {"field": "tag"}}}
            }}
        }),
        10,
    )
    .unwrap();
    let err = match index.search(&expr) {
        Ok(_) => panic!("filters agg with sub_aggs must be refused, not silently dropped"),
        Err(e) => e,
    };
    let msg = err.to_string();
    assert!(
        msg.contains("filters") && msg.contains("not yet supported"),
        "error should explain filters sub-aggs are unsupported: {msg}"
    );

    cleanup(&path);
}

#[test]
fn knn_bad_field_in_nested_inner_hits_errors() {
    // The `inner_hits` re-bind (`collect_inner_hit_specs`) was the third
    // swallow site. A `nested` query whose inner query is a bad-field knn
    // must surface as `Err` — the user must never silently receive empty
    // inner_hits. (With the §4 main-path strictness, the main nested bind
    // catches a field error first; this test pins the end-to-end contract
    // that the combination errors, and the inner_hits `?`-propagation is
    // additionally locked in by type-checking.) See [[feature-knn-query-type]] §4b.
    let (path, index) = build_index("nested_inner_hits_bad_field");
    let expr = parse_search(
        json!({
            "query": {"nested": {
                "path": "items",
                "query": {"knn": {
                    "field": "nope",
                    "query_vector": [1.0, 0.0, 0.0, 0.0],
                    "k": 2
                }},
                "inner_hits": {"name": "matched"}
            }}
        }),
        10,
    )
    .unwrap();
    let err = match index.search(&expr) {
        Ok(_) => panic!("bad-field knn under nested inner_hits must error, not silently empty"),
        Err(e) => e,
    };
    let msg = err.to_string();
    assert!(
        msg.contains("nope") && msg.contains("unknown"),
        "error should name the unknown field: {msg}"
    );

    cleanup(&path);
}

// --- kNN recall survives a segment merge (regression for the
//     merge-persist silent-drop) ---

#[test]
fn knn_recall_survives_segment_merge() {
    // `execute_merge` rewrites the global HNSW resolver in memory but
    // `commit()` persists the vector index *before* `maybe_merge`. Before
    // the fix, the on-disk resolver kept pointing merged docs at the
    // removed source segments; the reader loads that stale copy, so every
    // kNN hit resolving to a merged-away segment was silently dropped.
    // After `force_merge(1)` collapses every segment, that meant *zero*
    // hits returned. This is the unit-scale reproduction of the 600k
    // recall collapse. See [[vector-recall-investigation-audit]] H6.
    let path = test_dir("merge_recall");
    let schema = Mapping::builder()
        .field("embedding", FieldType::dense_vector(4))
        .build();
    let index = Index::create_with_mapping(&path, schema).unwrap();
    index.set_memory_budget(1); // one segment per document

    let n: usize = 16;
    let docs: Vec<_> = (0..n)
        .map(|i| json!({"embedding": [i as f32 + 1.0, 1.0, 0.0, 0.0]}))
        .collect();
    index.bulk(docs).unwrap();
    index.force_merge(1).unwrap(); // collapse all segments -> rewrites resolver

    // Every document must still be retrievable through the global graph;
    // a stale persisted resolver drops the merged docs.
    let results = search(
        &index,
        json!({"query": {"knn": {
            "field": "embedding",
            "query_vector": [1.0, 1.0, 0.0, 0.0],
            "k": n,
            "num_candidates": 100
        }}}),
        n,
    );
    assert_eq!(
        results.len(),
        n,
        "merge left dangling resolver entries: kNN returned {} of {n} hits",
        results.len()
    );

    cleanup(&path);
}