laurus 0.10.0

Unified search library for lexical, vector, and semantic retrieval
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
//! Inverted index writer implementation.
//!
//! This module provides the writer for building inverted indexes in schema-less mode.

use std::collections::HashMap;
use std::sync::Arc;

use ahash::{AHashMap, AHashSet};

use crate::analysis::analyzer::analyzer::Analyzer;
use crate::analysis::analyzer::per_field::PerFieldAnalyzer;
use crate::analysis::analyzer::standard::StandardAnalyzer;
use crate::analysis::token::Token;
use crate::error::{LaurusError, Result};
use crate::lexical::core::analyzed::{AnalyzedDocument, AnalyzedTerm};
use crate::lexical::core::document::Document;

use crate::lexical::core::field::FieldOption;
use crate::lexical::index::inverted::IndexMetadata;
use crate::lexical::index::inverted::core::posting::{Posting, TermPostingIndex};
use crate::lexical::index::inverted::segment::SegmentInfo;
use crate::lexical::index::structures::bkd_tree::BKDWriter;
use crate::lexical::index::structures::dictionary::{TermDictionaryBuilder, TermInfo};
use crate::lexical::index::structures::doc_values::DocValuesWriter;
use crate::lexical::writer::LexicalIndexWriter;

use crate::storage::Storage;
use crate::storage::structured::StructWriter;

// ============================================================================
// Inverted index writer implementation
// ============================================================================

/// Inverted index writer configuration.
#[derive(Clone)]
pub struct InvertedIndexWriterConfig {
    /// Maximum number of documents to buffer before flushing to disk.
    pub max_buffered_docs: usize,

    /// Maximum memory usage for buffering (in bytes).
    pub max_buffer_memory: usize,

    /// Segment name prefix.
    pub segment_prefix: String,

    /// Whether to store term positions for phrase queries.
    pub store_term_positions: bool,

    /// Whether to optimize segments after writing.
    pub optimize_segments: bool,

    /// Analyzer for text fields (can be PerFieldAnalyzer for field-specific analysis).
    pub analyzer: Arc<dyn Analyzer>,

    /// Shard ID for this writer.
    pub shard_id: u16,

    /// Field-specific configurations.
    pub fields: HashMap<String, FieldOption>,
}

impl std::fmt::Debug for InvertedIndexWriterConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("InvertedIndexWriterConfig")
            .field("max_buffered_docs", &self.max_buffered_docs)
            .field("max_buffer_memory", &self.max_buffer_memory)
            .field("segment_prefix", &self.segment_prefix)
            .field("store_term_positions", &self.store_term_positions)
            .field("optimize_segments", &self.optimize_segments)
            .field("analyzer", &self.analyzer.name())
            .finish()
    }
}

impl Default for InvertedIndexWriterConfig {
    fn default() -> Self {
        InvertedIndexWriterConfig {
            max_buffered_docs: 10000,
            max_buffer_memory: 64 * 1024 * 1024, // 64MB
            segment_prefix: "segment".to_string(),
            store_term_positions: true,
            optimize_segments: false,
            analyzer: Arc::new(StandardAnalyzer::new().unwrap()),
            shard_id: 0,
            fields: HashMap::new(),
        }
    }
}

/// Statistics about the writing process.
#[derive(Debug, Clone)]
pub struct WriterStats {
    /// Number of documents added.
    pub docs_added: u64,
    /// Number of unique terms indexed.
    pub unique_terms: u64,
    /// Total postings created.
    pub total_postings: u64,
    /// Memory currently used.
    pub memory_used: usize,
    /// Number of segments created.
    pub segments_created: u32,
    /// Number of deleted documents (from persisted segments).
    pub deleted_count: u64,
}

/// Inverted index writer implementation (schema-less mode).
pub struct InvertedIndexWriter {
    /// The storage backend.
    storage: Arc<dyn Storage>,

    /// Writer configuration.
    config: InvertedIndexWriterConfig,

    /// In-memory inverted index being built.
    inverted_index: TermPostingIndex,

    /// Buffered analyzed documents with their assigned doc IDs.
    buffered_docs: Vec<(u64, AnalyzedDocument)>,

    /// Whether [`Self::inverted_index`] / [`Self::doc_values_writer`] are stale
    /// relative to [`Self::buffered_docs`] and need a rebuild before flush.
    ///
    /// Set by [`Self::remove_pending_document`], which drops the doc from
    /// `buffered_docs` (cheap, order-preserving `retain`) but **defers** the
    /// expensive `rebuild_in_memory_index` rather than running it per removal.
    /// The rebuild runs once at flush time (and eagerly on the same-id re-upsert
    /// path), turning an `update × M` over an `N`-doc uncommitted buffer from
    /// `O(M·N)` into `O(M) + O(N)` (Issue #828). While dirty, the in-memory
    /// index still holds the removed doc's postings, so the NRT lookups
    /// ([`Self::find_doc_id_by_term`] / [`Self::find_doc_ids_by_term`]) filter
    /// their results by [`Self::buffered_doc_ids`] (the physically-correct live
    /// set).
    index_dirty: bool,

    /// Membership index of the doc IDs currently in [`Self::buffered_docs`].
    ///
    /// Kept perfectly in sync with `buffered_docs` (every push inserts, every
    /// clear empties it) so [`Self::remove_pending_document`] can answer
    /// "is this id buffered?" in O(1). Without it the upsert path scanned the
    /// whole buffer on every call — `O(N)` per upsert, `O(N²)` over an
    /// `add × N` ingest, since newly assigned doc IDs are never already
    /// buffered yet still paid the full scan (Issue #570).
    buffered_doc_ids: AHashSet<u64>,

    /// DocValues writer for the current segment.
    doc_values_writer: DocValuesWriter,

    /// Document ID counter.
    next_doc_id: u64,

    /// Current segment number.
    current_segment: u32,

    /// Whether the writer is closed.
    closed: bool,

    /// Writer statistics.
    stats: WriterStats,

    /// Base metadata read at startup.
    base_metadata: IndexMetadata,

    /// Last processed WAL sequence number.
    last_wal_seq: u64,

    /// Pending deletions that are not yet reflected in the reader (NRT).
    pending_deletions: std::collections::HashSet<u64>,

    /// Cached `(segment_id, min_doc_id, max_doc_id)` for every committed
    /// segment, mirroring the `*.meta` files on storage (Issue #559 / #864).
    ///
    /// Built from the constructor's existing recovery scan (no extra I/O),
    /// extended in place whenever this writer flushes a segment, and rebuilt
    /// by [`Self::invalidate_segment_cache`] after an external segment
    /// rewrite ([`LexicalStore::optimize`](crate::lexical::store::LexicalStore::optimize)
    /// force-merge — the only path that rewrites segments behind a live
    /// writer; `LexicalStore::commit` drops the writer before it merges).
    /// Lets [`Self::find_segments_for_doc`] answer from memory instead of
    /// listing + JSON-parsing every `.meta` file per upsert.
    segment_ranges: Vec<(String, u64, u64)>,

    /// Highest `max_doc_id` across [`Self::segment_ranges`] (0 when no
    /// segments exist). Fresh doc IDs handed out by the WAL are strictly
    /// greater, so the steady-state ingest path rejects the "is this doc in a
    /// committed segment?" question with one integer compare.
    max_committed_doc_id: u64,

    /// Lazily created [`DeletionManager`](crate::maintenance::deletion::DeletionManager)
    /// reused across upserts of already-committed documents (Issue #571).
    /// Construction loads every `.delmap` bitmap from storage, so building it
    /// once per writer — and only when an overwrite actually needs it —
    /// replaces a full bitmap reload per call. Dropped together with
    /// [`Self::segment_ranges`] on [`Self::invalidate_segment_cache`] because
    /// a force-merge deletes the segments (and bitmaps) it holds in memory.
    ///
    /// Deletion persistence is deferred (Issue #875): the manager buffers
    /// bitmap changes in memory and [`Self::flush_deletions`] group-commits
    /// them (called from [`Self::commit`] and, via
    /// [`LexicalStore::optimize`](crate::lexical::store::LexicalStore::optimize),
    /// before any force-merge).
    deletion_manager: Option<crate::maintenance::deletion::DeletionManager>,

    /// Segments whose `.meta` still advertises `has_deletions = false` but
    /// received their first deletion since the last
    /// [`Self::flush_deletions`] (Issue #875).
    ///
    /// The `has_deletions` flip used to open + JSON-parse the `.meta` file on
    /// **every** delete (and rewrite + fsync it on the first); deferring the
    /// flip removes both from the upsert hot path. Flushed together with the
    /// deletion bitmaps — `.delmap` first, then `.meta` — so a reader that
    /// sees the flag always finds the bitmap.
    pending_meta_deletions: AHashSet<String>,
}

impl std::fmt::Debug for InvertedIndexWriter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("InvertedIndexWriter")
            .field("config", &self.config)
            .field("next_doc_id", &self.next_doc_id)
            .field("current_segment", &self.current_segment)
            .field("closed", &self.closed)
            .field("buffered_docs_count", &self.buffered_docs.len())
            .field("stats", &self.stats)
            .finish()
    }
}

