a3s-vec 0.1.8

Native Rust in-process vector database with zvec-compatible capabilities
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
#![allow(clippy::cast_precision_loss)]
use super::IndexRegistry;
use crate::doc::DocumentMap;
use crate::index::ordinals::OrdinalSet;
use crate::{
    CollectionSchema, DataType, Doc, FieldSchema, Fts, HnswQueryParams, IndexParams, MetricType,
    SearchQuery,
};
use roaring::RoaringTreemap;
use std::collections::BTreeSet;
use std::sync::Arc;

fn indexed_schema() -> CollectionSchema {
    indexed_schema_with(
        &IndexParams::hnsw(MetricType::L2, 4, 16).expect("descriptor must be valid"),
    )
}

fn indexed_schema_with(params: &IndexParams) -> CollectionSchema {
    let mut field =
        FieldSchema::new("embedding", DataType::VectorFp32, false, 2).expect("field must be valid");
    field
        .set_index_params(params)
        .expect("ANN index must be supported");
    CollectionSchema::builder("incremental")
        .add_field(field)
        .build()
        .expect("schema must be valid")
}

fn vector_doc(id: &str, vector: &[f32]) -> Arc<Doc> {
    let mut doc = Doc::with_pk(id).expect("document must be valid");
    doc.add_vector_f32("embedding", vector)
        .expect("vector must be valid");
    Arc::new(doc)
}

fn mixed_index_schema() -> CollectionSchema {
    let mut embedding =
        FieldSchema::new("embedding", DataType::VectorFp32, false, 2).expect("field must be valid");
    embedding
        .set_index_params(
            &IndexParams::hnsw(MetricType::L2, 4, 16).expect("HNSW params must be valid"),
        )
        .expect("HNSW index must be valid");
    let mut language =
        FieldSchema::new("language", DataType::String, false, 0).expect("field must be valid");
    language
        .set_index_params(&IndexParams::invert(false, false).expect("scalar params must be valid"))
        .expect("scalar index must be valid");
    let mut body =
        FieldSchema::new("body", DataType::String, false, 0).expect("field must be valid");
    body.set_index_params(
        &IndexParams::fts(Some("standard"), None, None).expect("FTS params must be valid"),
    )
    .expect("FTS index must be valid");
    CollectionSchema::builder("mixed-indexes")
        .add_field(embedding)
        .add_field(language)
        .add_field(body)
        .build()
        .expect("schema must be valid")
}

#[test]
fn targeted_rebuild_preserves_unrelated_ann_generation() {
    let schema = mixed_index_schema();
    let docs: DocumentMap = (0_u16..32)
        .map(|value| {
            let id = format!("doc-{value:03}");
            let mut doc = Doc::with_pk(&id).expect("document must be valid");
            doc.add_vector_f32("embedding", &[f32::from(value), 0.0])
                .expect("vector must be valid");
            doc.add_string("language", if value % 2 == 0 { "rust" } else { "go" })
                .expect("language must be valid");
            doc.add_string("body", &format!("workspace symbol{value}"))
                .expect("body must be valid");
            (id, Arc::new(doc))
        })
        .collect();
    let indexes = IndexRegistry::build(&schema, &docs, 1).expect("indexes must build");
    let initial = indexes.indexes.get("embedding").expect("ANN must exist");

    let scalar = indexes
        .rebuild_field(&schema, &docs, 1, "language")
        .expect("scalar rebuild must succeed");
    let after_scalar = scalar.indexes.get("embedding").expect("ANN must remain");
    assert!(Arc::ptr_eq(&initial.base, &after_scalar.base));

    let fts = scalar
        .rebuild_field(&schema, &docs, 1, "body")
        .expect("FTS rebuild must succeed");
    let after_fts = fts.indexes.get("embedding").expect("ANN must remain");
    assert!(Arc::ptr_eq(&initial.base, &after_fts.base));

    let ann = fts
        .rebuild_field(&schema, &docs, 1, "embedding")
        .expect("ANN rebuild must succeed");
    let rebuilt = ann.indexes.get("embedding").expect("ANN must remain");
    assert!(!Arc::ptr_eq(&initial.base, &rebuilt.base));
}

