kividb 1.2.2

A fast RedisĀ®-compatible in-memory store written in Rust
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
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
use std::collections::BTreeSet;
use std::sync::Arc;

use crate::commands::ctx::Ctx;
use crate::resp::write::*;
use crate::vector::{FieldType, LiveIndex, VectorAlgo};

/// Commands whose first argument names an index, and so may name an alias.
fn takes_index_name(name: &str) -> bool {
    matches!(
        name,
        "FT.INFO"
            | "FT.DROPINDEX"
            | "FT.SEARCH"
            | "FT.AGGREGATE"
            | "FT.ALTER"
            | "FT.EXPLAIN"
            | "FT.EXPLAINCLI"
            | "FT.PROFILE"
            | "FT.TAGVALS"
            | "FT.SYNUPDATE"
            | "FT.SYNDUMP"
            | "FT.SPELLCHECK"
            | "FT.HYBRID"
            | "FT.ALIASLIST"
    )
}

/// What a command is told of a name it gave that is another database's.
#[derive(Clone, Copy)]
enum Elsewhere {
    /// An index (or an alias of one): `Index not found`.
    Index,
    /// The alias of `FT.ALIASDEL`: `Alias does not exist`.
    Alias,
    /// The alias of `FT.ALIASUPDATE`, which would be moved from another
    /// database's index: `Alias already exists`, as `FT.ALIASADD` says.
    AliasTaken,
}

/// The arguments of `name` that name an index or an alias of one, in the
/// order they are checked: for most, the first; `FT.ALIASADD` and
/// `FT.ALIASUPDATE` name the index second (and `FT.ALIASUPDATE` the alias
/// first), `FT.CURSOR` and `FT.DEBUG COMPACT|POSTINGS|PAGE` after their
/// subcommand.
fn index_name_positions(name: &str, cmd: &[&[u8]]) -> &'static [(usize, Elsewhere)] {
    match name {
        "FT.ALIASUPDATE" => &[(1, Elsewhere::AliasTaken), (2, Elsewhere::Index)],
        "FT.ALIASADD" | "FT.CURSOR" => &[(2, Elsewhere::Index)],
        "FT.ALIASDEL" => &[(1, Elsewhere::Alias)],
        "FT.DEBUG"
            if cmd.get(1).is_some_and(|sub| {
                [&b"COMPACT"[..], b"POSTINGS", b"PAGE"]
                    .iter()
                    .any(|known| sub.eq_ignore_ascii_case(known))
            }) =>
        {
            &[(2, Elsewhere::Index)]
        }
        _ if takes_index_name(name) => &[(1, Elsewhere::Index)],
        _ => &[],
    }
}

/// Whether the index or alias `given` is one of another database than the
/// selected one. An index is its database's: from any other, it is not
/// there (#447). (Redis has indexes in database 0 only: `FT.CREATE` in any
/// other is refused, and every database sees database 0's.)
fn in_another_database(ctx: &Ctx, given: &[u8]) -> bool {
    let Ok(given) = std::str::from_utf8(given) else {
        return false;
    };
    ctx.index_store
        .read()
        .get(given)
        .is_some_and(|def| def.db_index != *ctx.selected_db)
}

pub fn handle(ctx: &mut Ctx, name: &str) -> Option<Vec<u8>> {
    if !name.starts_with("FT.") {
        return None;
    }
    // An index, or an alias, of another database is not there.
    for &(at, elsewhere) in index_name_positions(name, &ctx.cmd) {
        let Some(given) = ctx.cmd.get(at).copied() else {
            continue;
        };
        if in_another_database(ctx, given) {
            let given = String::from_utf8_lossy(given);
            return Some(resp_error(&match elsewhere {
                Elsewhere::Index => crate::commands::search::no_such_index(&given),
                Elsewhere::Alias => "ERR Alias does not exist".to_string(),
                Elsewhere::AliasTaken => "ERR Alias already exists".to_string(),
            }));
        }
    }
    // An alias is resolved here, once, for every command that takes an index
    // name: the handler then runs against the real index and never sees the
    // alias. (`ctx.cmd` is put back afterwards, so what is propagated is what
    // the client sent.)
    let alias_target = if takes_index_name(name) {
        ctx.cmd
            .get(1)
            .and_then(|b| std::str::from_utf8(b).ok())
            .and_then(|given| crate::vector::alias_target(&ctx.index_store.read(), given))
    } else {
        None
    };
    if let Some(real) = alias_target {
        let original = ctx.cmd[1];
        // `real` outlives the dispatch below, and `ctx.cmd[1]` is restored
        // before it is dropped.
        let erased: &'static [u8] = unsafe { std::mem::transmute(real.as_bytes()) };
        ctx.cmd[1] = erased;
        let reply = dispatch(ctx, name);
        ctx.cmd[1] = original;
        return reply;
    }
    dispatch(ctx, name)
}

fn dispatch(ctx: &mut Ctx, name: &str) -> Option<Vec<u8>> {
    match name {
        "FT.CREATE" => crate::commands::search::ft_create(ctx),
        "FT._LIST" => ft_list(ctx),
        "FT.INFO" => crate::commands::search::ft_info(ctx),
        "FT.DROPINDEX" => ft_dropindex(ctx),
        "FT.SEARCH" => crate::commands::search::ft_search(ctx),
        "FT.AGGREGATE" => ft_aggregate(ctx),

        // Alias commands — store a name → index-name mapping
        "FT.ALIASADD" => ft_alias_add(ctx),
        "FT.ALIASDEL" => ft_alias_del(ctx),
        "FT.ALIASUPDATE" => ft_alias_update(ctx),
        "FT.ALIASLIST" => ft_alias_list(ctx),

        "FT.ALTER" => ft_alter(ctx),

        // Dict commands — used for custom synonyms/stopwords
        "FT.DICTADD" => ft_dictadd(ctx),
        "FT.DICTDEL" => ft_dictdel(ctx),
        "FT.DICTDUMP" => ft_dictdump(ctx),

        "FT.EXPLAIN" => crate::commands::search_tools::ft_explain(ctx, false),
        "FT.EXPLAINCLI" => crate::commands::search_tools::ft_explain(ctx, true),
        "FT.PROFILE" => crate::commands::search_tools::ft_profile(ctx),
        "FT.CONFIG" => crate::commands::search_tools::ft_config(ctx),
        "FT.CURSOR" => crate::commands::search_tools::ft_cursor(ctx),
        "FT.TAGVALS" => ft_tagvals(ctx),
        "FT.SYNUPDATE" => ft_synupdate(ctx),
        "FT.SYNDUMP" => ft_syndump(ctx),
        "FT.SPELLCHECK" => ft_spellcheck(ctx),
        "FT.DEBUG" => ft_debug(ctx),
        "FT.HYBRID" => crate::commands::hybrid::ft_hybrid(ctx),
        _ => None,
    }
}

fn ft_list(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let store = ctx.index_store.read();
    // The selected database's indexes, not the aliases that point at them.
    // Those of every database used to be listed (#447).
    let db = *ctx.selected_db;
    let items: Vec<Vec<u8>> = store
        .iter()
        .filter(|(key, def)| !crate::vector::is_alias(key, def) && def.db_index == db)
        .map(|(key, _)| resp_bulk_str(key))
        .collect();
    Some(resp_array(items))
}

fn ft_dropindex(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    if cmd.len() < 2 {
        return Some(resp_error(
            "ERR wrong number of arguments for 'ft.dropindex' command",
        ));
    }
    let name = match std::str::from_utf8(cmd[1]) {
        Ok(s) => s.to_string(),
        Err(_) => return Some(resp_error("ERR index name must be valid UTF-8")),
    };

    // `DD` deletes the documents too. A server sends what it deleted as
    // DELs, and the drop without `DD` (below), so a `DD` a replica or an AOF
    // replay meets was written by a release before this one, and is run as
    // that release ran it, where that can be known: 1.1 and before kept the
    // documents; a 1.2.0 release candidate deleted them, and had each
    // replica delete what it found. A replica knows which release its
    // master is (`ReplState::master_dd_deletes`). An AOF does not say
    // which release wrote a record, and one file can hold the records of
    // several, so a replay keeps the documents: wrongly kept, a 1.2.0
    // candidate's deleted documents come back and can be deleted again;
    // wrongly deleted, a 1.1 server's documents would be lost.
    let relaxed = std::sync::atomic::Ordering::Relaxed;
    let delete_documents = match &cmd[2..] {
        [] => false,
        [dd] if dd.eq_ignore_ascii_case(b"DD") => {
            !ctx.apply_replication
                || (!crate::vector::DEFER_INDEXING.load(relaxed)
                    && ctx.repl_state.master_dd_deletes.load(relaxed))
        }
        _ => return Some(resp_error("SEARCH_ARG_UNRECOGNIZED Unknown argument")),
    };

    // The index's database, and what its documents are: taken from its
    // definition, which is not held while the keys are looked through.
    let found = ctx
        .index_store
        .read()
        .get(&name)
        .filter(|def| !crate::vector::is_alias(&name, def))
        .map(|def| (def.db_index, def.on.clone(), def.prefixes.clone()));
    let Some((db, on, prefixes)) = found else {
        return Some(resp_error(&crate::commands::search::no_such_index(&name)));
    };
    // The documents, if they go too: found before anything is deleted, one
    // shard at a time.
    let mut documents = Vec::new();
    if delete_documents {
        for shard in ctx.store[db].iter() {
            let map = shard.read();
            for (key, value) in map.iter() {
                let covered =
                    prefixes.is_empty() || prefixes.iter().any(|p| key.starts_with(p.as_bytes()));
                if covered && crate::vector::is_document(&on, value) {
                    documents.push(crate::store::Key::new(key));
                }
            }
        }
    }

    // Replicas and the AOF get it as Redis 8.10 sends it: the documents
    // deleted as DELs, and then the drop, without `DD`. A replica then
    // deletes what its master deleted and nothing else, where it used to
    // run `DD` itself and delete whatever its own copy of the index covered.
    //
    // The documents go a few at a time, each few a whole write of its own:
    // the write gate and the order of their keys are held for them only, so
    // a full resync or a checkpoint can cut between them, and writes to
    // other keys go on, however many documents there are. The index goes
    // last, as in Redis, so the state of this node after each record it
    // sends is the state a replica reaches by applying it, and a `DD` cut
    // short (the server killed part way, or a replica promoted with half
    // of it) leaves the index there to run it again: the documents left
    // are not orphaned. Dropping the index first would spare this node and
    // its replicas taking each document out of it (about two seconds a
    // million documents, measured), but not that.
    //
    // Each few is sent as one DEL of them all, where Redis sends a DEL of
    // each: a record apiece cost more than the deletes themselves (half
    // again as long, with the AOF on, for a million documents). In a
    // cluster, one DEL names the keys of one slot only, as a command from a
    // client would: a slot migration following the stream sends a command
    // on by its first key. So the documents are taken a slot at a time.
    //
    // A replica sends the same to its own AOF, whatever its master sent, so
    // that no `DD` is ever written again.
    let propagate = crate::replication::write_is_observed(ctx);
    let by_slot = propagate && ctx.cluster.enabled;
    let slot_of = |key: &[u8]| {
        if by_slot {
            crate::cluster::key_hash_slot(key)
        } else {
            0
        }
    };
    if by_slot {
        documents.sort_by_cached_key(|key| slot_of(key));
    }
    crate::aof::failpoint("dropindex-collected");
    let nested = crate::write_gate::held_by_this_thread();
    let mut deleted = 0usize;
    let mut del: Vec<Vec<u8>> = Vec::new();
    for chunk in documents.chunks(DROPINDEX_DOCUMENTS_PER_WRITE) {
        let _gate = dropindex_write_gate(ctx, nested);
        // The order of the chunk's keys, whether or not anything reads the
        // write stream: a write to one of them is then wholly before its
        // deletion here, or wholly after (#275).
        let _order = crate::write_order::lock_keys(db, chunk.iter().map(|k| k.as_bytes()));
        let mut slot = chunk.first().map(|key| slot_of(key));
        for key in chunk {
            if by_slot && slot != Some(slot_of(key)) {
                slot = Some(slot_of(key));
                send_dels(ctx, db, &mut del);
            }
            // One that has expired is gone already, as any read finds it.
            if crate::commands::helpers::is_expired(ctx.store, db, key) {
                crate::commands::helpers::delete_key_if_expired(ctx.store, db, key);
                continue;
            }
            // Only if it is still a document of the index (#275): one
            // written meanwhile as another type (a string, a hash under an
            // `ON JSON` index) is no longer one, and run after that write,
            // `DD` would have kept it. (Whether the index covers it goes by
            // its name and the index's prefixes, neither of which changes.)
            if !delete_document(ctx.store, db, key, &on) {
                continue;
            }
            deleted += 1;
            crate::vector::maybe_unindex(ctx.index_store, db, key);
            crate::stats::record_rdb_change();
            crate::keyspace_events::notify(
                ctx.pubsub,
                db,
                crate::keyspace_events::KS_GENERIC,
                "del",
                key,
            );
            ctx.watch_registry.notify(db, key);
            crate::commands::handler::invalidate_tracked_key(ctx, key, false);
            if propagate {
                if del.is_empty() {
                    del.push(b"DEL".to_vec());
                }
                del.push(key.as_bytes().to_vec());
            }
        }
        // Sent before the order of these keys is let go of.
        send_dels(ctx, db, &mut del);
    }

    // The index goes, and its aliases with it.
    let _gate = dropindex_write_gate(ctx, nested);
    if !crate::vector::remove_index(&mut ctx.index_store.write(), &name) {
        // Dropped meanwhile by another client, which sent the drop.
        if deleted == 0 {
            return Some(resp_error(&crate::commands::search::no_such_index(&name)));
        }
        return Some(resp_simple("OK"));
    }
    if propagate {
        propagate_in_db(ctx, db, &[b"FT.DROPINDEX".to_vec(), name.into_bytes()]);
    }
    Some(resp_simple("OK"))
}