impl InvertedIndexWriter {
    /// Create a new inverted index writer (schema-less mode).
    pub fn new(storage: Arc<dyn Storage>, config: InvertedIndexWriterConfig) -> Result<Self> {
        // Recover state from existing segments. The same scan also seeds the
        // segment-range cache (Issue #559 / #864), so the cache is warm from
        // birth at no extra I/O.
        let mut next_doc_id = 0;
        let mut max_segment_id = -1i32;
        let mut segment_ranges = Vec::new();
        let mut max_committed_doc_id = 0u64;

        if let Ok(files) = storage.list_files() {
            for file in files {
                if file.ends_with(".meta") && file != "index.meta" {
                    // unexpected error handling: ignore malformed files
                    if let Ok(input) = storage.open_input(&file)
                        && let Ok(meta) = serde_json::from_reader::<_, SegmentInfo>(input)
                    {
                        // Only consider segments from the same shard for next_doc_id (local counter part)
                        if meta.shard_id == config.shard_id {
                            let local_id = crate::util::id::get_local_id(meta.max_doc_id);
                            next_doc_id = next_doc_id.max(local_id + 1);
                        }
                        max_segment_id = max_segment_id.max(meta.generation as i32);
                        max_committed_doc_id = max_committed_doc_id.max(meta.max_doc_id);
                        segment_ranges.push((meta.segment_id, meta.min_doc_id, meta.max_doc_id));
                    }
                }
            }
        }

        let current_segment = (max_segment_id + 1) as u32;

        // Create initial DocValuesWriter (will be reset per segment)
        let initial_segment_name = format!("{}_{:06}", config.segment_prefix, current_segment);
        let doc_values_writer = DocValuesWriter::new(storage.clone(), initial_segment_name);

        // Read existing metadata or use default
        let base_metadata =
            crate::lexical::index::inverted::InvertedIndex::read_metadata(storage.as_ref())
                .unwrap_or_else(|_| IndexMetadata::default());

        Ok(InvertedIndexWriter {
            storage,
            config,
            inverted_index: TermPostingIndex::new(),
            buffered_docs: Vec::new(),
            index_dirty: false,
            buffered_doc_ids: AHashSet::new(),
            doc_values_writer,
            next_doc_id,
            current_segment,
            closed: false,
            stats: WriterStats {
                docs_added: 0,
                unique_terms: 0,
                total_postings: 0,
                memory_used: 0,
                segments_created: 0,
                deleted_count: 0,
            },
            last_wal_seq: base_metadata.last_wal_seq,
            base_metadata,
            pending_deletions: std::collections::HashSet::new(),
            segment_ranges,
            max_committed_doc_id,
            deletion_manager: None,
            pending_meta_deletions: AHashSet::new(),
        })
    }

    /// Add a document to the index with automatic ID assignment.
    /// Returns the assigned document ID.
    pub fn add_document(&mut self, doc: Document) -> Result<u64> {
        self.check_closed()?;

        // Schema-less mode: no validation needed
        // Analyze the document
        let analyzed_doc = self.analyze_document(doc)?;

        // Add the analyzed document and return the assigned ID
        self.add_analyzed_document(analyzed_doc)
    }

    /// Upsert a document to the index with a specific document ID.
    pub fn upsert_document(&mut self, doc_id: u64, doc: Document) -> Result<()> {
        self.check_closed()?;

        // Analyze the document
        let analyzed_doc = self.analyze_document(doc)?;

        // Same-id re-upsert detection: if this exact id is already buffered, the
        // deferred-rebuild scheme cannot distinguish the old version's stale
        // postings from the new version's (both carry this id), so we must purge
        // the old version from the in-memory index *before* re-indexing. This
        // never happens on the production engine path (every add gets a fresh
        // monotonic doc_id), so the eager rebuild here costs nothing in
        // production and only restores exact NRT state for direct re-upserts.
        let was_buffered = self.buffered_doc_ids.contains(&doc_id);

        // Upsert: remove any pending document with the same ID before adding
        self.remove_pending_document(doc_id)?;
        // Upsert: mark persisted occurrences as deleted (flushed segments)
        self.mark_persisted_doc_deleted(doc_id)?;

        if was_buffered {
            // Purge the just-removed old version's postings now (eager), so the
            // re-added version below is the only one in the in-memory index.
            self.rebuild_in_memory_index()?;
            self.index_dirty = false;
        }

        // Add the analyzed document with the specified ID
        self.upsert_analyzed_document(doc_id, analyzed_doc)
    }

    /// Add an already analyzed document to the index with a specific document ID.
    pub fn upsert_analyzed_document(
        &mut self,
        doc_id: u64,
        analyzed_doc: AnalyzedDocument,
    ) -> Result<()> {
        self.check_closed()?;

        // Update next_doc_id if necessary to avoid ID collisions
        if doc_id >= self.next_doc_id {
            self.next_doc_id = doc_id + 1;
        }

        // Add field values to DocValues
        for (field_name, value) in &analyzed_doc.stored_fields {
            self.doc_values_writer
                .add_value(doc_id, field_name, value.clone());
        }

        // Add to inverted index
        self.add_analyzed_document_to_index(doc_id, &analyzed_doc)?;

        // Buffer the document with its assigned ID
        self.buffered_docs.push((doc_id, analyzed_doc));
        self.buffered_doc_ids.insert(doc_id);
        self.stats.docs_added += 1;

        // Check if we need to flush
        if self.should_flush() {
            self.flush_segment()?;
        }

        Ok(())
    }

    /// Add an already analyzed document to the index with automatic ID assignment.
    /// Returns the assigned document ID.
    ///
    /// This method allows you to add pre-analyzed documents directly,
    /// bypassing the internal document analysis step. This is useful when:
    /// - You want to use DocumentParser explicitly for better control
    /// - You have pre-tokenized documents from external systems
    /// - You need to customize the analysis process
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use laurus::lexical::core::document::Document;
    /// use laurus::lexical::core::parser::DocumentParser;
    /// use laurus::analysis::analyzer::per_field::PerFieldAnalyzer;
    /// use laurus::analysis::analyzer::standard::StandardAnalyzer;
    /// use laurus::lexical::index::inverted::writer::{InvertedIndexWriter, InvertedIndexWriterConfig};
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::StorageConfig;
    /// use std::sync::Arc;
    ///
    /// let storage = Arc::new(MemoryStorage::new(MemoryStorageConfig::default()));
    /// let per_field = PerFieldAnalyzer::new(Arc::new(StandardAnalyzer::new().unwrap()));
    /// let config = InvertedIndexWriterConfig {
    ///     analyzer: Arc::new(per_field.clone()),
    ///     ..Default::default()
    /// };
    /// let mut writer = InvertedIndexWriter::new(storage, config).unwrap();
    ///
    /// use laurus::lexical::core::field::TextOption;
    /// let doc = Document::builder()
    ///     .add_text("title", "Rust Programming")
    ///     .build();
    ///
    /// let doc_parser = DocumentParser::new(Arc::new(per_field));
    /// let analyzed = doc_parser.parse(doc).unwrap();
    /// let doc_id = writer.add_analyzed_document(analyzed).unwrap();
    /// ```
    pub fn add_analyzed_document(&mut self, analyzed_doc: AnalyzedDocument) -> Result<u64> {
        self.check_closed()?;

        // Assign document ID using shard-prefixed strategy
        let local_id = self.next_doc_id;
        self.next_doc_id += 1;
        let doc_id = crate::util::id::create_doc_id(self.config.shard_id, local_id);

        // Add the analyzed document with the assigned ID
        self.upsert_analyzed_document(doc_id, analyzed_doc)?;

        Ok(doc_id)
    }

    /// Find the internal document ID for a given term (field:value).
    ///
    /// This searches both the in-memory buffer (uncommitted) and, in the future,
    /// persisted segments (committed).
    ///
    /// Currently only searches the in-memory buffer for NRT (Near Real-Time) lookups.
    pub fn find_doc_id_by_term(&self, field: &str, term: &str) -> Result<Option<u64>> {
        let full_term = format!("{field}:{term}");

        // 1. Check in-memory inverted index. The index may hold stale postings
        // for docs removed since the last rebuild (deferred under #828), so keep
        // only ids still in the live buffer and return the highest (latest).
        if let Some(posting_list) = self.inverted_index.get_posting_list(&full_term)
            && let Some(doc_id) = posting_list
                .postings
                .iter()
                .map(|p| p.doc_id)
                .filter(|id| self.buffered_doc_ids.contains(id))
                .max()
        {
            return Ok(Some(doc_id));
        }

        // 2. TODO: Check persisted segments
        // This requires opening readers for existing segments, which is expensive if done on every write.
        // For now, we rely on the upper layer (LexicalStore/HybridEngine) to check committed segments via Readers,
        // and use this method specifically for the "In-Memory / NRT" part of the check.
        // Or we implement a BloomFilter cache for segments here.

        Ok(None)
    }