#[test]
fn indexed_fts_plan_keeps_scores_in_the_ordinal_generation() {
    let schema = mixed_index_schema();
    let docs: DocumentMap = [
        ("doc-z", "workspace rust"),
        ("doc-a", "workspace rust rust"),
        ("doc-m", "unrelated"),
    ]
    .into_iter()
    .map(|(id, body)| {
        let mut doc = Doc::with_pk(id).expect("document must be valid");
        doc.add_vector_f32("embedding", &[0.0, 0.0])
            .expect("vector must be valid");
        doc.add_string("language", "rust")
            .expect("language must be valid");
        doc.add_string("body", body).expect("body must be valid");
        (id.to_string(), Arc::new(doc))
    })
    .collect();
    let indexes = IndexRegistry::build(&schema, &docs, 1).expect("indexes must build");
    let mut fts = Fts::new().expect("FTS payload must be valid");
    fts.set_match_string("workspace rust")
        .expect("FTS expression must be valid");
    let query = SearchQuery::fts("body", &fts, 1).expect("FTS query must be valid");

    let plan = indexes
        .plan_candidates(&docs, 1, &query, None)
        .expect("candidate plan must build");
    let scores = plan.fts_scores.as_ref().expect("FTS scores must exist");

    assert!(plan.selection.is_none());
    assert!(plan.used_fts_index);
    assert_eq!(plan.candidate_count(docs.len()), 2);
    assert_eq!(scores.entries().count(), 1);
}

#[test]
fn stale_generation_declines_selection_for_exact_fallback() {
    let schema = indexed_schema();
    let mut doc = Doc::with_pk("one").expect("document must be valid");
    doc.add_vector_f32("embedding", &[1.0, 0.0])
        .expect("vector must be valid");
    let docs = [("one".to_string(), Arc::new(doc))]
        .into_iter()
        .collect::<DocumentMap>();
    let indexes = IndexRegistry::build(&schema, &docs, 3).expect("index must build");
    let query = SearchQuery::new("embedding", &[1.0, 0.0], 1).expect("query must be valid");
    assert!(indexes
        .candidates(&docs, 4, &query, None)
        .expect("selection must not fail")
        .is_none());
}

#[test]
fn incremental_generation_shares_base_and_shadows_updates_and_deletes() {
    let schema = indexed_schema();
    let docs = [
        ("base-a".to_string(), vector_doc("base-a", &[0.0, 0.0])),
        ("base-b".to_string(), vector_doc("base-b", &[10.0, 0.0])),
    ]
    .into_iter()
    .collect::<DocumentMap>();
    let indexes = IndexRegistry::build(&schema, &docs, 1).expect("index must build");

    let mut scalar_only = docs.clone();
    Arc::make_mut(scalar_only.get_mut("base-a").expect("document must exist"))
        .set_score(1.0)
        .expect("score must be valid");
    let scalar_generation = indexes
        .apply_document_changes(
            &schema,
            &docs,
            &scalar_only,
            2,
            &BTreeSet::from(["base-a".to_string()]),
        )
        .expect("scalar-only generation must publish");
    let initial = indexes.indexes.get("embedding").expect("index must exist");
    let scalar = scalar_generation
        .indexes
        .get("embedding")
        .expect("index must exist");
    assert!(Arc::ptr_eq(&initial.base, &scalar.base));
    assert!(scalar.delta.is_empty());
    assert!(scalar.tombstones.is_empty());

    let mut next_docs = scalar_only.clone();
    next_docs.insert("base-a".to_string(), vector_doc("base-a", &[100.0, 0.0]));
    next_docs.remove("base-b");
    next_docs.insert("delta-c".to_string(), vector_doc("delta-c", &[50.0, 0.0]));
    let changed = BTreeSet::from([
        "base-a".to_string(),
        "base-b".to_string(),
        "delta-c".to_string(),
    ]);
    let next = scalar_generation
        .apply_document_changes(&schema, &scalar_only, &next_docs, 3, &changed)
        .expect("incremental generation must publish");
    let incremental = next.indexes.get("embedding").expect("index must exist");
    assert!(Arc::ptr_eq(&initial.base, &incremental.base));
    assert_eq!(incremental.delta.len(), 2);
    let expected_tombstones: RoaringTreemap = ["base-a", "base-b"]
        .into_iter()
        .filter_map(|id| next.ordinals.ordinal(id))
        .collect();
    assert_eq!(incremental.tombstones, expected_tombstones);

    let mut query = SearchQuery::new("embedding", &[100.0, 0.0], 2).expect("query must be valid");
    query
        .set_hnsw_params(HnswQueryParams::new(2, 0.0, false, true))
        .expect("HNSW controls must be valid");
    let candidates = next
        .candidates(&next_docs, 3, &query, None)
        .expect("candidate selection must succeed")
        .expect("ANN generation must be selected");
    assert_eq!(
        candidates.selection.ids().collect::<BTreeSet<_>>(),
        BTreeSet::from(["base-a", "delta-c"])
    );
    let stat = &next.stats(&schema, &next_docs, 3)[0];
    assert_eq!(stat.document_count, 2);
    assert_eq!(stat.source_revision, 3);
    assert!(stat.estimated_payload_bytes.is_some_and(|bytes| bytes > 0));
}