/// How many documents `FT.DROPINDEX … DD` deletes under one hold of the
/// write gate and of their keys' order.
const DROPINDEX_DOCUMENTS_PER_WRITE: usize = 256;

/// The write gate, for one part of an `FT.DROPINDEX`, which the handler
/// leaves it to take (see `handler::run_command`): stepped out of while a
/// handoff is under way, as the handler does for any other write. Inside a
/// transaction or a script the gate is held already, and this takes nothing.
/// Delete `key` if it holds a document of an index `ON on`, deciding and
/// deleting under its shard's write lock. Whether it did.
fn delete_document(
    store: &crate::store::Store,
    db: usize,
    key: &[u8],
    on: &crate::vector::IndexOn,
) -> bool {
    let shard = crate::store::get_shard(&store[db], key);
    let removal = shard
        .write()
        .remove_if(key, |value| crate::vector::is_document(on, value));
    // Freed with the shard unlocked.
    matches!(removal, crate::store::Removal::Taken(_))
}

fn dropindex_write_gate(ctx: &Ctx, nested: bool) -> crate::write_gate::WriteGuard {
    let mut gate = crate::write_gate::enter();
    while !nested && !ctx.apply_replication && crate::pause::handoff() {
        drop(gate);
        crate::pause::wait_out_handoff();
        gate = crate::write_gate::enter();
    }
    gate
}

/// The DEL `del` holds, if it names a key, to replicas and the AOF; `del`
/// is left empty.
fn send_dels(ctx: &mut Ctx, db: usize, del: &mut Vec<Vec<u8>>) {
    if del.len() > 1 {
        propagate_in_db(ctx, db, del);
    }
    del.clear();
}

/// Hand `parts` to replicas and the AOF as a write made in database `db`,
/// which need not be the one selected: an index's documents are in its own.
fn propagate_in_db(ctx: &mut Ctx, db: usize, parts: &[Vec<u8>]) {
    let selected = *ctx.selected_db;
    *ctx.selected_db = db;
    crate::replication::replicate_one_write(ctx, parts);
    *ctx.selected_db = selected;
}

// ── FT.ALTER ─────────────────────────────────────────────────────────────────
//
// FT.ALTER index [SKIPINITIALSCAN] SCHEMA ADD field type [options ...]
//
// Appends new fields to an existing index's schema.
// Does NOT rebuild the live vector index for existing keys — that would
// require a full table scan and is what SKIPINITIALSCAN controls in Redis.
// For non-vector fields (TEXT/TAG/NUMERIC) there is no live structure to
// update; they are evaluated at query time by scanning hash fields.
// For VECTOR fields added via ALTER we do backfill unless SKIPINITIALSCAN.
//
// Key Rust lesson: we take a write lock on the whole IndexStore, get a
// `&mut IndexDef` from it, and mutate in place. This is the idiomatic way
// to update a value inside Arc<RwLock<HashMap<..>>>.

fn ft_alter(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;

    // Minimum: FT.ALTER <index> SCHEMA ADD <field> <type>
    if cmd.len() < 6 {
        return Some(resp_error(
            "ERR wrong number of arguments for 'ft.alter' command",
        ));
    }

    let index_name = match std::str::from_utf8(cmd[1]) {
        Ok(s) => s.to_string(),
        Err(_) => return Some(resp_error("ERR index name must be valid UTF-8")),
    };

    // Walk args to find SCHEMA ADD, skipping optional SKIPINITIALSCAN
    let mut i = 2usize;
    let mut skip_initial_scan = false;

    while i < cmd.len() {
        let tok = match std::str::from_utf8(cmd[i]) {
            Ok(s) => s.to_ascii_uppercase(),
            Err(_) => return Some(resp_error("ERR invalid argument")),
        };
        match tok.as_str() {
            "SKIPINITIALSCAN" => {
                skip_initial_scan = true;
                i += 1;
            }
            "SCHEMA" => {
                i += 1;
                break;
            }
            _ => {
                i += 1;
            }
        }
    }

    // Now expect ADD
    let add_tok = cmd
        .get(i)
        .and_then(|b| std::str::from_utf8(b).ok())
        .map(|s| s.to_ascii_uppercase());

    match add_tok.as_deref() {
        Some("ADD") => {
            i += 1;
        }
        _ => {
            return Some(resp_error(
                "ERR syntax: FT.ALTER index SCHEMA ADD field type ...",
            ));
        }
    }

    // The same parser as FT.CREATE, so the two always accept the same schema.
    let new_fields = match crate::commands::search::parse_schema_fields(cmd, &mut i) {
        Ok(fields) => fields,
        Err(e) => return Some(resp_error(&e)),
    };

    if new_fields.is_empty() {
        return Some(resp_error("ERR no fields specified"));
    }

    // ── Mutate the stored IndexDef ────────────────────────────────────────────
    //
    // We need a write lock on the store to get &mut IndexDef.
    // This is safe: write() gives exclusive access, so no other thread
    // can read or write the map until we're done.
    let mut store = ctx.index_store.write();
    let def = match store.get_mut(&index_name) {
        Some(d) => d,
        None => {
            return Some(resp_error(&crate::commands::search::no_such_index(
                &index_name,
            )));
        }
    };

    // Check for duplicate field names — Redis rejects them
    for nf in &new_fields {
        if def
            .fields
            .iter()
            .any(|f| f.name == nf.name || f.attribute() == nf.attribute())
        {
            return Some(resp_error(&format!(
                "Duplicate field in schema - {}",
                nf.attribute()
            )));
        }
    }

    // ── Provision live indexes for any newly added VECTOR fields ──────────────
    //
    // A VECTOR field needs an empty FLAT or HNSW structure so HSET can start
    // populating it. A TEXT field needs its words indexed. Both are then
    // filled in from the documents that already exist, below, unless
    // SKIPINITIALSCAN.
    let mut newly_added_vector_fields: Vec<String> = Vec::new();
    let mut newly_added_text_fields: Vec<String> = Vec::new();
    let added_filter_fields: Vec<crate::vector::SchemaField> = new_fields
        .iter()
        .filter(|f| matches!(f.field_type, FieldType::Tag | FieldType::Numeric))
        .cloned()
        .collect();
    for field in &new_fields {
        match &field.field_type {
            FieldType::Vector { algo, .. } => {
                let live = match algo {
                    VectorAlgo::Flat => {
                        LiveIndex::Flat(Arc::new(crate::vector::VectorIndex::new()))
                    }
                    VectorAlgo::Hnsw {
                        m, ef_construction, ..
                    } => LiveIndex::Hnsw(Arc::new(crate::vector::HnswIndex::new(
                        *m,
                        *ef_construction,
                    ))),
                };
                def.indexes.insert(field.name.clone(), live);
                newly_added_vector_fields.push(field.name.clone());
            }
            FieldType::Text if !field.options.noindex => {
                newly_added_text_fields.push(field.name.clone());
            }
            _ => {}
        }
    }
    def.fields.extend(new_fields);
    let altered = def.clone();

    // We need to drop the write lock before backfilling — backfill takes
    // a *read* lock on the index store, and parking_lot's RwLock is not
    // re-entrant. Holding both would deadlock.
    drop(store);

    // ── Backfill ──────────────────────────────────────────────────────────────
    //
    // Walk every existing key matching the index's prefixes. For each
    // newly added VECTOR field that has a value in the hash, insert it
    // into that field's LiveIndex. Existing fields are already populated;
    // only the new ones need this pass.
    if !skip_initial_scan && !newly_added_vector_fields.is_empty() {
        backfill_new_vector_fields(ctx, &index_name, &newly_added_vector_fields);
    }
    if !skip_initial_scan && !newly_added_text_fields.is_empty() {
        crate::vector::backfill_text_fields(
            &ctx.store[altered.db_index],
            &altered,
            &newly_added_text_fields,
        );
    }
    if !added_filter_fields.is_empty() {
        crate::vector::backfill_postings(
            &ctx.store[altered.db_index],
            &altered,
            &added_filter_fields,
            !skip_initial_scan,
        );
    }

    Some(resp_simple("OK"))
}