    /// Find all internal document IDs for a given term (field:value).
    ///
    /// This searches both the in-memory buffer (uncommitted) and, in the future,
    /// persisted segments (committed).
    fn find_doc_ids_by_term(&self, field: &str, term: &str) -> Result<Option<Vec<u64>>> {
        let full_term = format!("{field}:{term}");

        // 1. Check in-memory inverted index. The index may hold stale postings
        // for docs removed since the last rebuild (deferred under #828), so keep
        // only ids still in the live buffer; a same-id re-upsert can also leave
        // two postings for one id, so dedup.
        if let Some(posting_list) = self.inverted_index.get_posting_list(&full_term) {
            let mut seen = AHashSet::new();
            let ids: Vec<u64> = posting_list
                .postings
                .iter()
                .map(|p| p.doc_id)
                .filter(|id| self.buffered_doc_ids.contains(id) && seen.insert(*id))
                .collect();
            if !ids.is_empty() {
                return Ok(Some(ids));
            }
        }

        Ok(None)
    }

    /// Analyze a document into terms.
    fn analyze_document(&mut self, doc: Document) -> Result<AnalyzedDocument> {
        let mut field_terms = AHashMap::new();
        let mut stored_fields = AHashMap::new();
        let mut point_values = AHashMap::new();

        // Process each field in the document
        for (field_name, val) in &doc.fields {
            use crate::data::DataValue;

            // 1. Get field option (Schema-aware)
            // If the field is not in schema, we check if it starts with "_" (internal)
            // Internal fields are indexed/stored by default unless explicitly disabled?
            // For now, let's say if NOT in schema, we follow a "relaxed" schema-less mode:
            // - If config.fields is empty, we index/store everything (backward compat)
            // - If config.fields is NOT empty, we ONLY index/store what's in schema,
            //   plus reserved fields like "_id".
            let option = if let Some(opt) = self.config.fields.get(field_name) {
                Some(opt)
            } else if field_name.starts_with('_') || self.config.fields.is_empty() {
                // If it's an internal field or we are in schema-less mode (empty config)
                None // Uses default behavior below
            } else {
                continue; // Skip fields not in schema
            };

            let (should_index, should_store) = match option {
                Some(FieldOption::Text(opt)) => (opt.indexed, opt.stored),
                Some(FieldOption::Integer(opt)) => (opt.indexed, opt.stored),
                Some(FieldOption::Float(opt)) => (opt.indexed, opt.stored),
                Some(FieldOption::Boolean(opt)) => (opt.indexed, opt.stored),
                Some(FieldOption::DateTime(opt)) => (opt.indexed, opt.stored),
                Some(FieldOption::Geo(opt)) => (opt.indexed, opt.stored),
                Some(FieldOption::Geo3d(opt)) => (opt.indexed, opt.stored),
                Some(FieldOption::Bytes(opt)) => (false, opt.stored), // Bytes are not lexically indexed
                None => (true, true), // Internal or schema-less default
            };

            // Index the field if enabled
            if should_index {
                match val {
                    DataValue::Text(text) => {
                        // Use analyzer from config (can be PerFieldAnalyzer for field-specific analysis)
                        let tokens = if let Some(per_field) = self
                            .config
                            .analyzer
                            .as_any()
                            .downcast_ref::<PerFieldAnalyzer>()
                        {
                            per_field.analyze_field(field_name, text)?
                        } else {
                            self.config.analyzer.analyze(text)?
                        };
                        let token_vec: Vec<Token> = tokens.collect();
                        let analyzed_terms = self.tokens_to_analyzed_terms(token_vec);

                        field_terms.insert(field_name.clone(), analyzed_terms);
                    }

                    DataValue::Int64(num) => {
                        // Convert integer to text for indexing
                        let text = num.to_string();

                        let analyzed_term = AnalyzedTerm {
                            term: text.clone(),
                            position: 0,
                            frequency: 1,
                            offset: (0, text.len()),
                        };

                        field_terms.insert(field_name.clone(), vec![analyzed_term]);
                        point_values.insert(field_name.clone(), vec![vec![*num as f64]]);
                    }
                    DataValue::Float64(num) => {
                        // ...
                        field_terms.insert(
                            field_name.clone(),
                            vec![AnalyzedTerm {
                                term: num.to_string(),
                                position: 0,
                                frequency: 1,
                                offset: (0, num.to_string().len()),
                            }],
                        );
                        point_values.insert(field_name.clone(), vec![vec![*num]]);
                    }
                    DataValue::DateTime(dt) => {
                        let ts = dt.timestamp() as f64;
                        field_terms.insert(
                            field_name.clone(),
                            vec![AnalyzedTerm {
                                term: ts.to_string(),
                                position: 0,
                                frequency: 1,
                                offset: (0, ts.to_string().len()),
                            }],
                        );
                        point_values.insert(field_name.clone(), vec![vec![ts]]);
                    }
                    DataValue::Bool(b) => {
                        // bool is indexed as "true"/"false" text for lexical queries,
                        // and also stored as a point value (1.0/0.0) for numeric range queries
                        let text = if *b { "true" } else { "false" };
                        field_terms.insert(
                            field_name.clone(),
                            vec![AnalyzedTerm {
                                term: text.to_string(),
                                position: 0,
                                frequency: 1,
                                offset: (0, text.len()),
                            }],
                        );
                    }
                    DataValue::Geo(p) => {
                        // Geo points are indexed as 2D points in BKD
                        field_terms.insert(
                            field_name.clone(),
                            vec![AnalyzedTerm {
                                term: format!("{},{}", p.lat, p.lon),
                                position: 0,
                                frequency: 1,
                                offset: (0, format!("{},{}", p.lat, p.lon).len()),
                            }],
                        );
                        point_values.insert(field_name.clone(), vec![vec![p.lat, p.lon]]);
                    }
                    DataValue::GeoEcef(p) => {
                        // 3D ECEF point: emitting a 3-element point here is
                        // what tells `BKDWriter::new` to build a 3D BKD when
                        // the per-field tree is materialized. Schema-side,
                        // `FieldOption::Geo3d` (#298) classifies the field
                        // as lexical and respects the (indexed, stored)
                        // flags exactly like 2D Geo.
                        field_terms.insert(
                            field_name.clone(),
                            vec![AnalyzedTerm {
                                term: format!("{},{},{}", p.x, p.y, p.z),
                                position: 0,
                                frequency: 1,
                                offset: (0, format!("{},{},{}", p.x, p.y, p.z).len()),
                            }],
                        );
                        point_values.insert(field_name.clone(), vec![vec![p.x, p.y, p.z]]);
                    }
                    DataValue::Int64Array(arr) => {
                        // Multi-valued integer field. Each element is a
                        // distinct analyzed term and a distinct 1D BKD
                        // point so range queries match if any value falls
                        // in range (Lucene "any match" semantics).
                        let mut terms: Vec<AnalyzedTerm> = Vec::with_capacity(arr.len());
                        let mut points: Vec<Vec<f64>> = Vec::with_capacity(arr.len());
                        let mut offset = 0usize;
                        for (idx, num) in arr.iter().enumerate() {
                            let text = num.to_string();
                            let len = text.len();
                            terms.push(AnalyzedTerm {
                                term: text,
                                position: idx as u32,
                                frequency: 1,
                                offset: (offset, offset + len),
                            });
                            offset += len + 1;
                            points.push(vec![*num as f64]);
                        }
                        field_terms.insert(field_name.clone(), terms);
                        point_values.insert(field_name.clone(), points);
                    }
                    DataValue::Float64Array(arr) => {
                        // Multi-valued float field. Same shape as Int64Array.
                        let mut terms: Vec<AnalyzedTerm> = Vec::with_capacity(arr.len());
                        let mut points: Vec<Vec<f64>> = Vec::with_capacity(arr.len());
                        let mut offset = 0usize;
                        for (idx, num) in arr.iter().enumerate() {
                            let text = num.to_string();
                            let len = text.len();
                            terms.push(AnalyzedTerm {
                                term: text,
                                position: idx as u32,
                                frequency: 1,
                                offset: (offset, offset + len),
                            });
                            offset += len + 1;
                            points.push(vec![*num]);
                        }
                        field_terms.insert(field_name.clone(), terms);
                        point_values.insert(field_name.clone(), points);
                    }
                    // Handle other variants (Bytes, Vector, Null) — not lexically indexed.
                    _ => {}
                }
            }

            // Store the field if enabled
            if should_store {
                stored_fields.insert(field_name.clone(), val.clone());
            }
        }

        // Calculate field lengths (number of tokens per field)
        let mut field_lengths = AHashMap::new();
        for (field_name, terms) in &field_terms {
            field_lengths.insert(field_name.clone(), terms.len() as u32);
        }

        Ok(AnalyzedDocument {
            field_terms,
            stored_fields,
            field_lengths,
            point_values,
        })
    }

    /// Convert tokens to analyzed terms.
    fn tokens_to_analyzed_terms(&self, tokens: Vec<Token>) -> Vec<AnalyzedTerm> {
        let mut term_frequencies = AHashMap::new();
        let mut analyzed_terms = Vec::new();

        for (position, token) in tokens.into_iter().enumerate() {
            let term = token.text;
            let frequency = term_frequencies.entry(term.clone()).or_insert(0);
            *frequency += 1;

            analyzed_terms.push(AnalyzedTerm {
                term: term.clone(),
                position: position as u32,
                frequency: *frequency,
                offset: (token.start_offset, token.end_offset),
            });
        }

        analyzed_terms
    }