#[test]
fn incremental_equal_scores_keep_primary_key_order_across_ordinals() {
    let schema = indexed_schema();
    let docs = [("z-base".to_string(), vector_doc("z-base", &[1.0, 0.0]))]
        .into_iter()
        .collect::<DocumentMap>();
    let indexes = IndexRegistry::build(&schema, &docs, 1).expect("index must build");

    let mut next_docs = docs.clone();
    next_docs.insert("a-delta".to_string(), vector_doc("a-delta", &[1.0, 0.0]));
    let next = indexes
        .apply_document_changes(
            &schema,
            &docs,
            &next_docs,
            2,
            &BTreeSet::from(["a-delta".to_string()]),
        )
        .expect("incremental generation must publish");
    assert!(
        next.ordinals.ordinal("z-base").expect("base ordinal")
            < next.ordinals.ordinal("a-delta").expect("delta ordinal")
    );

    let mut query = SearchQuery::new("embedding", &[1.0, 0.0], 1).expect("query must be valid");
    query
        .set_hnsw_params(HnswQueryParams::new(1, 0.0, false, true))
        .expect("HNSW controls must be valid");
    let candidates = next
        .candidates(&next_docs, 2, &query, None)
        .expect("candidate selection must succeed")
        .expect("ANN generation must be selected");
    assert_eq!(
        candidates.selection.ids().collect::<Vec<_>>(),
        vec!["a-delta"]
    );
}

#[test]
fn ordinal_compaction_rebuilds_ann_membership_without_filter_drift() {
    const DOCUMENTS: usize = 513;
    const DELETIONS: usize = 64;

    for params in [
        IndexParams::hnsw(MetricType::L2, 8, 32).expect("HNSW descriptor must be valid"),
        IndexParams::ivf(MetricType::L2, 16, 5, false).expect("IVF descriptor must be valid"),
    ] {
        let schema = indexed_schema_with(&params);
        let docs: DocumentMap = (0..DOCUMENTS)
            .map(|index| {
                let id = format!("doc-{index:04}");
                let coordinate = f32::from(u16::try_from(index).expect("fixture index fits u16"));
                (id.clone(), vector_doc(&id, &[coordinate, 0.0]))
            })
            .collect();
        let indexes = IndexRegistry::build(&schema, &docs, 1).expect("index must build");
        let initial = indexes.indexes.get("embedding").expect("index must exist");

        let mut next_docs = docs.clone();
        let changed: BTreeSet<String> = (0..DELETIONS)
            .map(|index| format!("doc-{index:04}"))
            .collect();
        for id in &changed {
            next_docs.remove(id);
        }
        let next = indexes
            .apply_document_changes(&schema, &docs, &next_docs, 2, &changed)
            .expect("compacted generation must publish");
        let compacted = next.indexes.get("embedding").expect("index must exist");

        // Sixty-four retired ordinals trigger ordinal compaction at this
        // collection size, while the vector overlay threshold is 65.
        assert!(!Arc::ptr_eq(&initial.base, &compacted.base));
        assert!(compacted.delta.is_empty());
        assert!(compacted.tombstones.is_empty());
        assert_eq!(
            compacted.base.vector_ordinals.len(),
            u64::try_from(DOCUMENTS - DELETIONS).expect("fixture size fits u64")
        );

        let allowed_bitmap: RoaringTreemap = (DELETIONS..DOCUMENTS)
            .filter(|index| index % 2 == 0)
            .filter_map(|index| next.ordinals.ordinal(&format!("doc-{index:04}")))
            .collect();
        let allowed = OrdinalSet::new(&next.ordinals, allowed_bitmap);
        let query = SearchQuery::new("embedding", &[512.0, 0.0], 5).expect("query must be valid");
        let candidates = next
            .candidates(&next_docs, 2, &query, Some(&allowed))
            .expect("candidate selection must succeed")
            .expect("ANN generation must remain usable");
        assert!(candidates.selection.count() >= 5);
        assert!(candidates.selection.ids().all(|id| allowed.contains(id)));
    }
}