// ── FT.AGGREGATE ──────────────────────────────────────────────────────────────
//
// FT.AGGREGATE index query [LOAD ...] [GROUPBY ... REDUCE ...] [SORTBY ...] [LIMIT ...]
//
// The pipeline model:
//   1. Scan all hash keys matching the index prefixes (same as FT.SEARCH
//      but we collect ALL matches, not just KNN results).
//   2. Each clause transforms the Vec<Row> in place.
//   3. Emit the final rows as a RESP array.
//
// A Row is a HashMap<String, String> — field name → string value.
// This mirrors how Redis represents aggregate rows internally.
//
// Rust lesson: we use Vec<AHashMap<String, String>> as our row buffer and
// pass it through each pipeline stage by value (move semantics). Each stage
// consumes the input Vec and produces a new one. This is idiomatic Rust —
// no in-place mutation of shared state, no locks needed after the initial
// data collection phase.

type Row = ahash::AHashMap<String, String>;

pub(crate) fn ft_aggregate(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;

    if cmd.len() < 3 {
        return Some(resp_error(
            "ERR wrong number of arguments for 'ft.aggregate' command",
        ));
    }

    let index_name = match std::str::from_utf8(cmd[1]) {
        Ok(s) => s.to_string(),
        Err(_) => return Some(resp_error("ERR index name must be valid UTF-8")),
    };

    let query_str = match std::str::from_utf8(cmd[2]) {
        Ok(s) => s.to_string(),
        Err(_) => return Some(resp_error("ERR query must be valid UTF-8")),
    };

    // Parse the query into a filter AST.
    // FT.AGGREGATE query syntax is the same as FT.SEARCH pre-filter syntax:
    //   "*"                        → match all (no filter)
    //   "@category:{electronics}"  → TAG filter
    //   "@price:[0 200]"           → NUMERIC range
    //   "(@tag:{x} @price:[0 100])"→ AND of two clauses
    //
    // We reuse parse_prefilter from the filter module — it already handles
    // the full pre-filter grammar. For aggregate queries there's no KNN
    // suffix, so parse_prefilter just sees the whole string as the filter part.
    //
    // The query is parsed against the index's schema, so the index comes first.

    // ── Resolve the index ─────────────────────────────────────────────────────
    let index_store = ctx.index_store.read();
    let def = match index_store.get(&index_name) {
        Some(d) => d.clone(), // clone so we can drop the store lock before scanning
        None => {
            return Some(resp_error(&crate::commands::search::no_such_index(
                &index_name,
            )));
        }
    };
    drop(index_store);

    // PARAMS, wherever in the command they are.
    let mut params = crate::vector::filter::Params::new();
    if let Some(at) = cmd.iter().position(|a| a.eq_ignore_ascii_case(b"PARAMS")) {
        let n = cmd
            .get(at + 1)
            .and_then(|a| std::str::from_utf8(a).ok())
            .and_then(|s| s.parse::<usize>().ok())
            .unwrap_or(0);
        let values: Vec<&[u8]> = cmd.iter().skip(at + 2).take(n).copied().collect();
        for pair in values.chunks(2) {
            if let [name, value] = pair {
                params.insert(String::from_utf8_lossy(name).into_owned(), value.to_vec());
            }
        }
    }
    // `VERBATIM`, `LANGUAGE` and `SCORER`, where an option may be.
    let option = |name: &str| crate::commands::aggregate_pipeline::option_at(cmd, 3, name);
    let value_of = |at: usize| cmd.get(at + 1).and_then(|a| std::str::from_utf8(a).ok());
    let mut options = crate::vector::filter::QueryOptions {
        verbatim: option("VERBATIM").is_some(),
        language: None,
    };
    if let Some(at) = option("LANGUAGE") {
        match value_of(at).and_then(crate::vector::stem::Language::parse) {
            Some(language) => options.language = Some(language),
            None => return Some(resp_error("No such language")),
        }
    }
    let scorer = match option("SCORER") {
        Some(at) => match value_of(at).and_then(crate::vector::score::Scorer::parse) {
            Some(scorer) => scorer,
            None => {
                return Some(resp_error(&format!(
                    "No such scorer {}",
                    value_of(at).unwrap_or_default()
                )));
            }
        },
        None => crate::vector::score::Scorer::default_scorer(),
    };
    let (filter, knn) =
        match crate::vector::filter::parse_query_with(&query_str, &params, &def, options) {
            Ok(query) => (query.filter, query.knn),
            Err(e) => return Some(resp_error(&e)),
        };

    // `ADDSCORES`: the relevance of each document, as `@__score`.
    let addscores = cmd.iter().any(|a| a.eq_ignore_ascii_case(b"ADDSCORES"));
    let scoring = crate::vector::score::Scoring::new(&def, &filter, scorer);
    let cursor = match crate::commands::search_tools::cursor_options(cmd) {
        Ok(cursor) => cursor,
        Err(e) => return Some(resp_error(&e)),
    };
    // `TIMEOUT` where an option may be, not a word of another clause that
    // happens to read `TIMEOUT` (`APPLY … AS timeout`, a parameter).
    let mut timeout_ms = None;
    if let Some(at) = crate::commands::aggregate_pipeline::option_at(cmd, 3, "TIMEOUT") {
        match cmd
            .get(at + 1)
            .and_then(|a| std::str::from_utf8(a).ok())
            .and_then(|s| s.parse::<u64>().ok())
        {
            Some(ms) => timeout_ms = Some(ms),
            None => {
                return Some(resp_error(
                    "Bad arguments for TIMEOUT: expected a non-negative integer",
                ));
            }
        }
    }
    let deadline = crate::commands::search::search_deadline(timeout_ms);

    // ── Stage 0: Collect all matching rows ────────────────────────────────────
    //
    // Walk every shard, collect hashes that match our prefixes.
    // Each hash becomes a Row with all its fields as string values.
    let mut rows: Vec<Row> = Vec::new();
    let db_ref = &ctx.store[*ctx.selected_db];
    // In a cluster, only the keys of the slots this node serves: see
    // `cluster::search`. Read before the walk: the documents are looked at
    // with their shard locked, and the slot table is not read then (see
    // `ClusterState`).
    let served = ctx
        .cluster
        .enabled
        .then(|| crate::cluster::search::served_slots(ctx.cluster));
    let not_served = |key: &str| {
        served.as_deref().is_some_and(|s| {
            !crate::cluster::search::in_served(s, &*crate::key_bytes::bytes_of(key))
        })
    };
    let json = matches!(def.on, crate::vector::IndexOn::Json);
    let mut add_row = |key: &str,
                       inner: &crate::vector::Fields,
                       json_doc: Option<&serde_json::Value>| {
        // ── Build row ─────────────────────────────────────────────────
        // Only rows that passed the filter reach here.
        let mut row: Row = ahash::AHashMap::new();

        if addscores {
            let score = crate::commands::search::relevance(&def, key, inner, &scoring, json_doc);
            if score == f64::NEG_INFINITY {
                return;
            }
            row.insert(
                "__score".to_string(),
                crate::commands::search::number_text(score),
            );
        }

        // Always include the key itself as a special field.
        // Prefixed with __ so it doesn't collide with user field names.
        row.insert("__key".to_string(), key.to_string());
        for (field, val) in inner.iter() {
            // Text only: a vector's bytes are not a value a row can show
            // or a pipeline can compute with.
            let Ok(s) = std::str::from_utf8(val) else {
                continue;
            };
            // A field of a JSON document goes by the name the schema
            // gives its path.
            let shown = match def.fields.iter().find(|f| json && &f.name == field) {
                Some(f) => f.attribute().to_string(),
                None => field.clone(),
            };
            row.insert(shown, s.to_string());
        }

        rows.push(row);
    };
    // From postings, if they narrow the filter (see `search::plan`); else
    // every document, the deadline kept between runs of them (`scan_docs`).
    let timed_out_scan = match crate::commands::search::plan(ctx, &def, &filter, None) {
        Some(plan) => {
            let mut seen = 0u32;
            let mut cut = false;
            for key in crate::commands::search::keys_of(&def, &plan.ids) {
                if crate::commands::search::deadline_due(deadline, &mut seen) {
                    cut = true;
                    break;
                }
                if not_served(&key) {
                    continue;
                }
                crate::commands::search::with_doc(db_ref, &def, &key, |inner, json| {
                    if plan.exact || crate::vector::filter::eval(&filter, inner) {
                        add_row(&key, inner, json);
                    }
                });
            }
            cut
        }
        None => crate::commands::search::scan_docs(db_ref, deadline, |key, value| {
            if !def.covers(&crate::key_bytes::bytes_of(key)) {
                return;
            }
            if not_served(key) {
                return;
            }
            let Some(inner) = crate::vector::doc_fields(&def.on, &def.fields, value) else {
                return;
            };
            // The filter on the raw fields, before anything is turned into
            // strings.
            if crate::vector::filter::eval(&filter, &*inner) {
                let json = inner.json();
                add_row(key, &inner, json);
            }
        }),
    };
    let mut timed_out = timed_out_scan;

    // ── A KNN clause: the nearest `k` of what the filter left ────────────────
    //
    // Exactly, as FT.SEARCH does with a filter, and with the distance under
    // the clause's name, for the pipeline. It was dropped, and every
    // document the filter left went on (#162). After a scan cut short, of
    // the rows it found; and it keeps the deadline too.
    if let Some(knn) = &knn {
        let Some(field) = def.fields.iter().find(|f| f.name == knn.field) else {
            return Some(resp_error("The vector field is gone from the index"));
        };
        let FieldType::Vector { metric, .. } = &field.field_type else {
            return Some(resp_error("The vector field is gone from the index"));
        };
        let Some(live) = def.indexes.get(&knn.field) else {
            return Some(resp_error("The vector field has no index"));
        };
        let candidates = rows
            .iter()
            .filter_map(|row| row.get("__key").map(String::as_str));
        let (nearest, cut) = knn_filtered(live, &knn.vector, knn.k, metric, candidates, deadline);
        timed_out |= cut;
        let rank: ahash::AHashMap<String, (usize, f32)> = nearest
            .into_iter()
            .enumerate()
            .map(|(i, found)| (found.key, (i, found.score)))
            .collect();
        rows.retain(|row| row.get("__key").is_some_and(|key| rank.contains_key(key)));
        for row in &mut rows {
            let (_, distance) = rank[&row["__key"]];
            row.insert(
                knn.alias.clone(),
                crate::commands::search::distance_text(distance),
            );
        }
        rows.sort_by_key(|row| rank[&row["__key"]].0);
    }

    // ── The rest of the cluster ──────────────────────────────────────────────
    // Asked by a coordinator: the rows as they are, every column, and
    // whether this scan ran out of time, if the coordinator asks (one from
    // before there was a timeout does not). A coordinator: the rows of every
    // other master too, before any step, then `ON_TIMEOUT`.
    if crate::cluster::search::is_forwarded(ctx) {
        let flag = crate::cluster::search::wants_timeout_flag(cmd).then_some(timed_out);
        return Some(crate::cluster::search::encode_rows(&rows, flag));
    }
    if crate::cluster::search::coordinates(ctx) {
        let mut owned: Vec<Vec<u8>> = cmd.iter().map(|a| a.to_vec()).collect();
        crate::cluster::search::ask_for_timeout_flag(&mut owned);
        match crate::cluster::search::fan_out(ctx, &owned) {
            Ok(replies) => {
                for reply in replies {
                    let (more, shard_timed_out) = crate::cluster::search::decode_rows(reply);
                    rows.extend(more);
                    timed_out |= shard_timed_out;
                }
            }
            Err(e) => return Some(resp_error(&e)),
        }
        // Each master sent its own nearest: the nearest of them all.
        if let Some(knn) = &knn {
            let distance = |row: &Row| {
                row.get(&knn.alias)
                    .and_then(|d| d.parse::<f64>().ok())
                    .unwrap_or(f64::INFINITY)
            };
            rows.sort_by(|a, b| distance(a).total_cmp(&distance(b)));
            rows.truncate(knn.k);
        }
    }
    if timed_out && crate::commands::search::on_timeout_fails() {
        return Some(resp_error("Timeout limit was reached"));
    }

    // ── Parse and execute pipeline clauses ────────────────────────────────────
    // To the same deadline: GROUPBY, APPLY, FILTER and SORTBY over a large
    // scan cost as much as the scan.
    let (rows, cut) = crate::commands::aggregate_pipeline::run_within(rows, cmd, 3, deadline);
    timed_out |= cut;
    if cut && crate::commands::search::on_timeout_fails() {
        return Some(resp_error("Timeout limit was reached"));
    }

    // ── Emit final rows ─────────────────────────────────────────────────────
    //
    // A row shows what the command asked to see: the fields it LOADed, the
    // ones APPLY computed, the ones it sorted by — or, after a GROUPBY, the
    // group. It used to show every field of the document, whatever was
    // asked for, with a vector's bytes turned into an empty string.
    let word = |i: usize| -> String {
        cmd.get(i)
            .map(|a| String::from_utf8_lossy(a).to_ascii_uppercase())
            .unwrap_or_default()
    };
    let name = |i: usize| -> String {
        cmd.get(i)
            .map(|a| {
                String::from_utf8_lossy(a)
                    .trim_start_matches('@')
                    .to_string()
            })
            .unwrap_or_default()
    };
    let count = |i: usize| -> usize { word(i).parse::<usize>().unwrap_or(0) };
    let mut everything = false;
    let mut grouped = false;
    // (the row's field, the name to show it under). `ADDSCORES` shows the
    // score first in every row, whatever else is asked for, as Redis does:
    // it showed only when a `SORTBY` or `LOAD` named it, and a plain
    // `ADDSCORES` gave empty rows (#234).
    let mut shown: Vec<(String, String)> = Vec::new();
    if addscores {
        shown.push(("__score".to_string(), "__score".to_string()));
    }
    let mut i = 3usize;
    while i < cmd.len() {
        match word(i).as_str() {
            "LOAD" if word(i + 1) == "*" => {
                everything = true;
                i += 2;
            }
            "LOAD" => {
                let end = (i + 2 + count(i + 1)).min(cmd.len());
                let mut at = i + 2;
                while at < end {
                    let field = name(at);
                    at += 1;
                    let mut label = field.clone();
                    if at + 1 < end && word(at) == "AS" {
                        label = name(at + 1);
                        at += 2;
                    }
                    shown.push((field, label));
                }
                i = end;
            }
            "GROUPBY" => {
                grouped = true;
                i += 1;
            }
            "APPLY" => {
                let label = if word(i + 2) == "AS" {
                    name(i + 3)
                } else {
                    cmd.get(i + 1)
                        .map(|a| String::from_utf8_lossy(a).into_owned())
                        .unwrap_or_default()
                };
                shown.push((label.clone(), label));
                i += 2;
            }
            "SORTBY" => {
                let end = (i + 2 + count(i + 1)).min(cmd.len());
                for arg in &cmd[(i + 2).min(end)..end] {
                    if let Some(field) = arg.strip_prefix(b"@") {
                        let field = String::from_utf8_lossy(field).into_owned();
                        shown.push((field.clone(), field));
                    }
                }
                i = end;
            }
            _ => i += 1,
        }
    }

    let rows_out: Vec<crate::commands::search_tools::ShownRow> = rows
        .into_iter()
        .map(|row| {
            let mut pairs: Vec<(String, String)> = Vec::new();
            if everything || grouped {
                // A group is not a document, and has no score.
                if let Some(score) = row.get("__score").filter(|_| addscores && !grouped) {
                    pairs.push(("__score".to_string(), score.clone()));
                }
                let mut fields: Vec<(&String, &String)> =
                    row.iter().filter(|(k, _)| !k.starts_with("__")).collect();
                fields.sort();
                for (k, v) in fields {
                    pairs.push((k.clone(), v.clone()));
                }
            } else {
                let mut seen: Vec<&str> = Vec::new();
                for (field, label) in &shown {
                    if seen.contains(&label.as_str()) {
                        continue;
                    }
                    if let Some(value) = row.get(field) {
                        seen.push(label);
                        pairs.push((label.clone(), value.clone()));
                    }
                }
            }
            pairs
        })
        .collect();

    // `WITHCURSOR`: the first rows now, the rest as they are asked for.
    if let Some((count, max_idle)) = cursor {
        return Some(crate::commands::search_tools::open_cursor(
            ctx.resp,
            &index_name,
            rows_out,
            count,
            max_idle,
            timed_out,
        ));
    }
    Some(crate::commands::search_tools::rows_reply(
        ctx.resp,
        rows_out.len(),
        &rows_out,
        timed_out,
    ))
}
// ── format_f64 ────────────────────────────────────────────────────────────────
//
// Format a float for aggregate output.
// Integers display without decimal point; floats show up to 6 sig digits.
// This matches Redis's behaviour for numeric aggregate results.