    /// Add an analyzed document to the inverted index.
    fn add_analyzed_document_to_index(
        &mut self,
        doc_id: u64,
        doc: &AnalyzedDocument,
    ) -> Result<()> {
        for (field_name, terms) in &doc.field_terms {
            for analyzed_term in terms {
                let full_term = format!("{field_name}:{}", analyzed_term.term);

                let posting = if self.config.store_term_positions {
                    Posting::with_positions(doc_id, vec![analyzed_term.position])
                } else {
                    Posting::with_frequency(doc_id, analyzed_term.frequency)
                };

                self.inverted_index.add_posting(full_term, posting);
                self.stats.total_postings += 1;
            }
        }

        self.stats.unique_terms = self.inverted_index.term_count();
        Ok(())
    }

    /// Check if we should flush the current segment.
    fn should_flush(&self) -> bool {
        self.buffered_docs.len() >= self.config.max_buffered_docs
            || self.estimate_memory_usage() >= self.config.max_buffer_memory
    }

    /// Estimate current memory usage.
    fn estimate_memory_usage(&self) -> usize {
        // Rough estimation
        let doc_memory = self.buffered_docs.len() * 1024; // 1KB per doc estimate
        let index_memory = self.inverted_index.term_count() as usize * 256; // 256 bytes per term estimate
        doc_memory + index_memory
    }

    /// Flush the current segment to disk.
    fn flush_segment(&mut self) -> Result<()> {
        if self.buffered_docs.is_empty() {
            return Ok(());
        }

        // Materialize any deferred removals (#828) so the in-memory index +
        // DocValues match `buffered_docs` before they are written to disk.
        if self.index_dirty {
            self.rebuild_in_memory_index()?;
            self.index_dirty = false;
        }

        let segment_name = format!("{}_{:06}", self.config.segment_prefix, self.current_segment);

        self.write_segment_files(&segment_name)?;

        // Mirror the freshly written `.meta` into the segment-range cache
        // (Issue #559 / #864) so an overwrite of one of these docs later in
        // this writer's life resolves without rescanning storage.
        self.extend_segment_cache(&segment_name);

        // Clear buffers
        self.buffered_docs.clear();
        self.buffered_doc_ids.clear();
        self.index_dirty = false;
        self.inverted_index = TermPostingIndex::new();

        // Reset DocValuesWriter for next segment
        let next_segment_name = format!(
            "{}_{:06}",
            self.config.segment_prefix,
            self.current_segment + 1
        );
        self.doc_values_writer = DocValuesWriter::new(self.storage.clone(), next_segment_name);

        self.current_segment += 1;
        self.stats.segments_created += 1;

        Ok(())
    }

    /// Write all per-segment files for the currently buffered documents under
    /// `segment_name`, returning the written file paths.
    ///
    /// Shared by the normal flush path ([`Self::flush_segment`]) and the merge
    /// path ([`Self::flush_buffered_to_segment`], Issue #753) so both produce an
    /// identical, complete, typed segment. Does not touch buffers or the
    /// auto-naming counter — callers manage those.
    ///
    /// # Arguments
    ///
    /// * `segment_name` - Name the segment's files are written under.
    fn write_segment_files(&self, segment_name: &str) -> Result<Vec<String>> {
        self.write_inverted_index(segment_name)?;
        self.write_stored_documents(segment_name)?;
        self.write_field_lengths(segment_name)?;
        self.write_field_stats(segment_name)?;
        self.write_doc_values(segment_name)?;
        self.write_segment_metadata(segment_name)?;
        self.write_bkd_trees(segment_name)?;
        // The redundant `.json` stored-field mirror is no longer written
        // (Issue #756); stored fields are read from the typed `.docs` file.

        // Collect every file written under this segment prefix (the BKD step
        // writes one `.{field}.bkd` per numeric/geo field, so enumerate rather
        // than hard-code the set).
        let prefix = format!("{segment_name}.");
        let paths = self
            .storage
            .list_files()?
            .into_iter()
            .filter(|f| f.starts_with(&prefix))
            .collect();
        Ok(paths)
    }

    /// Flush the currently buffered documents to a caller-named segment,
    /// returning the written file paths (Issue #753).
    ///
    /// Unlike [`Self::flush_segment`], the segment name is supplied by the
    /// caller (the merge engine names the merged segment) and the auto-naming
    /// counter is left untouched. Buffers are cleared afterwards so the writer
    /// can be dropped. Returns an empty vector when nothing is buffered.
    ///
    /// # Arguments
    ///
    /// * `segment_name` - Name the merged segment's files are written under.
    pub fn flush_buffered_to_segment(&mut self, segment_name: &str) -> Result<Vec<String>> {
        if self.buffered_docs.is_empty() {
            return Ok(Vec::new());
        }
        // Materialize any deferred removals (#828) before writing. The merge
        // path only adds distinct, pre-deduped docs so this is normally a no-op,
        // but the guard keeps the invariant if that ever changes.
        if self.index_dirty {
            self.rebuild_in_memory_index()?;
            self.index_dirty = false;
        }
        let paths = self.write_segment_files(segment_name)?;
        self.extend_segment_cache(segment_name);
        self.buffered_docs.clear();
        self.buffered_doc_ids.clear();
        self.index_dirty = false;
        self.inverted_index = TermPostingIndex::new();
        self.stats.segments_created += 1;
        Ok(paths)
    }

    /// Write the inverted index to storage.
    fn write_inverted_index(&self, segment_name: &str) -> Result<()> {
        // Write posting lists
        let posting_file = format!("{segment_name}.post");
        let posting_output = self.storage.create_output(&posting_file)?;
        let mut posting_writer = StructWriter::new(posting_output);

        let mut term_dict_builder = TermDictionaryBuilder::new();

        // Compute per-field average length and a `doc_id → field_lengths`
        // lookup once per flush. Both are needed to precompute the
        // tightened BM25 TF-component upper bound stored as
        // `TermInfo::max_score_factor` (#403 PR-B2). The factor uses
        // the default BM25 parameters (`k1 = 1.2`, `b = 0.75`) — at
        // search time, scorers fall back to the loose `k1 + 1` ceiling
        // when the caller overrides `(k1, b)`.
        let field_avg_lengths = self.compute_field_avg_lengths();
        let field_lengths_by_doc: AHashMap<u64, &AHashMap<String, u32>> = self
            .buffered_docs
            .iter()
            .map(|(doc_id, doc)| (*doc_id, &doc.field_lengths))
            .collect();

        // Collect and sort terms for deterministic output
        let mut terms: Vec<_> = self.inverted_index.terms().collect();
        terms.sort();

        for term in terms {
            if let Some(posting_list) = self.inverted_index.get_posting_list(term) {
                let start_offset = posting_writer.position();

                // Write posting list (v2: includes on-disk multi-level
                // skip table for O(log_8 N) `skip_to`, #503).
                posting_list.encode_v2(&mut posting_writer)?;

                let end_offset = posting_writer.position();
                let length = end_offset - start_offset;

                // The internal term key is `"<field>:<term>"`; split on
                // the first `:` to recover the field name. Terms that
                // somehow lack a field prefix get a `0.0` factor and
                // the BM25 scorer will fall back to the loose bound.
                let (max_score_factor, block_max) = match term.split_once(':') {
                    Some((field_name, _)) => {
                        let avg_len = field_avg_lengths
                            .get(field_name)
                            .copied()
                            .unwrap_or(0.0_f32);
                        let term_factor = Self::compute_term_max_score_factor(
                            posting_list,
                            field_name,
                            avg_len,
                            &field_lengths_by_doc,
                        );
                        // Compute per-block max-impact metadata for
                        // Block-Max-WAND (#403 PR-C). A term with no
                        // postings naturally produces no blocks.
                        let blocks = Self::compute_term_block_max(
                            posting_list,
                            field_name,
                            avg_len,
                            &field_lengths_by_doc,
                        );
                        (term_factor, blocks)
                    }
                    None => (0.0_f32, Vec::new()),
                };

                // Add to term dictionary
                let term_info = TermInfo::with_block_max(
                    start_offset,
                    length,
                    posting_list.doc_frequency,
                    posting_list.total_frequency,
                    max_score_factor,
                    block_max,
                );
                term_dict_builder.add_term(term.clone(), term_info);
            }
        }

        posting_writer.close()?;

        // Write term dictionary
        let dict_file = format!("{segment_name}.dict");
        let dict_output = self.storage.create_output(&dict_file)?;
        let mut dict_writer = StructWriter::new(dict_output);

        let term_dict = term_dict_builder.build()?;
        term_dict.write_to_storage(&mut dict_writer)?;
        dict_writer.close()?;

        Ok(())
    }