#[test]
fn exact_unquantized_score_by_ordinal_matches_document_promotion() {
    let schema = indexed_schema();
    let docs: DocumentMap = [
        ("a", [1.0_f32, 0.0]),
        ("b", [0.0_f32, 1.0]),
        ("c", [0.5_f32, 0.5]),
    ]
    .into_iter()
    .map(|(id, vector)| (id.to_string(), vector_doc(id, &vector)))
    .collect();
    let indexes = IndexRegistry::build(&schema, &docs, 1).expect("indexes must build");
    let query = [0.75_f32, 0.25];
    let query_norm =
        f64::from(query[0]) * f64::from(query[0]) + f64::from(query[1]) * f64::from(query[1]);
    let query_norm = query_norm.sqrt();
    for (id, vector) in [
        ("a", [1.0_f32, 0.0]),
        ("b", [0.0_f32, 1.0]),
        ("c", [0.5_f32, 0.5]),
    ] {
        let ordinal = indexes
            .document_ordinal(id)
            .expect("fixture document must have an ordinal");
        let from_index = indexes
            .exact_unquantized_f32_score_at(
                "embedding",
                ordinal,
                &query,
                query_norm,
                MetricType::L2,
            )
            .expect("ordinal score must resolve");
        let from_document = super::quantization::score_dense_with_query_norm(
            &query,
            &vector,
            MetricType::L2,
            query_norm,
        );
        assert_eq!(from_index.to_bits(), from_document.to_bits());
    }
}

#[test]
fn flat_index_returns_exact_topk_candidates() {
    let schema = indexed_schema_with(
        &IndexParams::flat(MetricType::Cosine).expect("Flat params must be valid"),
    );
    let docs: DocumentMap = [
        ("a", [1.0_f32, 0.0]),
        ("b", [0.0_f32, 1.0]),
        ("c", [0.8_f32, 0.2]),
        ("d", [-1.0_f32, 0.0]),
    ]
    .into_iter()
    .map(|(id, vector)| (id.to_string(), vector_doc(id, &vector)))
    .collect();
    let indexes = IndexRegistry::build(&schema, &docs, 1).expect("Flat indexes must build");
    assert!(
        indexes.stats(&schema, &docs, 1).iter().any(|stat| {
            stat.name == "embedding"
                && stat.index_type == crate::types::IndexType::Flat
                && stat.state == "ready"
        }),
        "Flat field must appear as a ready index"
    );
    let query = SearchQuery::new("embedding", &[1.0, 0.0], 2).expect("query must be valid");
    let plan = indexes
        .plan_candidates(&docs, 1, &query, None)
        .expect("Flat plan must succeed");
    assert!(
        !plan.used_ann,
        "Flat must record as exact, not approximate ANN"
    );
    let selection = plan.selection.expect("Flat must return candidate ordinals");
    assert_eq!(selection.count(), 2);
    let ids: Vec<_> = selection.ids().collect();
    assert_eq!(ids, ["a", "c"]);
}

fn flat_schema() -> CollectionSchema {
    indexed_schema_with(&IndexParams::flat(MetricType::Cosine).expect("Flat must be valid"))
}

#[test]
fn flat_overlay_tombstones_still_select_exact_live_candidates() {
    let schema = flat_schema();
    let docs = [
        ("a".to_string(), vector_doc("a", &[1.0, 0.0])),
        ("b".to_string(), vector_doc("b", &[0.0, 1.0])),
        ("c".to_string(), vector_doc("c", &[0.7, 0.3])),
    ]
    .into_iter()
    .collect::<DocumentMap>();
    let indexes = IndexRegistry::build(&schema, &docs, 1).expect("Flat must build");
    let mut overlays = indexes.clone();
    let flat = overlays
        .indexes
        .get_mut("embedding")
        .expect("Flat field must exist");
    let tombstone = overlays
        .ordinals
        .ordinal("b")
        .expect("ordinal for deleted doc");
    flat.tombstones.insert(tombstone);
    // Keep source_revision matched so Flat is selected; overlay forces live scan.
    assert_eq!(flat.source_revision, 1);

    let query = SearchQuery::new("embedding", &[1.0, 0.0], 2).expect("query");
    let candidates = overlays
        .candidates(&docs, 1, &query, None)
        .expect("selection")
        .expect("Flat generation must be selected");
    let ids: BTreeSet<_> = candidates.selection.ids().collect();
    assert_eq!(ids, BTreeSet::from(["a", "c"]));
}