pub(crate) fn format_f64(v: f64) -> String {
    if v.fract() == 0.0 && v.abs() < 1e15 {
        format!("{}", v as i64)
    } else {
        format!("{:.6}", v)
    }
}

// ── FT.TAGVALS ────────────────────────────────────────────────────────────────
//
// FT.TAGVALS index field_name
//
// Returns all distinct values stored in a TAG field across every indexed key.
// This is used by clients like RedisInsight for autocomplete dropdowns and
// by OM (Object Mapping) libraries to enumerate facet values.
//
// How it works:
//   1. Resolve the index by name.
//   2. Verify the named field exists and is of type TAG.
//   3. Walk every shard, collect hashes matching the index prefixes.
//   4. For each hash, read the TAG field value, split on the Redis TAG
//      separator ("|" or ","), trim, lowercase, and insert into a set.
//   5. Return the deduplicated set as a sorted RESP array.
//
// Rust lesson: we use an AHashSet<String> for O(1) deduplication while
// collecting, then collect into a Vec and sort before emitting. This is
// the standard collect-deduplicate-sort pattern.

fn ft_tagvals(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    if cmd.len() != 3 {
        return Some(resp_error("ERR syntax: FT.TAGVALS index field"));
    }

    let index_name = match std::str::from_utf8(cmd[1]) {
        Ok(s) => s,
        Err(_) => return Some(resp_error("ERR index name must be valid UTF-8")),
    };
    let field_name = match std::str::from_utf8(cmd[2]) {
        Ok(s) => s.to_string(),
        Err(_) => return Some(resp_error("ERR field name must be valid UTF-8")),
    };

    // Resolve the index and the field: where its value is stored, and how it
    // is split into tags. Cloned out, so the store lock is not held while the
    // shards are scanned.
    let (prefixes, tag_field, doc_on, doc_schema, db) = {
        let store = ctx.index_store.read();
        let def = match store.get(index_name) {
            Some(d) => d,
            None => {
                return Some(resp_error(&crate::commands::search::no_such_index(
                    index_name,
                )));
            }
        };
        let tag_field = def
            .field_by_attribute(&field_name)
            .filter(|f| matches!(f.field_type, FieldType::Tag))
            .map(|f| {
                (
                    f.name.clone(),
                    f.options.separator,
                    f.options.case_sensitive,
                )
            });
        (
            def.prefixes.clone(),
            tag_field,
            def.on.clone(),
            def.fields.clone(),
            def.db_index,
        )
    };
    let Some((stored_as, separator, case_sensitive)) = tag_field else {
        return Some(resp_error(
            "ERR field is not a TAG field or does not exist in index",
        ));
    };

    // Scan all shards of the index's database and collect distinct tag values.
    let mut distinct: ahash::AHashSet<String> = ahash::AHashSet::new();
    let db_ref = &ctx.store[db];

    for shard in db_ref.iter() {
        let map = shard.read();
        for (key, value) in map.iter() {
            let matches =
                prefixes.is_empty() || prefixes.iter().any(|p| key.starts_with(p.as_bytes()));
            if !matches {
                continue;
            }
            let Some(inner) = crate::vector::doc_fields(&doc_on, &doc_schema, value) else {
                continue;
            };
            let Some(s) = inner
                .get(&stored_as)
                .and_then(|v| std::str::from_utf8(v).ok())
            else {
                continue;
            };
            // The field's separator — it used to be split at `|` and `,`
            // whatever the schema said.
            for tag in crate::vector::filter::split_tags(s, separator) {
                distinct.insert(if case_sensitive {
                    tag.to_string()
                } else {
                    tag.to_lowercase()
                });
            }
        }
    }

    // In a cluster, the values of every master's documents.
    if crate::cluster::search::coordinates(ctx) {
        let owned: Vec<Vec<u8>> = ctx.cmd.iter().map(|a| a.to_vec()).collect();
        match crate::cluster::search::fan_out(ctx, &owned) {
            Ok(replies) => {
                for reply in replies {
                    if let crate::cluster::search::Reply::Array(items) = reply {
                        distinct.extend(items.iter().filter_map(|v| v.text()));
                    }
                }
            }
            Err(e) => return Some(resp_error(&e)),
        }
    }

    // Sort for deterministic output — clients often display these in UI
    let mut values: Vec<String> = distinct.into_iter().collect();
    values.sort_unstable();

    let items: Vec<Vec<u8>> = values.iter().map(|v| resp_bulk_str(v)).collect();
    Some(resp_array(items))
}

// ── FT.SYNUPDATE ─────────────────────────────────────────────────────────────
//
// FT.SYNUPDATE index synonym_group_id [SKIPINITIALSCAN] term [term ...]
//
// Creates or updates a synonym group. Multiple terms in a group are treated
// as equivalent during TEXT field search — searching for any one of them
// also matches the others.
//
// We store synonym groups inside IndexDef.synonyms (added above).
// The group_id is a string key; the value is a Vec<String> of terms.
//
// Rust lesson: `store.get_mut(key)` on a write-locked HashMap gives us
// &mut IndexDef so we can mutate synonyms in place. The entry API
// (`entry(...).or_insert_with(...)`) is idiomatic for "get or create".