    /// Compute per-field average field length over the currently
    /// buffered documents. Used by [`Self::write_inverted_index`] to
    /// precompute the tightened BM25 TF-component bound that lands in
    /// each term's `TermInfo::max_score_factor` (#403 PR-B2).
    fn compute_field_avg_lengths(&self) -> AHashMap<String, f32> {
        let mut totals: AHashMap<String, (u64, u64)> = AHashMap::new();
        for (_doc_id, doc) in &self.buffered_docs {
            for (field_name, &length) in &doc.field_lengths {
                let entry = totals.entry(field_name.clone()).or_insert((0, 0));
                entry.0 += 1; // doc count contributing to this field
                entry.1 += length as u64; // total length for this field
            }
        }
        totals
            .into_iter()
            .map(|(field, (count, total))| {
                let avg = if count > 0 {
                    total as f32 / count as f32
                } else {
                    0.0
                };
                (field, avg)
            })
            .collect()
    }

    /// Compute the per-term tightened BM25 TF-component upper bound
    /// using the default `k1 = 1.2`, `b = 0.75` parameters and the
    /// segment's average field length (#403 PR-B2).
    ///
    /// Returns the maximum of
    /// `(tf · (k1 + 1)) / (tf + k1 · (1 - b + b · (L / avg_L)))`
    /// taken over every posting in `posting_list`. `0.0` if the
    /// posting list is empty or no doc lengths are resolvable.
    fn compute_term_max_score_factor(
        posting_list: &crate::lexical::index::inverted::core::posting::PostingList,
        field_name: &str,
        avg_field_length: f32,
        field_lengths_by_doc: &AHashMap<u64, &AHashMap<String, u32>>,
    ) -> f32 {
        const K1: f32 = 1.2;
        const B: f32 = 0.75;

        let mut max_factor: f32 = 0.0;
        for posting in &posting_list.postings {
            let tf = posting.frequency as f32;
            if tf == 0.0 {
                continue;
            }
            let field_len = field_lengths_by_doc
                .get(&posting.doc_id)
                .and_then(|fls| fls.get(field_name))
                .copied()
                .unwrap_or(0) as f32;
            let len_ratio = if avg_field_length > 0.0 {
                field_len / avg_field_length
            } else {
                1.0
            };
            let denom = tf + K1 * (1.0 - B + B * len_ratio);
            if denom == 0.0 {
                continue;
            }
            let factor = (tf * (K1 + 1.0)) / denom;
            if factor > max_factor {
                max_factor = factor;
            }
        }
        max_factor
    }

    /// Compute the per-block max-impact metadata used by Block-Max-WAND
    /// (#403 PR-C). Walks the posting list in
    /// [`BLOCK_SIZE`](crate::lexical::index::structures::dictionary::BLOCK_SIZE)-wide
    /// chunks and records `(last_doc_id, max_factor)` for each block.
    ///
    /// `max_factor` is computed with the same formula as
    /// [`Self::compute_term_max_score_factor`] but restricted to the
    /// block's postings; the per-term factor is the max over all per-
    /// block factors.
    fn compute_term_block_max(
        posting_list: &crate::lexical::index::inverted::core::posting::PostingList,
        field_name: &str,
        avg_field_length: f32,
        field_lengths_by_doc: &AHashMap<u64, &AHashMap<String, u32>>,
    ) -> Vec<crate::lexical::index::structures::dictionary::BlockMax> {
        use crate::lexical::index::structures::dictionary::{BLOCK_SIZE, BlockMax};

        const K1: f32 = 1.2;
        const B: f32 = 0.75;

        if posting_list.postings.is_empty() {
            return Vec::new();
        }

        let mut blocks = Vec::with_capacity(posting_list.postings.len().div_ceil(BLOCK_SIZE));
        for chunk in posting_list.postings.chunks(BLOCK_SIZE) {
            let mut block_max: f32 = 0.0;
            for posting in chunk {
                let tf = posting.frequency as f32;
                if tf == 0.0 {
                    continue;
                }
                let field_len = field_lengths_by_doc
                    .get(&posting.doc_id)
                    .and_then(|fls| fls.get(field_name))
                    .copied()
                    .unwrap_or(0) as f32;
                let len_ratio = if avg_field_length > 0.0 {
                    field_len / avg_field_length
                } else {
                    1.0
                };
                let denom = tf + K1 * (1.0 - B + B * len_ratio);
                if denom == 0.0 {
                    continue;
                }
                let factor = (tf * (K1 + 1.0)) / denom;
                if factor > block_max {
                    block_max = factor;
                }
            }
            // `chunks` always yields at least one element (we early-
            // returned on empty above), so unwrap is safe.
            let last_doc_id = chunk.last().unwrap().doc_id;
            blocks.push(BlockMax {
                last_doc_id,
                max_factor: block_max,
            });
        }
        blocks
    }

    /// Write stored documents to storage with type information preserved.
    fn write_stored_documents(&self, segment_name: &str) -> Result<()> {
        let stored_file = format!("{segment_name}.docs");
        let stored_output = self.storage.create_output(&stored_file)?;
        let mut stored_writer = StructWriter::new(stored_output);

        // Write document count
        stored_writer.write_varint(self.buffered_docs.len() as u64)?;

        // Write each document
        for (doc_id, doc) in &self.buffered_docs {
            stored_writer.write_u64(*doc_id)?;
            stored_writer.write_varint(doc.stored_fields.len() as u64)?;

            for (field_name, field_value) in &doc.stored_fields {
                stored_writer.write_string(field_name)?;

                // Write type tag and value
                match field_value {
                    crate::data::DataValue::Text(text) => {
                        stored_writer.write_u8(0)?; // Type tag for Text
                        stored_writer.write_string(text)?;
                    }
                    crate::data::DataValue::Int64(num) => {
                        stored_writer.write_u8(1)?; // Type tag for Integer
                        stored_writer.write_u64(*num as u64)?; // Store as u64, preserving bit pattern
                    }
                    crate::data::DataValue::Float64(num) => {
                        stored_writer.write_u8(2)?; // Type tag for Float
                        stored_writer.write_f64(*num)?;
                    }
                    crate::data::DataValue::Bool(b) => {
                        stored_writer.write_u8(3)?; // Type tag for Boolean
                        stored_writer.write_u8(if *b { 1 } else { 0 })?;
                    }
                    crate::data::DataValue::DateTime(dt) => {
                        stored_writer.write_u8(5)?; // Type tag for DateTime
                        stored_writer.write_string(&dt.to_rfc3339())?;
                    }
                    crate::data::DataValue::Geo(p) => {
                        stored_writer.write_u8(6)?; // Type tag for Geo
                        stored_writer.write_f64(p.lat)?;
                        stored_writer.write_f64(p.lon)?;
                    }
                    crate::data::DataValue::GeoEcef(p) => {
                        // Type tag 12 = 3D ECEF point. Tag 11 was originally
                        // claimed for ECEF in #297 but collided with the
                        // pre-existing Float64Array tag (also 11); #299
                        // moves ECEF to tag 12 and wires reader support.
                        stored_writer.write_u8(12)?;
                        stored_writer.write_f64(p.x)?;
                        stored_writer.write_f64(p.y)?;
                        stored_writer.write_f64(p.z)?;
                    }
                    crate::data::DataValue::Bytes(bytes, mime) => {
                        stored_writer.write_u8(4)?; // Type tag for Bytes
                        stored_writer.write_string(mime.as_deref().unwrap_or(""))?;
                        stored_writer.write_varint(bytes.len() as u64)?;
                        stored_writer.write_bytes(bytes)?;
                    }
                    crate::data::DataValue::Null => {
                        stored_writer.write_u8(7)?; // Type tag for Null
                    }
                    crate::data::DataValue::Vector(v) => {
                        stored_writer.write_u8(9)?; // Type tag for Vector
                        stored_writer.write_varint(v.len() as u64)?;
                        for &f in v {
                            stored_writer.write_f32(f)?;
                        }
                    }
                    crate::data::DataValue::Int64Array(arr) => {
                        stored_writer.write_u8(10)?; // Type tag for Int64Array
                        stored_writer.write_varint(arr.len() as u64)?;
                        for &v in arr {
                            stored_writer.write_u64(v as u64)?;
                        }
                    }
                    crate::data::DataValue::Float64Array(arr) => {
                        stored_writer.write_u8(11)?; // Type tag for Float64Array
                        stored_writer.write_varint(arr.len() as u64)?;
                        for &v in arr {
                            stored_writer.write_f64(v)?;
                        }
                    }
                }
            }
        }

        stored_writer.close()?;
        Ok(())
    }

    /// Calculate field statistics from buffered documents.
    fn calculate_field_stats(&self) -> AHashMap<String, (u64, f64, u64, u64)> {
        // field_name -> (doc_count, total_length, min_length, max_length)
        let mut field_stats: AHashMap<String, (u64, u64, u64, u64)> = AHashMap::new();

        for (_doc_id, doc) in &self.buffered_docs {
            for (field_name, &length) in &doc.field_lengths {
                let stats = field_stats
                    .entry(field_name.clone())
                    .or_insert((0, 0, u64::MAX, 0));
                stats.0 += 1; // doc_count
                stats.1 += length as u64; // total_length
                stats.2 = stats.2.min(length as u64); // min_length
                stats.3 = stats.3.max(length as u64); // max_length
            }
        }

        // Convert to (doc_count, avg_length, min_length, max_length)
        field_stats
            .into_iter()
            .map(
                |(field, (doc_count, total_length, min_length, max_length))| {
                    let avg_length = if doc_count > 0 {
                        total_length as f64 / doc_count as f64
                    } else {
                        0.0
                    };
                    (field, (doc_count, avg_length, min_length, max_length))
                },
            )
            .collect()
    }