#[test]
fn flat_filtered_allowed_bitmap_uses_live_scan_path() {
    let schema = flat_schema();
    let docs = (0..64)
        .map(|value| {
            let id = format!("d{value:02}");
            (id.clone(), vector_doc(&id, &[value as f32, 0.0]))
        })
        .collect::<DocumentMap>();
    let indexes = IndexRegistry::build(&schema, &docs, 1).expect("Flat must build");
    let mut allowed = RoaringTreemap::new();
    for value in [1_u64, 2, 3, 10, 20, 30] {
        if let Some(ordinal) = indexes.ordinals.ordinal(&format!("d{value:02}")) {
            allowed.insert(ordinal);
        }
    }
    let allowed_set = OrdinalSet::new(&indexes.ordinals, allowed);
    let query = SearchQuery::new("embedding", &[30.0, 0.0], 6).expect("query");
    let candidates = indexes
        .candidates(&docs, 1, &query, Some(&allowed_set))
        .expect("selection")
        .expect("Flat with allow-list");
    let ids: BTreeSet<_> = candidates.selection.ids().collect();
    assert_eq!(
        ids,
        BTreeSet::from(["d01", "d02", "d03", "d10", "d20", "d30"])
    );
}

#[test]
fn rebuild_field_rejects_unknown_and_unsupported_targets() {
    let schema = mixed_index_schema();
    let docs: DocumentMap = (0_u16..8)
        .map(|value| {
            let id = format!("doc-{value}");
            let mut doc = Doc::with_pk(&id).expect("document must be valid");
            doc.add_vector_f32("embedding", &[f32::from(value), 0.0])
                .expect("vector must be valid");
            doc.add_string("language", "rust")
                .expect("language must be valid");
            doc.add_string("body", "workspace")
                .expect("body must be valid");
            (id, Arc::new(doc))
        })
        .collect();
    let indexes = IndexRegistry::build(&schema, &docs, 1).expect("indexes must build");
    assert!(indexes.rebuild_field(&schema, &docs, 1, "missing").is_err());

    // Unindexed scalar field is not a rebuild target.
    let mut schema_with_tag = mixed_index_schema();
    schema_with_tag
        .fields
        .push(FieldSchema::new("tag", DataType::String, false, 0).expect("tag"));
    assert!(indexes
        .rebuild_field(&schema_with_tag, &docs, 1, "tag")
        .is_err());
}

#[test]
fn build_rejects_incomplete_or_nonpositive_ann_construction_params() {
    use serde_json::json;

    let docs: DocumentMap = (0_u16..8)
        .map(|value| {
            let id = format!("doc-{value}");
            (id.clone(), vector_doc(&id, &[f32::from(value), 0.0]))
        })
        .collect();

    let mut schema = indexed_schema();
    schema.vectors[0]
        .index_params
        .as_mut()
        .expect("hnsw")
        .params
        .remove("m");
    assert!(IndexRegistry::build(&schema, &docs, 1).is_err());

    let mut schema = indexed_schema();
    schema.vectors[0]
        .index_params
        .as_mut()
        .expect("hnsw")
        .params
        .insert("ef_construction".into(), json!(0));
    assert!(IndexRegistry::build(&schema, &docs, 1).is_err());

    let mut schema =
        indexed_schema_with(&IndexParams::ivf(MetricType::L2, 4, 1, false).expect("ivf"));
    schema.vectors[0]
        .index_params
        .as_mut()
        .expect("ivf")
        .params
        .remove("use_soar");
    assert!(IndexRegistry::build(&schema, &docs, 1).is_err());

    let mut schema =
        indexed_schema_with(&IndexParams::diskann(MetricType::L2, 8, 16, 0).expect("diskann"));
    schema.vectors[0]
        .index_params
        .as_mut()
        .expect("diskann")
        .params
        .insert("alpha".into(), json!(f64::NAN));
    assert!(IndexRegistry::build(&schema, &docs, 1).is_err());

    let mut schema =
        indexed_schema_with(&IndexParams::vamana(MetricType::L2, 8, 16, 1.2).expect("vamana"));
    schema.vectors[0]
        .index_params
        .as_mut()
        .expect("vamana")
        .params
        .remove("saturate");
    assert!(IndexRegistry::build(&schema, &docs, 1).is_err());

    let mut schema =
        indexed_schema_with(&IndexParams::hnsw_rabitq(MetricType::L2, 8, 32).expect("hnsw-rabitq"));
    schema.vectors[0]
        .index_params
        .as_mut()
        .expect("hnsw-rabitq")
        .params
        .remove("total_bits");
    assert!(IndexRegistry::build(&schema, &docs, 1).is_err());

    let mut schema = indexed_schema_with(
        &IndexParams::ivf_rabitq(MetricType::L2, 8, 4, 32).expect("ivf-rabitq"),
    );
    schema.vectors[0]
        .index_params
        .as_mut()
        .expect("ivf-rabitq")
        .params
        .insert("n_list".into(), json!(0));
    assert!(IndexRegistry::build(&schema, &docs, 1).is_err());
}