fn ft_synupdate(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;

    // FT.SYNUPDATE index group_id [SKIPINITIALSCAN] term [term ...]
    if cmd.len() < 4 {
        return Some(resp_error(
            "ERR syntax: FT.SYNUPDATE index group_id term [term ...]",
        ));
    }

    let index_name = match std::str::from_utf8(cmd[1]) {
        Ok(s) => s.to_string(),
        Err(_) => return Some(resp_error("ERR index name must be valid UTF-8")),
    };
    let group_id = match std::str::from_utf8(cmd[2]) {
        Ok(s) => s.to_string(),
        Err(_) => return Some(resp_error("ERR group ID must be valid UTF-8")),
    };

    // Collect terms, skipping optional SKIPINITIALSCAN flag
    let terms: Vec<String> = cmd[3..]
        .iter()
        .filter_map(|b| std::str::from_utf8(b).ok())
        .filter(|s| !s.eq_ignore_ascii_case("SKIPINITIALSCAN"))
        .map(|s| s.to_lowercase())
        .collect();

    if terms.is_empty() {
        return Some(resp_error("ERR at least one term is required"));
    }

    let mut store = ctx.index_store.write();
    let def = match store.get_mut(&index_name) {
        Some(d) => d,
        None => {
            return Some(resp_error(&crate::commands::search::no_such_index(
                &index_name,
            )));
        }
    };

    // entry().or_insert_with() is the idiomatic Rust "get or create" pattern.
    // If the group already exists we extend it; if not we create it.
    let group = def.synonyms.entry(group_id).or_insert_with(Vec::new);
    for term in terms {
        if !group.contains(&term) {
            group.push(term);
        }
    }
    drop(store);

    Some(resp_simple("OK"))
}

// ── FT.SYNDUMP ────────────────────────────────────────────────────────────────
//
// FT.SYNDUMP index
//
// Returns all synonym groups as a flat array of alternating:
//   term, [group_id, group_id, ...]
//
// Redis's response format maps each *term* to the list of groups it belongs
// to (the inverse of how we store it). We invert on the fly.
//
// Example:
//   FT.SYNUPDATE idx grp1 hello world
//   FT.SYNUPDATE idx grp2 world earth
//   FT.SYNDUMP idx →
//     1) "hello"
//     2) 1) "grp1"
//     3) "world"
//     4) 1) "grp1"
//        2) "grp2"
//     5) "earth"
//     6) 1) "grp2"

fn ft_syndump(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    if cmd.len() != 2 {
        return Some(resp_error("ERR syntax: FT.SYNDUMP index"));
    }

    let index_name = match std::str::from_utf8(cmd[1]) {
        Ok(s) => s,
        Err(_) => return Some(resp_error("ERR index name must be valid UTF-8")),
    };

    let store = ctx.index_store.read();
    let def = match store.get(index_name) {
        Some(d) => d,
        None => {
            return Some(resp_error(&crate::commands::search::no_such_index(
                index_name,
            )));
        }
    };

    if def.synonyms.is_empty() {
        return Some(resp_array(vec![]));
    }

    // Invert: group_id → [term] becomes term → [group_id]
    // We use a BTreeMap here (not AHashMap) so output is sorted by term —
    // deterministic output matters for tests and client compatibility.
    let mut term_to_groups: std::collections::BTreeMap<&str, Vec<&str>> =
        std::collections::BTreeMap::new();

    for (group_id, terms) in &def.synonyms {
        for term in terms {
            term_to_groups
                .entry(term.as_str())
                .or_default()
                .push(group_id.as_str());
        }
    }

    // Build flat alternating array: term, [groups], term, [groups], ...
    let mut out: Vec<Vec<u8>> = Vec::new();
    for (term, mut groups) in term_to_groups {
        groups.sort_unstable(); // sort group IDs for determinism
        out.push(resp_bulk_str(term));
        let group_items: Vec<Vec<u8>> = groups.iter().map(|g| resp_bulk_str(g)).collect();
        out.push(resp_array(group_items));
    }

    Some(resp_array(out))
}

// ── FT.SPELLCHECK ─────────────────────────────────────────────────────────────
//
// FT.SPELLCHECK index query [DISTANCE distance] [TERMS INCLUDE|EXCLUDE dict ...]
//
// Returns spelling suggestions for unrecognized terms in the query.
// Redis compares query tokens against the indexed vocabulary (terms seen in
// TEXT fields) and returns candidates within edit distance N.
//
// Full implementation requires:
//   1. A vocabulary built from all indexed TEXT field values (expensive to
//      maintain incrementally — typically built at query time or periodically).
//   2. An edit-distance (Levenshtein) function.
//   3. Candidate generation (all words within distance N).
//
// We implement this properly with a real Levenshtein computation against the
// live vocabulary. The vocabulary is built on-demand by scanning indexed hashes
// for TEXT field values — this is O(n*m) where n=docs, m=text fields, but
// spell-check is a low-frequency operation so that's acceptable.
//
// Rust lesson: Levenshtein distance is a classic DP problem. We implement it
// with the standard two-row rolling array optimization: instead of an nƗm
// matrix we keep only two rows at a time, cutting memory from O(n*m) to O(m).

fn levenshtein(a: &str, b: &str) -> usize {
    let a: Vec<char> = a.chars().collect();
    let b: Vec<char> = b.chars().collect();
    let (na, nb) = (a.len(), b.len());

    if na == 0 {
        return nb;
    }
    if nb == 0 {
        return na;
    }

    // prev[j] = edit distance between a[0..0] and b[0..j]
    let mut prev: Vec<usize> = (0..=nb).collect();
    let mut curr: Vec<usize> = vec![0; nb + 1];

    for i in 1..=na {
        curr[0] = i; // deleting i chars from a to match empty b
        for j in 1..=nb {
            let cost = if a[i - 1] == b[j - 1] { 0 } else { 1 };
            curr[j] = (prev[j] + 1) // deletion
                .min(curr[j - 1] + 1) // insertion
                .min(prev[j - 1] + cost); // substitution
        }
        std::mem::swap(&mut prev, &mut curr);
    }

    prev[nb] // after the swap, prev holds the last computed row
}

fn ft_spellcheck(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    if cmd.len() < 3 {
        return Some(resp_error(
            "ERR syntax: FT.SPELLCHECK index query [DISTANCE n]",
        ));
    }

    let index_name = match std::str::from_utf8(cmd[1]) {
        Ok(s) => s.to_string(),
        Err(_) => return Some(resp_error("ERR index name must be valid UTF-8")),
    };
    let query = match std::str::from_utf8(cmd[2]) {
        Ok(s) => s.to_lowercase(),
        Err(_) => return Some(resp_error("ERR query must be valid UTF-8")),
    };

    // Parse optional DISTANCE
    let mut max_distance: usize = 1; // Redis default
    let mut i = 3;
    while i < cmd.len() {
        let tok = match std::str::from_utf8(cmd[i]) {
            Ok(s) => s.to_ascii_uppercase(),
            Err(_) => {
                i += 1;
                continue;
            }
        };
        if tok == "DISTANCE" {
            if let Some(d) = cmd
                .get(i + 1)
                .and_then(|b| std::str::from_utf8(b).ok())
                .and_then(|s| s.parse().ok())
            {
                max_distance = d;
            }
            i += 2;
        } else {
            i += 1;
        }
    }

    // The index first, as RediSearch looks it up before the rest.
    let Some(def) = ctx.index_store.read().get(&index_name).cloned() else {
        return Some(resp_error(&crate::commands::search::no_such_index(
            &index_name,
        )));
    };

    // The TERMS INCLUDE|EXCLUDE clauses.
    let mut include_dicts: Vec<String> = Vec::new();
    let mut exclude_dicts: Vec<String> = Vec::new();
    let mut j = 3usize;
    while j < cmd.len() {
        let tok = match std::str::from_utf8(cmd[j]) {
            Ok(s) => s.to_ascii_uppercase(),
            Err(_) => {
                j += 1;
                continue;
            }
        };
        if tok == "TERMS" {
            j += 1;
            let mode = match std::str::from_utf8(cmd.get(j).unwrap_or(&b"".as_ref())) {
                Ok(s) => s.to_ascii_uppercase(),
                Err(_) => {
                    j += 1;
                    continue;
                }
            };
            j += 1;
            // Collect dict names until next keyword
            while j < cmd.len() {
                let next = match std::str::from_utf8(cmd[j]) {
                    Ok(s) => s.to_ascii_uppercase(),
                    Err(_) => break,
                };
                if next == "TERMS" || next == "DISTANCE" || next == "DIALECT" {
                    break;
                }
                let dict_name = std::str::from_utf8(cmd[j]).unwrap_or("").to_string();
                match mode.as_str() {
                    "INCLUDE" => include_dicts.push(dict_name),
                    "EXCLUDE" => exclude_dicts.push(dict_name),
                    _ => {}
                }
                j += 1;
            }
        } else {
            j += 1;
        }
    }

    // The dictionaries, each of which must exist, the INCLUDE ones first,
    // as RediSearch checks them (before anything else is read). A query word
    // in an EXCLUDE dictionary is spelled right and gets no entry. The words
    // of an INCLUDE dictionary are offered as suggestions too, but a query
    // word that is only in one is still misspelled.
    let mut included: BTreeSet<String> = BTreeSet::new();
    let mut excluded: BTreeSet<String> = BTreeSet::new();
    {
        let dict_store = ctx.dict_store.read();
        for (names, into) in [
            (&include_dicts, &mut included),
            (&exclude_dicts, &mut excluded),
        ] {
            for dict_name in names {
                match dict_store.get(dict_name) {
                    Some(dict) => into.extend(dict.iter().cloned()),
                    None => {
                        return Some(resp_error(&format!("Dict does not exist: {dict_name}")));
                    }
                }
            }
        }
    }

    // The words some document holds, each with how many do, read in one
    // pass and cloned, so that the lock is let go of before the edit
    // distances are measured. A word no document holds any more is neither
    // spelled right nor offered, as in RediSearch once its GC has run.
    let vocab: std::collections::BTreeMap<String, usize> = {
        let text = def.text.read();
        text.live_words()
            .map(|(id, word)| (word.to_string(), text.df(id)))
            .collect()
    };

    // What RediSearch divides a word's document count by: every document of
    // the index (`docs.size - 1`), as `FT.INFO`'s `num_docs` counts them.
    let cluster = ctx.cluster.enabled.then_some(&**ctx.cluster);
    let total_docs =
        crate::commands::search::count_docs(&ctx.store[def.db_index], &def, cluster).max(1) as f64;

    // RediSearch's reply per misspelled word, RESP2:
    //   ["TERM", word, [[score, suggestion], ...]]
    // and RESP3: {"results": {word: [{suggestion: score}, ...], ...}}.
    // A suggestion's score is the share of the index's documents that hold
    // it (`spell_check.c`), best first, written as Redis writes a double; a
    // word only in an INCLUDE dictionary scores 0.
    let resp3 = ctx.resp == 3;
    let score = |docs: usize| -> Vec<u8> {
        let digits = crate::commands::helpers::format_score(docs as f64 / total_docs);
        if resp3 {
            format!(",{digits}\r\n").into_bytes()
        } else {
            resp_bulk_str(&digits)
        }
    };
    let mut out: Vec<Vec<u8>> = Vec::new();

    // The query's words as the index has them, so that `world_x` and
    // `users\.u_1` are the words the vocabulary holds.
    for token_clean in crate::vector::tokenize_for_vocab(&query) {
        if token_clean.is_empty()
            || vocab.contains_key(&token_clean)
            || excluded.contains(&token_clean)
        {
            continue; // correctly spelled — skip
        }

        let near = |word: &str| levenshtein(&token_clean, word) <= max_distance;
        let mut candidates: Vec<(usize, &str)> = vocab
            .iter()
            .filter(|(word, _)| near(word))
            .map(|(word, &docs)| (docs, word.as_str()))
            .collect();
        for word in &included {
            if !vocab.contains_key(word) && near(word) {
                candidates.push((0, word.as_str()));
            }
        }
        // Best first; words of one score in order, to be the same each time.
        candidates.sort_unstable_by(|a, b| b.0.cmp(&a.0).then_with(|| a.1.cmp(b.1)));

        let suggestions: Vec<Vec<u8>> = candidates
            .iter()
            .map(|&(docs, word)| {
                if resp3 {
                    resp_map(vec![resp_bulk_str(word), score(docs)])
                } else {
                    resp_array(vec![score(docs), resp_bulk_str(word)])
                }
            })
            .collect();

        if resp3 {
            out.push(resp_bulk_str(&token_clean));
            out.push(resp_array(suggestions));
        } else {
            out.push(resp_array(vec![
                resp_bulk_str("TERM"),
                resp_bulk_str(&token_clean),
                resp_array(suggestions),
            ]));
        }
    }

    if resp3 {
        return Some(resp_map(vec![resp_bulk_str("results"), resp_map(out)]));
    }
    Some(resp_array(out))
}