    /// Write field lengths to storage.
    fn write_field_lengths(&self, segment_name: &str) -> Result<()> {
        let lens_file = format!("{segment_name}.lens");
        let lens_output = self.storage.create_output(&lens_file)?;
        let mut lens_writer = StructWriter::new(lens_output);

        // Write document count
        lens_writer.write_varint(self.buffered_docs.len() as u64)?;

        // Write field lengths for each document
        for (doc_id, doc) in &self.buffered_docs {
            lens_writer.write_u64(*doc_id)?;
            lens_writer.write_varint(doc.field_lengths.len() as u64)?;

            for (field_name, length) in &doc.field_lengths {
                lens_writer.write_string(field_name)?;
                lens_writer.write_u32(*length)?;
            }
        }

        lens_writer.close()?;
        Ok(())
    }

    /// Write field statistics to storage.
    fn write_field_stats(&self, segment_name: &str) -> Result<()> {
        let fstats_file = format!("{segment_name}.fstats");
        let fstats_output = self.storage.create_output(&fstats_file)?;
        let mut fstats_writer = StructWriter::new(fstats_output);

        let field_stats = self.calculate_field_stats();

        // Write number of fields
        fstats_writer.write_varint(field_stats.len() as u64)?;

        for (field_name, (doc_count, avg_length, min_length, max_length)) in field_stats {
            fstats_writer.write_string(&field_name)?;
            fstats_writer.write_u64(doc_count)?;
            fstats_writer.write_f64(avg_length)?;
            fstats_writer.write_u64(min_length)?;
            fstats_writer.write_u64(max_length)?;
        }

        fstats_writer.close()?;
        Ok(())
    }

    /// Write DocValues to storage.
    fn write_doc_values(&self, segment_name: &str) -> Result<()> {
        // Write under the caller-supplied segment name. On the normal flush
        // path this equals the writer's own `segment_name`; the merge path
        // (Issue #753) passes the merged segment's name so accumulated values
        // land in the right `.dv` file.
        self.doc_values_writer.write_to(segment_name)?;
        Ok(())
    }

    /// Write BKD trees for numeric and geo fields.
    ///
    /// Per-field state is accumulated into flat row-major coordinate buffers
    /// (`points`) and parallel doc-id buffers (`doc_ids`) so the BKD writer
    /// can be fed without re-allocating per point. The dimensionality is
    /// captured from the first point seen for each field.
    fn write_bkd_trees(&self, segment_name: &str) -> Result<()> {
        // (flat points, doc_ids, num_dims)
        let mut field_buckets: AHashMap<String, (Vec<f64>, Vec<u64>, usize)> = AHashMap::new();

        for (doc_id, doc) in &self.buffered_docs {
            for (field, points) in &doc.point_values {
                // Each `point` is one BKD entry. A single-valued field
                // contributes one entry; a multi-valued field contributes
                // one per element. The BKD reader's `range_search` already
                // deduplicates `doc_id`s, so a multi-valued document is
                // reported at most once per query.
                for point in points {
                    let bucket = field_buckets
                        .entry(field.clone())
                        .or_insert_with(|| (Vec::new(), Vec::new(), point.len()));
                    bucket.0.extend_from_slice(point);
                    bucket.1.push(*doc_id);
                }
            }
        }

        for (field, (points, doc_ids, num_dims)) in field_buckets {
            if doc_ids.is_empty() {
                continue;
            }

            let file_name = format!("{segment_name}.{field}.bkd");
            let output = self.storage.create_output(&file_name)?;
            let mut writer = BKDWriter::new(output, num_dims as u32);
            writer.write(&points, &doc_ids)?;
            writer.finish()?;
        }
        Ok(())
    }

    /// Doc-ID range `(min, max)` of the currently buffered documents,
    /// `(0, 0)` when the buffer is empty. Shared by
    /// [`Self::write_segment_metadata`] and [`Self::extend_segment_cache`] so
    /// the on-storage `.meta` and the in-memory segment-range cache can never
    /// disagree.
    fn buffered_doc_id_range(&self) -> (u64, u64) {
        let min_id = self
            .buffered_docs
            .iter()
            .map(|(id, _)| *id)
            .min()
            .unwrap_or(0);
        let max_id = self
            .buffered_docs
            .iter()
            .map(|(id, _)| *id)
            .max()
            .unwrap_or(0);
        (min_id, max_id)
    }

    /// Append the segment just written from the current buffer to
    /// [`Self::segment_ranges`] and raise [`Self::max_committed_doc_id`]
    /// (Issue #559 / #864). Callers must invoke this after
    /// [`Self::write_segment_files`] and **before** clearing the buffers the
    /// range is computed from.
    ///
    /// # Parameters
    ///
    /// - `segment_name` - Name the segment's files were written under.
    fn extend_segment_cache(&mut self, segment_name: &str) {
        let (min_id, max_id) = self.buffered_doc_id_range();
        self.max_committed_doc_id = self.max_committed_doc_id.max(max_id);
        self.segment_ranges
            .push((segment_name.to_string(), min_id, max_id));
    }

    /// Write segment metadata.
    fn write_segment_metadata(&self, segment_name: &str) -> Result<()> {
        let (min_id, max_id) = self.buffered_doc_id_range();

        // Create SegmentInfo
        let info = SegmentInfo {
            segment_id: segment_name.to_string(),
            doc_count: self.buffered_docs.len() as u64,
            min_doc_id: min_id,
            max_doc_id: max_id,
            generation: self.current_segment as u64,
            has_deletions: false, // New segments initially have no deletions
            shard_id: self.config.shard_id,
        };

        // Write as JSON for compatibility with InvertedIndex::load_segments()
        let meta_file = format!("{segment_name}.meta");
        let json_data = serde_json::to_string_pretty(&info).map_err(|e| {
            LaurusError::index(format!("Failed to serialize segment metadata: {e}"))
        })?;

        let mut output = self.storage.create_output(&meta_file)?;
        std::io::Write::write_all(&mut output, json_data.as_bytes())?;
        output.close()?;

        Ok(())
    }

    /// Commit all pending changes.
    pub fn commit(&mut self) -> Result<()> {
        self.check_closed()?;

        // Flush any remaining documents
        if !self.buffered_docs.is_empty() {
            self.flush_segment()?;
        }

        // Group-commit buffered deletion state (Issue #875). This must run
        // BEFORE `write_metadata_json` persists `last_wal_seq`: once the
        // checkpoint is durable, WAL replay skips the delete records, so a
        // crash between the checkpoint and a later deletion flush would lose
        // the deletions permanently. In this order a mid-commit crash keeps
        // the records replayable. It also runs before `LexicalStore::commit`'s
        // `maybe_merge`, so the merge engine always sees the deletions.
        self.flush_deletions()?;

        // Write index metadata
        self.write_index_metadata()?;
        self.write_metadata_json()?;

        Ok(())
    }

    /// Write global index metadata.
    fn write_index_metadata(&self) -> Result<()> {
        let meta_output = self.storage.create_output("index.meta")?;
        let mut meta_writer = StructWriter::new(meta_output);

        meta_writer.write_u32(0x494D4554)?; // Magic "IMET"
        meta_writer.write_u32(1)?; // Version
        meta_writer.write_u64(crate::util::time::now_secs())?; // Timestamp
        meta_writer.write_u64(self.stats.docs_added)?;
        meta_writer.write_u32(self.stats.segments_created)?;

        meta_writer.close()?;
        Ok(())
    }

    /// Write metadata.json (used by InvertedIndex).
    fn write_metadata_json(&self) -> Result<()> {
        let mut meta = self.base_metadata.clone();
        meta.doc_count += self.stats.docs_added;
        meta.deleted_count += self.stats.deleted_count;
        meta.modified = crate::util::time::now_secs();
        meta.generation += 1; // Increment generation
        meta.last_wal_seq = self.last_wal_seq;

        let metadata_json = serde_json::to_string_pretty(&meta)
            .map_err(|e| LaurusError::index(format!("Failed to serialize metadata: {e}")))?;

        let mut output = self.storage.create_output("metadata.json")?;
        std::io::Write::write_all(&mut output, metadata_json.as_bytes())?;
        output.close()?;
        Ok(())
    }

    /// Rollback all pending changes.
    ///
    /// Buffered deletion state (deferred bitmap writes and `has_deletions`
    /// meta flips, Issue #875) is intentionally NOT discarded: deletions were
    /// never rollback-able (they used to be persisted synchronously at delete
    /// time) and their WAL records are already acknowledged, so they are kept
    /// and persisted by the next [`Self::flush_deletions`].
    pub fn rollback(&mut self) -> Result<()> {
        self.check_closed()?;

        // Clear all buffers
        self.buffered_docs.clear();
        self.buffered_doc_ids.clear();
        self.index_dirty = false;
        self.inverted_index = TermPostingIndex::new();

        Ok(())
    }