#[test]
fn missing_packed_ann_reports_incomplete_stats_and_flat_absent_stays_ready() {
    let schema = indexed_schema();
    let docs: DocumentMap = (0_u16..8)
        .map(|value| {
            let id = format!("doc-{value}");
            (id.clone(), vector_doc(&id, &[f32::from(value), 0.0]))
        })
        .collect();
    let indexes = IndexRegistry::build(&schema, &docs, 1).expect("indexes must build");
    let mut stripped = indexes.clone();
    stripped.indexes.clear();
    let stats = stripped.stats(&schema, &docs, 1);
    assert!(
        stats.iter().any(|stat| {
            stat.name == "embedding"
                && stat.state == "missing"
                && (stat.completeness - 0.0).abs() < f32::EPSILON
        }),
        "absent HNSW must surface as missing, got {stats:?}"
    );

    let flat_schema = flat_schema();
    let flat_docs = docs.clone();
    let flat = IndexRegistry::build(&flat_schema, &flat_docs, 1).expect("flat must build");
    let mut flat_stripped = flat.clone();
    flat_stripped.indexes.clear();
    let flat_stats = flat_stripped.stats(&flat_schema, &flat_docs, 7);
    assert!(
        flat_stats.iter().any(|stat| {
            stat.name == "embedding"
                && stat.index_type == crate::types::IndexType::Flat
                && stat.state == "ready"
                && stat.source_revision == 7
        }),
        "absent packed Flat remains document-scan ready, got {flat_stats:?}"
    );
}

#[test]
fn binary_flat_stats_remain_document_backed() {
    let mut field = FieldSchema::new("bits", DataType::VectorBinary32, false, 32).expect("field");
    field
        .set_index_params(&IndexParams::flat(MetricType::L2).expect("flat"))
        .expect("binary flat");
    let schema = CollectionSchema::builder("binary-flat-stats")
        .add_field(field)
        .build()
        .expect("schema");
    let mut doc = Doc::with_pk("one").expect("doc");
    doc.add_vector_binary32("bits", &[0, 0, 0, 0])
        .expect("binary");
    let docs: DocumentMap = [("one".to_string(), Arc::new(doc))].into_iter().collect();
    let indexes = IndexRegistry::build(&schema, &docs, 3).expect("build");
    let stats = indexes.stats(&schema, &docs, 3);
    assert!(
        stats.iter().any(|stat| {
            stat.name == "bits"
                && stat.index_type == crate::types::IndexType::Flat
                && stat.state == "ready"
                && stat.document_count == 1
        }),
        "binary Flat must stay document-backed ready, got {stats:?}"
    );
}

#[test]
fn vector_index_classification_helpers_are_consistent() {
    use super::{
        builds_packed_vector_index, is_approximate_ann, is_in_memory_vector, is_incremental_vector,
    };
    use crate::types::IndexType;

    assert!(is_in_memory_vector(IndexType::Hnsw));
    assert!(is_in_memory_vector(IndexType::Flat));
    assert!(!is_in_memory_vector(IndexType::Invert));
    assert!(!is_in_memory_vector(IndexType::Fts));

    assert!(builds_packed_vector_index(
        IndexType::Hnsw,
        DataType::VectorFp32
    ));
    assert!(!builds_packed_vector_index(
        IndexType::Flat,
        DataType::VectorBinary32
    ));
    assert!(is_incremental_vector(IndexType::Hnsw, DataType::VectorFp32));
    assert!(!is_incremental_vector(
        IndexType::Flat,
        DataType::VectorFp32
    ));
    assert!(is_approximate_ann(IndexType::Ivf));
    assert!(!is_approximate_ann(IndexType::Flat));
}