// ── Helpers for filtered search ──────────────────────────────────────────────

/// The nearest `k` of the documents offered to it, measured as they come:
/// what a filtered KNN keeps while the filter runs.
///
/// A filter used to hand its keys over as a set of cloned `String`s, which
/// the KNN then looked up again one by one. The scorer is offered each key
/// as the filter accepts it, measures it at once against the query (scaled
/// to unit length once, for a cosine), and keeps only the best `k`: a key is
/// copied only when it is among them so far.
///
/// Each offer takes the vector index's locks for itself and lets them go:
/// it runs under a shard's read lock, and a write to that shard can hold the
/// shard while it waits on them.
pub(crate) struct FilteredKnn<'a> {
    live: &'a LiveIndex,
    query: crate::vector::PreparedQuery<'a>,
    best: Best,
}

impl<'a> FilteredKnn<'a> {
    pub(crate) fn new(
        live: &'a LiveIndex,
        query: &'a [f32],
        k: usize,
        metric: &'a crate::vector::DistanceMetric,
    ) -> Self {
        Self {
            live,
            query: crate::vector::PreparedQuery::new(metric, query),
            best: Best::new(k),
        }
    }

    /// Measure the document at `key`, if the index holds a vector for it.
    #[inline]
    pub(crate) fn offer(&mut self, key: &str) {
        let dim = self.query.vector().len();
        let distance = match self.live {
            LiveIndex::Flat(idx) => {
                let vectors = idx.vectors.read();
                match vectors.get(key) {
                    Some(v) if v.len() == dim => self.query.to_raw(v),
                    _ => return,
                }
            }
            LiveIndex::Hnsw(idx) => {
                let Some(id) = idx.key_to_id.read().get(key).copied() else {
                    return;
                };
                let nodes = idx.nodes.read();
                match idx.vector_of(&nodes, id, key) {
                    Some(v) if v.len() == dim => self.query.to_unit(v),
                    _ => return,
                }
            }
        };
        self.best.offer(distance, key);
    }

    /// The nearest, nearest first.
    pub(crate) fn nearest(self) -> Vec<crate::vector::KnnResult> {
        self.best.into_sorted()
    }
}

/// The `k` smallest distances offered, with their keys: a max-heap whose
/// top is the one to beat.
pub(crate) struct Best {
    k: usize,
    heap: std::collections::BinaryHeap<Far>,
}

/// A distance ordered for the heap: `total_cmp`, so a NaN is the farthest.
struct Far(f32, String);

impl PartialEq for Far {
    fn eq(&self, other: &Self) -> bool {
        self.0.total_cmp(&other.0).is_eq()
    }
}
impl Eq for Far {}
impl PartialOrd for Far {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for Far {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.total_cmp(&other.0)
    }
}

impl Best {
    fn new(k: usize) -> Self {
        Self {
            k,
            heap: std::collections::BinaryHeap::with_capacity(k.min(4096) + 1),
        }
    }

    #[inline]
    fn offer(&mut self, distance: f32, key: &str) {
        if self.heap.len() < self.k {
            self.heap.push(Far(distance, key.to_string()));
        } else if let Some(mut top) = self.heap.peek_mut()
            && distance.total_cmp(&top.0).is_lt()
        {
            // In place: the key's buffer is reused.
            top.0 = distance;
            top.1.clear();
            top.1.push_str(key);
        }
    }

    fn into_sorted(self) -> Vec<crate::vector::KnnResult> {
        self.heap
            .into_sorted_vec()
            .into_iter()
            .map(|Far(score, key)| crate::vector::KnnResult { key, score })
            .collect()
    }
}

// ── Filtered KNN from postings ───────────────────────────────────────────────
//
// When postings name exactly the documents a filter matches (see
// `search::plan`), the nearest of them are found one of two ways:
//
// * few documents: each is measured (`knn_measured`) — exact, and as cheap
//   as it gets for a few thousand vectors;
// * many: the graph is searched, keeping only the documents the filter
//   matches (`vector::knn_hnsw_admitting`). The search walks every node, so
//   it costs about what an unfiltered one does while the filter lets most
//   through; as it lets fewer through, the walk gets longer. It is given as
//   many distances as measuring every document would cost, and falls back
//   to that when they run out.
//
// `HYBRID_POLICY ADHOC_BF` always measures; `BATCHES` always searches the
// graph, however long the walk. A FLAT index is always measured.
//
// Each way keeps the query's deadline: cut short, it answers with the
// nearest it found so far and says it was cut, and the command then applies
// `ON_TIMEOUT` as it does after a scan.

/// The most documents a filtered KNN measures one by one rather than search
/// the graph for, by default. Measuring costs a distance per document; a
/// search that keeps a fraction `s` of the nodes it meets walks some `ef / s`
/// of them, and more for the graph's detours. The two cost the same at
/// `n = sqrt(c * ef * N)`; `c` is measured. On dbpedia-openai-100K-1536
/// (an Azure D16s_v5, AVX-512) the two cost the same at about 10,000
/// documents for ef 64, 13,500 for 128 and 16,000 for 256: `c` 16, 14 and
/// 10. Measuring a document of 1536 dimensions costs some 0.4 µs there; the
/// graph search kept the recall of an unfiltered one from 10% of documents
/// up.
pub(crate) fn measure_up_to(ef: usize, graph_size: usize) -> usize {
    const C: f64 = 12.0;
    let crossing = (C * ef as f64 * graph_size as f64).sqrt() as usize;
    crossing.max(2000)
}

/// The nearest `knn.k` of the documents `filter` matches, from postings, or
/// `None` if they do not name exactly those documents and the caller is to
/// find them by trying the filter. The second value is whether the deadline
/// cut the search short.
#[allow(clippy::too_many_arguments)]
pub(crate) fn knn_planned(
    ctx: &Ctx,
    def: &crate::vector::IndexDef,
    filter: &crate::vector::filter::Filter,
    only: Option<&ahash::AHashSet<String>>,
    knn: &crate::vector::filter::Knn,
    field: &crate::vector::SchemaField,
    deadline: Option<std::time::Instant>,
) -> Option<(Vec<crate::vector::KnnResult>, bool)> {
    let FieldType::Vector { metric, algo, .. } = &field.field_type else {
        return None;
    };
    let live = def.indexes.get(&knn.field)?;
    let mut plan = crate::commands::search::plan(ctx, def, filter, only)?;
    if !plan.exact {
        return None;
    }
    let ef = knn
        .ef_runtime
        .unwrap_or(match algo {
            VectorAlgo::Hnsw { ef_runtime, .. } => *ef_runtime,
            VectorAlgo::Flat => knn.k * 2,
        })
        .max(knn.k)
        .max(50);
    let db = &ctx.store[def.db_index];
    // A document overwritten by a value of another type stays in the
    // postings until something deletes it; one that comes up among the
    // nearest is taken out, and the search run again without it.
    for _ in 0..4 {
        let (nearest, cut) = knn_among(def, &plan.ids, knn, live, metric, ef, deadline);
        let gone: Vec<&str> = nearest
            .iter()
            .map(|r| r.key.as_str())
            .filter(|key| {
                let raw = crate::key_bytes::bytes_of(key);
                !crate::store::get_shard(db, &*raw)
                    .read()
                    .get(&*raw)
                    .is_some_and(|v| crate::vector::is_document(&def.on, v))
            })
            .collect();
        if gone.is_empty() || cut {
            return Some((nearest, cut));
        }
        for key in gone {
            if let Some(id) = def.postings.peek().id(key) {
                plan.ids.unset(id);
            }
            crate::vector::maybe_unindex(
                ctx.index_store,
                def.db_index,
                &*crate::key_bytes::bytes_of(key),
            );
        }
    }
    None
}

/// The nearest `knn.k` of the documents `ids`, by the policy above.
fn knn_among(
    def: &crate::vector::IndexDef,
    ids: &crate::vector::postings::Bits,
    knn: &crate::vector::filter::Knn,
    live: &LiveIndex,
    metric: &crate::vector::DistanceMetric,
    ef: usize,
    deadline: Option<std::time::Instant>,
) -> (Vec<crate::vector::KnnResult>, bool) {
    use crate::vector::filter::HybridPolicy;
    use std::sync::atomic::Ordering::Relaxed;
    let n = ids.count();
    if let LiveIndex::Hnsw(graph) = live {
        let size = graph.live_count.load(Relaxed);
        let search = match knn.policy {
            Some(HybridPolicy::AdhocBf) => false,
            Some(HybridPolicy::Batches) => size >= 500,
            None => size >= 500 && n > measure_up_to(ef, size),
        };
        if search {
            let budget = if knn.policy == Some(HybridPolicy::Batches) {
                usize::MAX
            } else {
                n
            };
            let prepared = crate::vector::PreparedQuery::new(metric, &knn.vector);
            let mut admit = |node: &crate::vector::HnswNode| {
                let mut doc = node.doc.load(Relaxed);
                if doc == crate::vector::postings::NO_DOC {
                    // Not known yet (a compaction, a snapshot, or a write
                    // that was queued made the node): looked up once, and
                    // kept. A document written before the query began has
                    // its id: the query's plan applied the queue.
                    match def.postings.peek().id(&node.key) {
                        Some(id) => {
                            node.doc.store(id, Relaxed);
                            doc = id;
                        }
                        None => return false,
                    }
                }
                ids.get(doc)
            };
            if let Some(found) = crate::vector::knn_hnsw_admitting(
                graph,
                prepared.vector(),
                knn.k,
                ef,
                metric,
                &mut admit,
                budget,
                deadline,
            ) {
                return found;
            }
        }
    }
    knn_measured(def, ids, knn, live, metric, deadline)
}