    /// Get writer statistics.
    pub fn stats(&self) -> &WriterStats {
        &self.stats
    }

    /// Close the writer.
    pub fn close(&mut self) -> Result<()> {
        if !self.closed {
            self.commit()?;
            self.closed = true;
        }
        Ok(())
    }

    /// Check if the writer is closed.
    fn check_closed(&self) -> Result<()> {
        if self.closed {
            Err(LaurusError::index("Writer is closed"))
        } else {
            Ok(())
        }
    }

    /// Get the number of pending documents.
    pub fn pending_docs(&self) -> usize {
        self.buffered_docs.len()
    }

    /// Check if the writer is closed.
    pub fn is_closed(&self) -> bool {
        self.closed
    }

    /// Remove a pending document with the given ID from in-memory buffers and rebuild indices.
    fn remove_pending_document(&mut self, doc_id: u64) -> Result<()> {
        // Fast path: nothing buffered.
        if self.buffered_docs.is_empty() {
            return Ok(());
        }

        // O(1) membership probe. Newly assigned doc IDs (the common `add`
        // case) are never already buffered, so this returns early and skips
        // the full-buffer scan + index rebuild below — turning the per-upsert
        // cost from O(N) into O(1) and the `add × N` ingest from O(N²) into
        // O(N) (Issue #570). `remove` also drops the id, keeping the set in
        // sync with the `retain` that follows.
        if !self.buffered_doc_ids.remove(&doc_id) {
            return Ok(());
        }

        // The id was buffered: drop it from the buffer (cheap, order-preserving
        // so postings stay doc-id-ascending for the skip-table encode) and
        // **defer** the expensive in-memory index / DocValues rebuild. The
        // rebuild runs once at flush time (and eagerly on the same-id re-upsert
        // path in `upsert_document`), so updating M docs in an N-doc uncommitted
        // buffer is O(M)+O(N) instead of O(M·N) (Issue #828). Until the rebuild,
        // the in-memory index still holds this doc's postings; the NRT lookups
        // filter them out via `buffered_doc_ids`.
        self.buffered_docs.retain(|(id, _)| *id != doc_id);
        self.index_dirty = true;

        // Decrement docs_added for the removed (un-done) document.
        if self.stats.docs_added > 0 {
            self.stats.docs_added -= 1;
        }
        Ok(())
    }

    /// Rebuild the in-memory index and DocValues from buffered docs (used after removals).
    fn rebuild_in_memory_index(&mut self) -> Result<()> {
        // Reset structures
        self.inverted_index = TermPostingIndex::new();
        let segment_name = format!("{}_{:06}", self.config.segment_prefix, self.current_segment);
        self.doc_values_writer = DocValuesWriter::new(self.storage.clone(), segment_name);

        // Reset stats counters that depend on buffered content
        // Do NOT reset docs_added here, as it includes flushed docs.
        // docs_added is adjusted in remove_pending_document directly.
        self.stats.unique_terms = 0;
        self.stats.total_postings = 0;

        // Re-add all buffered analyzed docs
        let buffered_snapshot = self.buffered_docs.clone();
        for (id, analyzed_doc) in buffered_snapshot {
            // Re-add stored fields to DocValues
            for (field_name, value) in &analyzed_doc.stored_fields {
                self.doc_values_writer
                    .add_value(id, field_name, value.clone());
            }

            // Re-add postings
            self.add_analyzed_document_to_index(id, &analyzed_doc)?;
            // stats.docs_added is ALREADY accounting for these docs (except the one removed)
        }

        Ok(())
    }

    /// Mark a persisted document as deleted.
    ///
    /// This updates the deletion bitmap for the segment containing the document.
    fn mark_persisted_doc_deleted(&mut self, doc_id: u64) -> Result<()> {
        let segments = self.find_segments_for_doc(doc_id)?;

        if !segments.is_empty() {
            // Create the deletion manager on first use and keep it for the
            // writer's lifetime (Issue #571): construction reloads every
            // `.delmap` bitmap from storage, and the fresh-id ingest path
            // (empty `segments`) never needs it at all.
            if self.deletion_manager.is_none() {
                // The deletion log is disabled (Issue #875): nothing replays
                // it — crash recovery is fully covered by the engine WAL,
                // whose delete records precede every index mutation — so its
                // per-delete append + fsync is pure overhead.
                let config = crate::maintenance::deletion::DeletionConfig {
                    enable_deletion_log: false,
                    ..Default::default()
                };
                self.deletion_manager = Some(crate::maintenance::deletion::DeletionManager::new(
                    config,
                    self.storage.clone(),
                )?);
            }
            let mut deleted = 0;
            for (segment_id, min_doc_id, max_doc_id) in &segments {
                let manager = self.deletion_manager.as_ref().ok_or_else(|| {
                    LaurusError::internal("deletion manager missing after initialization")
                })?;

                manager.initialize_segment(segment_id, *min_doc_id, *max_doc_id)?;

                let delete_result = manager.delete_document(segment_id, doc_id, "upsert");
                if delete_result.is_err() {
                    // If initializing failed (e.g. bitmap corrupted), try force re-init
                    // In production code we should be more careful, but here we prioritize consistency
                    manager.initialize_segment(segment_id, *min_doc_id, *max_doc_id)?;
                    manager.delete_document(segment_id, doc_id, "upsert")?;
                }

                // Queue the segment's `has_deletions` meta flip; persisted by
                // `flush_deletions` at commit (Issue #875).
                self.update_segment_meta_deletions(segment_id);

                deleted += 1;
            }
            // Track globally
            self.stats.deleted_count += deleted;
        }

        // Add to pending deletions for NRT visibility
        self.pending_deletions.insert(doc_id);

        Ok(())
    }

    /// Check if a document is marked as deleted in the pending set.
    pub fn is_updated_deleted(&self, doc_id: u64) -> bool {
        self.pending_deletions.contains(&doc_id)
    }

    /// Find all segments containing the global doc_id.
    /// Returns a list of (segment_id, min_doc_id, max_doc_id).
    ///
    /// Served from [`Self::segment_ranges`] (Issue #559 / #864) — the cache
    /// mirrors the on-storage `*.meta` files, so this no longer lists and
    /// JSON-parses them per call. Fresh doc IDs (the steady-state ingest
    /// path) are rejected with a single compare against
    /// [`Self::max_committed_doc_id`].
    fn find_segments_for_doc(&self, doc_id: u64) -> Result<Vec<(String, u64, u64)>> {
        // Fast path: WAL doc IDs are monotonic, so an ID above every
        // committed segment's max cannot be in any of them.
        if doc_id > self.max_committed_doc_id {
            return Ok(Vec::new());
        }
        // In Stable ID mode, we check if the ID is within the min/max range.
        // Note: This might match multiple segments if ranges overlap across shards,
        // or if we have multiple versions of the same document (upserts).
        // To be 100% sure, we should check if the document actually exists in
        // the segment. For now, assume the range is specific enough.
        Ok(self
            .segment_ranges
            .iter()
            .filter(|(_, min_doc_id, max_doc_id)| doc_id >= *min_doc_id && doc_id <= *max_doc_id)
            .cloned()
            .collect())
    }

    /// Rebuild [`Self::segment_ranges`] / [`Self::max_committed_doc_id`] from
    /// the `*.meta` files on storage and drop the cached
    /// [`Self::deletion_manager`].
    ///
    /// Must be called after an external segment rewrite that this writer did
    /// not perform itself — today that is only
    /// [`LexicalStore::optimize`](crate::lexical::store::LexicalStore::optimize)'s
    /// force-merge, which replaces every segment (and its deletion bitmap)
    /// behind a live writer. `LexicalStore::commit` needs no call: it drops
    /// the cached writer before merging, so the next writer rebuilds the
    /// cache in its constructor.
    ///
    /// # Errors
    ///
    /// Returns an error if listing the storage fails; malformed `.meta` files
    /// are skipped, matching the constructor's recovery scan.
    pub fn invalidate_segment_cache(&mut self) -> Result<()> {
        // Build the new view first and swap it in only on success: clearing
        // eagerly and then failing (e.g. on `list_files`) would leave the
        // writer alive with an EMPTY cache and `max_committed_doc_id == 0`,
        // silently skipping every subsequent overwrite's deletion via the
        // fast path — worse than the error itself.
        let mut segment_ranges = Vec::new();
        let mut max_committed_doc_id = 0u64;
        for file in self.storage.list_files()? {
            if !file.ends_with(".meta") || file == "index.meta" {
                continue;
            }
            let Ok(input) = self.storage.open_input(&file) else {
                continue;
            };
            let Ok(meta) = serde_json::from_reader::<_, SegmentInfo>(input) else {
                continue;
            };
            max_committed_doc_id = max_committed_doc_id.max(meta.max_doc_id);
            segment_ranges.push((meta.segment_id, meta.min_doc_id, meta.max_doc_id));
        }
        self.segment_ranges = segment_ranges;
        self.max_committed_doc_id = max_committed_doc_id;
        // Contract (Issue #875): any buffered deletion state must have been
        // flushed BEFORE the external rewrite that prompted this call —
        // `LexicalStore::optimize` calls `flush_deletions` ahead of its
        // force-merge. Dropping the manager (and the pending meta flips)
        // here discards only state that describes segments which no longer
        // exist; flushing at this point instead would recreate `.delmap` /
        // `.meta` files for merged-away segments.
        self.deletion_manager = None;
        self.pending_meta_deletions.clear();
        Ok(())
    }