/// The nearest `knn.k` of the documents `ids`, each measured.
fn knn_measured(
    def: &crate::vector::IndexDef,
    ids: &crate::vector::postings::Bits,
    knn: &crate::vector::filter::Knn,
    live: &LiveIndex,
    metric: &crate::vector::DistanceMetric,
    deadline: Option<std::time::Instant>,
) -> (Vec<crate::vector::KnnResult>, bool) {
    let keys = crate::commands::search::keys_of(def, ids);
    let query = crate::vector::PreparedQuery::new(metric, &knn.vector);
    let dim = knn.vector.len();
    let mut best = Best::new(knn.k);
    // The index's locks are let go between runs, for writers.
    const RUN: usize = 256;
    let mut cut = false;
    for (i, run) in keys.chunks(RUN).enumerate() {
        if i > 0 && i % 4 == 0 && crate::commands::search::past_deadline(deadline) {
            cut = true;
            break;
        }
        match live {
            LiveIndex::Flat(idx) => {
                let vectors = idx.vectors.read();
                for key in run {
                    if let Some(v) = vectors.get(&**key)
                        && v.len() == dim
                    {
                        best.offer(query.to_raw(v), key);
                    }
                }
            }
            LiveIndex::Hnsw(graph) => {
                // The two locks one after the other, never together: an
                // insert takes the ids' for writing while it holds the
                // nodes'.
                let found: Vec<(usize, &Arc<str>)> = {
                    let key_to_id = graph.key_to_id.read();
                    run.iter()
                        .filter_map(|key| key_to_id.get(&**key).map(|&id| (id, key)))
                        .collect()
                };
                let nodes = graph.nodes.read();
                for (id, key) in found {
                    if let Some(v) = graph.vector_of(&nodes, id, key)
                        && v.len() == dim
                    {
                        best.offer(query.to_unit(v), key);
                    }
                }
            }
        }
    }
    (best.into_sorted(), cut)
}

/// Brute-force KNN over only the keys in `candidates`. Stops when
/// `deadline` has passed, with the nearest of the candidates measured so
/// far, and then the second value is true.
pub(crate) fn knn_filtered<'k>(
    live_index: &crate::vector::LiveIndex,
    query: &[f32],
    k: usize,
    metric: &crate::vector::DistanceMetric,
    candidates: impl IntoIterator<Item = &'k str>,
    deadline: Option<std::time::Instant>,
) -> (Vec<crate::vector::KnnResult>, bool) {
    let mut scorer = FilteredKnn::new(live_index, query, k, metric);
    // A distance per candidate: the clock is read every DEADLINE_EVERY.
    let mut seen = 0u32;
    let mut cut = false;
    for key in candidates {
        if crate::commands::search::deadline_due(deadline, &mut seen) {
            cut = true;
            break;
        }
        scorer.offer(key);
    }
    (scorer.nearest(), cut)
}

/// Backfill a subset of vector fields after FT.ALTER added them.
///
/// Different from `backfill_index` (which runs on FT.CREATE and walks every
/// vector field in the schema): this one only walks the newly added fields,
/// since the existing ones already have their data. Sharing the loop body
/// would mean either a flag-driven version of backfill_index or duplicating
/// the keyspace walk — duplicating is clearer at this scale.
fn backfill_new_vector_fields(ctx: &Ctx, index_name: &str, new_field_names: &[String]) {
    use crate::vector::FieldType;

    // Resolve metadata for each new field once, before the keyspace walk.
    // We hold the index store read lock only for this resolution; the walk
    // below takes shard read locks per shard and only re-enters the index
    // store via maybe_index... actually no, we call hnsw_insert / Flat
    // insert directly, so no further index_store locks needed.
    let store = ctx.index_store.read();
    let def = match store.get(index_name) {
        Some(d) => d,
        None => return, // index dropped between ALTER and backfill — fine
    };

    // Collect (field_name, dim, metric, live_index_arc) tuples.
    // We clone the Arc<LiveIndex variants> so we can drop the index_store
    // read lock before doing the long keyspace walk. Without the clone,
    // any concurrent FT.CREATE / DROPINDEX would block on us.
    struct FieldWork {
        name: String,
        dim: usize,
        data_type: crate::vector::VectorType,
        metric: crate::vector::DistanceMetric,
        live: LiveIndex,
    }
    let mut work: Vec<FieldWork> = Vec::with_capacity(new_field_names.len());
    for name in new_field_names {
        let vf = match def.vector_field_by_name(name) {
            Some(f) => f,
            None => continue,
        };
        let (dim, data_type, metric) = match &vf.field_type {
            FieldType::Vector {
                dim,
                data_type,
                metric,
                ..
            } => (*dim, *data_type, metric.clone()),
            _ => continue,
        };
        let live = match def.indexes.get(name) {
            Some(LiveIndex::Flat(a)) => LiveIndex::Flat(Arc::clone(a)),
            Some(LiveIndex::Hnsw(a)) => LiveIndex::Hnsw(Arc::clone(a)),
            None => continue,
        };
        work.push(FieldWork {
            name: name.clone(),
            dim,
            data_type,
            metric,
            live,
        });
    }
    // The definition, to walk its documents by, with the index store let go
    // of: a clone shares the live indexes.
    let def = def.clone();
    drop(store);

    if work.is_empty() {
        return;
    }

    // ── Walk the index's documents, populate the new vector fields ────────────
    //
    // A key at a time, under its write order, as it is now: see
    // `vector::walk_documents` (#275). The walk used to index the fields as
    // it found them, and a write to the key meanwhile, which indexed what it
    // wrote, could be overwritten in the index by the walk's older copy.
    crate::vector::walk_documents(&ctx.store[def.db_index], &def, |key, inner| {
        for w in &work {
            let raw_bytes = match inner.get(&w.name) {
                Some(b) => b,
                None => continue,
            };
            let Some(vec) = w.data_type.decode(raw_bytes, w.dim) else {
                continue;
            };

            match &w.live {
                LiveIndex::Flat(idx) => idx.insert(key.to_string(), vec),
                LiveIndex::Hnsw(idx) => {
                    crate::vector::hnsw_insert(idx, key.to_string(), vec, &w.metric);
                }
            }
        }
    });
}

// ── Alias commands ────────────────────────────────────────────────────────────
//
// An alias lets clients refer to an index by another name. It is stored as a
// name that resolves to the index (see `crate::vector::set_alias`), not as a
// copy of it.

/// `FT.ALIASADD` / `FT.ALIASUPDATE`: the alias and the index it should name.
fn alias_args(ctx: &Ctx, command: &str) -> Result<(String, String), Vec<u8>> {
    let cmd = &ctx.cmd;
    if cmd.len() != 3 {
        return Err(resp_error(&format!(
            "ERR wrong number of arguments for '{command}' command"
        )));
    }
    let alias =
        std::str::from_utf8(cmd[1]).map_err(|_| resp_error("ERR alias must be valid UTF-8"))?;
    let index = std::str::from_utf8(cmd[2])
        .map_err(|_| resp_error("ERR index name must be valid UTF-8"))?;
    Ok((alias.to_string(), index.to_string()))
}

fn ft_alias_add(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let (alias, index) = match alias_args(ctx, "FT.ALIASADD") {
        Ok(args) => args,
        Err(e) => return Some(e),
    };
    let mut store = ctx.index_store.write();
    if store.contains_key(&alias) {
        return Some(resp_error("ERR Alias already exists"));
    }
    // The target may itself be given by alias.
    let Some(real) = crate::vector::resolve_index_name(&store, &index) else {
        return Some(resp_error(&crate::commands::search::no_such_index(&index)));
    };
    let def = store[&real].clone();
    crate::vector::set_alias(&mut store, &alias, &def);
    Some(resp_simple("OK"))
}

fn ft_alias_del(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    if cmd.len() != 2 {
        return Some(resp_error(
            "ERR wrong number of arguments for 'FT.ALIASDEL' command",
        ));
    }
    let alias = match std::str::from_utf8(cmd[1]) {
        Ok(s) => s,
        Err(_) => return Some(resp_error("ERR alias must be valid UTF-8")),
    };
    let mut store = ctx.index_store.write();
    // Only an alias: this must not be a way to drop an index by its own name.
    if !store
        .get(alias)
        .is_some_and(|def| crate::vector::is_alias(alias, def))
    {
        return Some(resp_error("ERR Alias does not exist"));
    }
    store.remove(alias);
    Some(resp_simple("OK"))
}

fn ft_alias_update(ctx: &mut Ctx) -> Option<Vec<u8>> {
    // Same as ALIASADD, but an existing alias is moved to the new index.
    let (alias, index) = match alias_args(ctx, "FT.ALIASUPDATE") {
        Ok(args) => args,
        Err(e) => return Some(e),
    };
    let mut store = ctx.index_store.write();
    if store
        .get(&alias)
        .is_some_and(|def| !crate::vector::is_alias(&alias, def))
    {
        // It would replace the index of that name.
        return Some(resp_error("ERR Alias already exists as an index name"));
    }
    let Some(real) = crate::vector::resolve_index_name(&store, &index) else {
        return Some(resp_error(&crate::commands::search::no_such_index(&index)));
    };
    let def = store[&real].clone();
    crate::vector::set_alias(&mut store, &alias, &def);
    Some(resp_simple("OK"))
}

/// `FT.ALIASLIST index` (Redis 8.10): the aliases of an index — an array, or
/// a set under RESP3.
fn ft_alias_list(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    if cmd.len() != 2 {
        return Some(resp_error(
            "ERR wrong number of arguments for 'FT.ALIASLIST' command",
        ));
    }
    let index = match std::str::from_utf8(cmd[1]) {
        Ok(s) => s,
        Err(_) => return Some(resp_error("ERR index name must be valid UTF-8")),
    };
    let store = ctx.index_store.read();
    // `handle` has already resolved an alias to its index, so anything that
    // is not an index here does not exist.
    if store
        .get(index)
        .is_none_or(|def| crate::vector::is_alias(index, def))
    {
        return Some(resp_error(&crate::commands::search::no_such_index(index)));
    }
    let aliases: Vec<Vec<u8>> = crate::vector::index_aliases(&store, index)
        .iter()
        .map(|alias| resp_bulk_str(alias))
        .collect();
    let resp = crate::commands::handler::effective_resp_for_reply(ctx);
    Some(resp_set_for(resp, aliases))
}

// ── Dict commands ─────────────────────────────────────────────────────────────
//
// Redis uses these for custom stopword/synonym dictionaries used in TEXT
// field tokenisation.

fn ft_dictadd(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    // FT.DICTADD dict term [term ...]
    if cmd.len() < 3 {
        return Some(resp_error("ERR syntax: FT.DICTADD dict term [term ...]"));
    }
    let dict_name = match std::str::from_utf8(cmd[1]) {
        Ok(s) => s.to_string(),
        Err(_) => return Some(resp_error("ERR dict name must be valid UTF-8")),
    };

    let terms: Vec<String> = cmd[2..]
        .iter()
        .filter_map(|b| std::str::from_utf8(b).ok())
        .map(|s| s.to_lowercase())
        .collect();

    let mut store = ctx.dict_store.write();
    let dict = store.entry(dict_name).or_default();

    let mut added = 0i64;
    for term in terms {
        if dict.insert(term) {
            added += 1;
        }
    }

    drop(store);

    Some(resp_integer(added))
}

fn ft_dictdel(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    if cmd.len() < 3 {
        return Some(resp_error("ERR syntax: FT.DICTDEL dict term [term ...]"));
    }
    let dict_name = match std::str::from_utf8(cmd[1]) {
        Ok(s) => s,
        Err(_) => return Some(resp_error("ERR dict name must be valid UTF-8")),
    };

    let mut store = ctx.dict_store.write();
    let dict = match store.get_mut(dict_name) {
        Some(d) => d,
        None => return Some(resp_integer(0)), // dict doesn't exist — 0 removed
    };

    let mut removed = 0i64;
    for term_bytes in &cmd[2..] {
        if let Ok(term) = std::str::from_utf8(term_bytes)
            && dict.remove(&term.to_lowercase())
        {
            removed += 1;
        }
    }

    drop(store);

    Some(resp_integer(removed))
}

fn ft_dictdump(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    if cmd.len() != 2 {
        return Some(resp_error("ERR syntax: FT.DICTDUMP dict"));
    }
    let dict_name = match std::str::from_utf8(cmd[1]) {
        Ok(s) => s,
        Err(_) => return Some(resp_error("ERR dict name must be valid UTF-8")),
    };

    let store = ctx.dict_store.read();
    let dict = match store.get(dict_name) {
        Some(d) => d,
        None => return Some(resp_array(vec![])),
    };

    // Sort for deterministic output — same reason as FT.TAGVALS
    let mut words: Vec<&str> = dict.iter().map(|s| s.as_str()).collect();
    words.sort_unstable();

    let items: Vec<Vec<u8>> = words.iter().map(|w| resp_bulk_str(w)).collect();
    Some(resp_array(items))
}

// ── FT.DEBUG ──────────────────────────────────────────────────────────────────
//
// FT.DEBUG <subcommand> [args...]
//
// A multiplexer for debug/ops commands that need to inspect or manipulate
// internal index state. Current subcommands:
//
//   FT.DEBUG COMPACT <index>
//     Force-trigger HNSW compaction for the named index. Returns OK if a
//     compaction request was queued, ERR if the index doesn't exist or is
//     not HNSW. Same backend as the auto-threshold; the only difference is
//     that this bypasses the COMPACTION_MIN_TOMBSTONES and threshold checks.
//
// The subcommand pattern (FT.DEBUG <verb> ...) mirrors how Redis's own
// DEBUG command works, and how FT.CONFIG / FT.CURSOR work in this codebase.

fn ft_debug(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    if cmd.len() < 2 {
        return Some(resp_error("ERR syntax: FT.DEBUG <subcommand> [args...]"));
    }
    let sub = std::str::from_utf8(cmd[1])
        .map(|s| s.to_ascii_uppercase())
        .unwrap_or_default();

    match sub.as_str() {
        "COMPACT" => ft_debug_compact(ctx),
        "POSTINGS" => ft_debug_postings(ctx),
        "PAGE" => ft_debug_page(ctx),
        // The subcommands, by name, as Redis's `_FT.DEBUG HELP` lists its.
        "HELP" if cmd.len() == 2 => Some(resp_array(
            ["COMPACT", "POSTINGS", "PAGE", "HELP"]
                .iter()
                .map(|name| resp_bulk_str(name))
                .collect(),
        )),
        _ => Some(resp_error(&format!(
            "ERR unknown FT.DEBUG subcommand '{sub}'"
        ))),
    }
}

/// `FT.DEBUG POSTINGS index ON|OFF`: whether queries answer from the TAG
/// and NUMERIC postings or try every document, for comparing the two.
fn ft_debug_postings(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    let on = match cmd.get(3) {
        Some(w) if cmd.len() == 4 && w.eq_ignore_ascii_case(b"ON") => true,
        Some(w) if cmd.len() == 4 && w.eq_ignore_ascii_case(b"OFF") => false,
        _ => return Some(resp_error("ERR syntax: FT.DEBUG POSTINGS <index> ON|OFF")),
    };
    let name = String::from_utf8_lossy(cmd[2]).into_owned();
    let Some(def) = ctx.index_store.read().get(&name).cloned() else {
        return Some(resp_error(&crate::commands::search::no_such_index(&name)));
    };
    def.postings.write().unused = !on;
    Some(resp_simple("OK"))
}

/// `FT.DEBUG PAGE index ON|OFF`: whether a query whose page the postings
/// know reads only the documents of that page (`search::page_from_postings`)
/// or every document it matches, for comparing the two.
fn ft_debug_page(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    let on = match cmd.get(3) {
        Some(w) if cmd.len() == 4 && w.eq_ignore_ascii_case(b"ON") => true,
        Some(w) if cmd.len() == 4 && w.eq_ignore_ascii_case(b"OFF") => false,
        _ => return Some(resp_error("ERR syntax: FT.DEBUG PAGE <index> ON|OFF")),
    };
    let name = String::from_utf8_lossy(cmd[2]).into_owned();
    let Some(def) = ctx.index_store.read().get(&name).cloned() else {
        return Some(resp_error(&crate::commands::search::no_such_index(&name)));
    };
    def.postings.write().read_all = !on;
    Some(resp_simple("OK"))
}

fn ft_debug_compact(ctx: &mut Ctx) -> Option<Vec<u8>> {
    let cmd = &ctx.cmd;
    if cmd.len() != 4 {
        return Some(resp_error("ERR syntax: FT.DEBUG COMPACT <index> <field>"));
    }
    let index_name = match std::str::from_utf8(cmd[2]) {
        Ok(s) => s.to_string(),
        Err(_) => return Some(resp_error("ERR index name must be valid UTF-8")),
    };
    let field_name = match std::str::from_utf8(cmd[3]) {
        Ok(s) => s.to_string(),
        Err(_) => return Some(resp_error("ERR field name must be valid UTF-8")),
    };

    {
        let store = ctx.index_store.read();
        let def = match store.get(&index_name) {
            Some(d) => d,
            None => {
                return Some(resp_error(&crate::commands::search::no_such_index(
                    &index_name,
                )));
            }
        };
        match def.indexes.get(&field_name) {
            Some(crate::vector::LiveIndex::Hnsw(_)) => {}
            Some(_) => {
                return Some(resp_error(
                    "ERR FT.DEBUG COMPACT only supported on HNSW fields",
                ));
            }
            None => {
                return Some(resp_error("ERR no such vector field on index"));
            }
        }
    }

    let req = crate::vector::CompactionRequest {
        index_name,
        field_name,
    };
    let _ = ctx.compaction_tx.try_send(req);

    Some(resp_simple("OK"))
}

#[cfg(test)]
mod filtered_knn_tests {
    use super::*;

    fn vector(dim: usize, seed: u64) -> Vec<f32> {
        let mut state = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1;
        (0..dim)
            .map(|_| {
                state ^= state << 13;
                state ^= state >> 7;
                state ^= state << 17;
                ((state >> 40) as f32 / (1u64 << 24) as f32) * 2.0 - 1.0
            })
            .collect()
    }

    /// The nearest `k` keys of `keys`, by the plain distance: what the
    /// filtered KNN returned before.
    fn exact(
        data: &[(String, Vec<f32>)],
        keys: &[&str],
        q: &[f32],
        k: usize,
        metric: &crate::vector::DistanceMetric,
    ) -> Vec<String> {
        let mut scored: Vec<(f32, &str)> = data
            .iter()
            .filter(|(key, _)| keys.contains(&key.as_str()))
            .map(|(key, v)| (crate::vector::distance(metric, q, v), key.as_str()))
            .collect();
        scored.sort_by(|a, b| a.0.total_cmp(&b.0));
        scored
            .into_iter()
            .take(k)
            .map(|(_, key)| key.to_string())
            .collect()
    }

    #[test]
    fn the_scorer_keeps_the_exact_nearest_of_the_keys_offered() {
        use crate::vector::{DistanceMetric, HnswIndex, VectorIndex};
        let dim = 37;
        let data: Vec<(String, Vec<f32>)> = (0..600)
            .map(|i| (format!("doc:{i}"), vector(dim, i as u64 + 1)))
            .collect();
        // Every third key, as a filter would leave them, and one with no vector.
        let mut keys: Vec<&str> = data.iter().step_by(3).map(|(k, _)| k.as_str()).collect();
        keys.push("doc:none");
        for metric in [
            DistanceMetric::Cosine,
            DistanceMetric::L2,
            DistanceMetric::InnerProduct,
        ] {
            let flat = Arc::new(VectorIndex::new());
            let graph = Arc::new(HnswIndex::new(16, 64));
            for (key, v) in &data {
                flat.insert(key.clone(), v.clone());
                crate::vector::hnsw_insert(&graph, key.clone(), v.clone(), &metric);
            }
            let q = vector(dim, 9_999);
            for k in [0usize, 1, 10, 500] {
                let want = exact(&data, &keys, &q, k, &metric);
                for live in [
                    LiveIndex::Flat(flat.clone()),
                    LiveIndex::Hnsw(graph.clone()),
                ] {
                    let (got, cut) =
                        knn_filtered(&live, &q, k, &metric, keys.iter().copied(), None);
                    assert!(!cut);
                    let got_keys: Vec<String> = got.iter().map(|r| r.key.clone()).collect();
                    assert_eq!(got_keys.len(), want.len(), "{metric:?} k={k}");
                    // Equal up to the order of near ties.
                    let mut a = got_keys.clone();
                    let mut b = want.clone();
                    a.sort();
                    b.sort();
                    assert_eq!(a, b, "{metric:?} k={k}");
                    assert!(got.windows(2).all(|w| w[0].score <= w[1].score));
                }
            }
        }
    }
}