    /// Record that a segment needs its `.meta` `has_deletions` flag flipped.
    ///
    /// The actual `.meta` parse + rewrite is deferred to
    /// [`Self::flush_deletions`] (Issue #875) — this is a set insert, so the
    /// per-delete hot path pays no storage read, JSON parse, or fsync.
    fn update_segment_meta_deletions(&mut self, segment_id: &str) {
        self.pending_meta_deletions.insert(segment_id.to_string());
    }

    /// Rewrite a segment's `.meta` to `has_deletions = true` (idempotent —
    /// nothing is written when the flag is already set).
    fn persist_segment_meta_deletions(&self, segment_id: &str) -> Result<()> {
        let meta_file = format!("{segment_id}.meta");
        let input = self.storage.open_input(&meta_file)?;
        let mut meta: SegmentInfo = serde_json::from_reader(input)
            .map_err(|e| LaurusError::index(format!("Failed to read segment meta: {e}")))?;

        if !meta.has_deletions {
            meta.has_deletions = true;
            let json = serde_json::to_string_pretty(&meta).map_err(|e| {
                LaurusError::index(format!("Failed to serialize segment meta: {e}"))
            })?;
            let mut output = self.storage.create_output(&meta_file)?;
            std::io::Write::write_all(&mut output, json.as_bytes())?;
            output.close()?;
        }

        Ok(())
    }

    /// Group-commit all buffered deletion state (Issue #875).
    ///
    /// Persists every dirty deletion bitmap via
    /// [`DeletionManager::flush`](crate::maintenance::deletion::DeletionManager::flush)
    /// and then flips the pending `.meta` `has_deletions` flags — in that
    /// order, so a reader (or the merge engine) that observes the flag always
    /// finds the corresponding `.delmap` on storage.
    ///
    /// Called from [`Self::commit`] before the `metadata.json` /
    /// `last_wal_seq` checkpoint (a crash before the checkpoint keeps the WAL
    /// delete records replayable), and from
    /// [`LexicalStore::optimize`](crate::lexical::store::LexicalStore::optimize)
    /// before its force-merge (the merge engine reads deletions from the
    /// on-disk `.delmap`, so unflushed deletions would be resurrected into
    /// the merged segment). Idempotent — a second call without intervening
    /// deletions writes nothing.
    ///
    /// # Errors
    ///
    /// Returns an error if persisting a bitmap or rewriting a `.meta` fails;
    /// unpersisted state stays buffered so a later flush retries it.
    pub fn flush_deletions(&mut self) -> Result<()> {
        if let Some(manager) = &self.deletion_manager {
            manager.flush()?;
        }

        if self.pending_meta_deletions.is_empty() {
            return Ok(());
        }
        let pending: Vec<String> = self.pending_meta_deletions.iter().cloned().collect();
        for segment_id in pending {
            self.persist_segment_meta_deletions(&segment_id)?;
            self.pending_meta_deletions.remove(&segment_id);
        }
        Ok(())
    }

    /// Delete a document by ID.
    ///
    /// Removes the document from the buffered documents if it exists. The
    /// in-memory inverted index and DocValues are rebuilt from the remaining
    /// buffered docs so the deleted doc's postings and stored fields do not
    /// leak into the next flushed segment. For documents that have already
    /// been committed to disk, the deletion is recorded through the
    /// `DeletionManager` (i.e. soft-deleted via the segment's deletion
    /// bitmap).
    ///
    /// This mirrors the pattern used by [`Self::upsert_document`], which also
    /// runs `remove_pending_document` + `mark_persisted_doc_deleted` before
    /// indexing the new version. Deleting via `buffered_docs.retain` alone
    /// (the previous behaviour) left the doc's postings in the in-memory
    /// inverted index, so subsequent puts of the same external id within the
    /// same uncommitted batch would accumulate ghost postings — see the
    /// regression test `delete_document_clears_inverted_index_postings`.
    pub fn delete_document(&mut self, doc_id: u64) -> Result<()> {
        self.remove_pending_document(doc_id)?;
        self.mark_persisted_doc_deleted(doc_id)?;
        Ok(())
    }
}

impl Drop for InvertedIndexWriter {
    fn drop(&mut self) {
        let _ = self.close();
    }
}

// Implement LexicalIndexWriter trait for compatibility with existing code
impl LexicalIndexWriter for InvertedIndexWriter {
    fn add_document(&mut self, doc: Document) -> Result<u64> {
        InvertedIndexWriter::add_document(self, doc)
    }

    fn invalidate_segment_cache(&mut self) -> Result<()> {
        InvertedIndexWriter::invalidate_segment_cache(self)
    }

    fn flush_deletions(&mut self) -> Result<()> {
        InvertedIndexWriter::flush_deletions(self)
    }

    fn upsert_document(&mut self, doc_id: u64, doc: Document) -> Result<()> {
        InvertedIndexWriter::upsert_document(self, doc_id, doc)
    }

    fn add_analyzed_document(&mut self, doc: AnalyzedDocument) -> Result<u64> {
        InvertedIndexWriter::add_analyzed_document(self, doc)
    }

    fn upsert_analyzed_document(&mut self, doc_id: u64, doc: AnalyzedDocument) -> Result<()> {
        InvertedIndexWriter::upsert_analyzed_document(self, doc_id, doc)
    }

    fn delete_document(&mut self, doc_id: u64) -> Result<()> {
        InvertedIndexWriter::delete_document(self, doc_id)
    }

    fn commit(&mut self) -> Result<()> {
        InvertedIndexWriter::commit(self)
    }

    fn rollback(&mut self) -> Result<()> {
        InvertedIndexWriter::rollback(self)
    }

    fn pending_docs(&self) -> u64 {
        InvertedIndexWriter::pending_docs(self) as u64
    }

    fn close(&mut self) -> Result<()> {
        InvertedIndexWriter::close(self)
    }

    fn is_closed(&self) -> bool {
        InvertedIndexWriter::is_closed(self)
    }

    fn set_last_wal_seq(&mut self, seq: u64) -> Result<()> {
        self.last_wal_seq = seq;
        Ok(())
    }

    fn is_updated_deleted(&self, doc_id: u64) -> bool {
        InvertedIndexWriter::is_updated_deleted(self, doc_id)
    }

    /// Builds an InvertedIndexReader from the current state of the writer's storage.
    /// This method is intended to be called by the LexicalIndexWriter trait implementation.
    fn build_reader(
        &self,
    ) -> Result<std::sync::Arc<dyn crate::lexical::reader::LexicalIndexReader>> {
        use crate::lexical::index::inverted::reader::{
            InvertedIndexReader, InvertedIndexReaderConfig,
        };
        use crate::lexical::index::inverted::segment::SegmentInfo;

        // List all segments from storage
        // This assumes standard segment naming: segment_XXXXXX.meta
        let mut segments = Vec::new();
        let mut segment_id = 0;

        loop {
            let segment_name = format!("{}_{:06}", self.config.segment_prefix, segment_id);
            let meta_file = format!("{}.meta", segment_name);

            if self.storage.file_exists(&meta_file) {
                // Read segment metadata
                let input = self.storage.open_input(&meta_file)?;
                let mut json_data = String::new();
                std::io::Read::read_to_string(&mut std::io::BufReader::new(input), &mut json_data)?;

                let segment_info: SegmentInfo = serde_json::from_str(&json_data).map_err(|e| {
                    LaurusError::index(format!("Failed to parse segment metadata: {e}"))
                })?;

                segments.push(segment_info);
                segment_id += 1;
            } else {
                break;
            }
        }

        let config = InvertedIndexReaderConfig {
            analyzer: self.config.analyzer.clone(),
            ..Default::default()
        };

        // Note: InvertedIndexReader::new expects Vec<SegmentInfo> and Arc<dyn Storage>
        // We use the same storage as the writer
        let reader = InvertedIndexReader::new(segments, self.storage.clone(), config)?;
        Ok(Arc::new(reader))
    }

    fn next_doc_id(&self) -> u64 {
        self.next_doc_id
    }

    fn find_doc_id_by_term(&self, field: &str, term: &str) -> Result<Option<u64>> {
        InvertedIndexWriter::find_doc_id_by_term(self, field, term)
    }

    fn find_doc_ids_by_term(&self, field: &str, term: &str) -> Result<Option<Vec<u64>>> {
        InvertedIndexWriter::find_doc_ids_by_term(self, field, term)
    }
}