File size: 56,982 Bytes
b92b744
 
 
 
0cb871f
b92b744
 
 
0cb871f
b92b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57a9a08
 
 
 
 
 
b92b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0cb871f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b92b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4e23c3
b92b744
 
 
a4e23c3
 
 
b92b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4e23c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b92b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86c2108
 
 
 
 
 
b92b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0cb871f
b92b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0cb871f
b92b744
57a9a08
b92b744
 
0cb871f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57a9a08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b92b744
86c2108
b92b744
 
 
 
 
 
 
 
 
 
 
86c2108
 
b92b744
 
 
 
 
 
 
86c2108
b92b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57a9a08
 
 
 
 
 
 
 
 
 
 
 
b92b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57a9a08
 
 
 
b92b744
 
 
 
 
 
57a9a08
b92b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Elasticsearch storage, source/extraction preservation, and full-match aggregations."""

import asyncio
import calendar
import contextlib
import math
import re
import unicodedata
from collections.abc import AsyncIterator
from datetime import UTC, date, datetime
from typing import Any

from elasticsearch import AsyncElasticsearch, AuthorizationException, BadRequestError, NotFoundError
from elasticsearch.helpers import async_bulk

from app.config import Settings, settings
from app.statistics import (
    DERIVED_SCALES,
    INCONCLUSIVE_REASONS,
    _analysis_type,
    _completion_date,
    _margin,
    _unreported,
    assign_bucket,
    derived_field,
    stored_analysis,
)

BUCKETS = ("effect", "credible_null", "reported_null", "inconclusive", "failed", "unreported")
# Direction is a property of an effect, not a bucket: these partition the effect bucket.
EFFECT_DIRECTIONS = {
    "favours_intervention": "favoursIntervention",
    "favours_comparator": "favoursComparator",
    "unclear": "unclear",
}
_CACHE_FIELDS = set(
    (
        "extracted_at extraction_version extraction_status extraction_evidence evidence_span "
        "extraction_source fulltext_status "
        "population intervention comparator outcome outcome_unit n n_intervention n_comparator "
        "mean_intervention mean_comparator sd_intervention sd_comparator "
        "events_intervention events_comparator "
        "estimate ci_low ci_high p_value effect_type ci_level ci_sides p_value_operator has_control "
        "design primary_outcome_met reported_result result_direction effect_direction "
        "outcome_direction"
    ).split()
)
_LINK_FIELDS = set(
    (
        "linked_papers canonical_id canonical_ids registry_title registry_abstract registry_url "
        "paper_result_label possible_abstract_spin"
    ).split()
)
_PAPER_FIELDS = set(
    (
        "title abstract authors venue embedding embedding_model result_label null_score evidence_span "
        "classification_method abstract_available work_type snapshot_provenance pmcid"
    ).split()
)


# Embedded studies, the corpus the gap map is drawn from. Reviews stay in: they
# occupy the space too, and `gapmap` excludes them from a region's attempts.
_EMBEDDED_QUERY = {
    "bool": {"filter": [{"term": {"record_kind": "study"}}, {"exists": {"field": "embedding"}}]}
}
# Everything the map needs per document and nothing else: abstracts would
# multiply the bytes on the wire without changing a single position.
_MAP_FIELDS = [
    "embedding",
    "title",
    "year",
    "bucket",
    "record_kind",
    "is_review",
    "cited_by_count",
    "source",
]
_PIT_KEEP_ALIVE = "10m"


def _split_budget(limit: int, slices: int) -> list[int | None]:
    """Share a document budget between concurrent slices; no limit means no cap."""
    if limit <= 0:
        return [None] * slices
    base, extra = divmod(limit, slices)
    return [base + (1 if index < extra else 0) for index in range(slices)]


def index_mapping(dimensions: int = 384) -> dict:
    properties: dict[str, Any] = {}
    for name in (
        "record_kind source result_label bucket evidence_tier effect_type overall_status "
        "pmids nct_ids referenced_works result_pmids canonical_id canonical_ids outcome_unit "
        "p_value_operator ci_sides extraction_version extraction_status embedding_model "
        "analysis_effect_type effect_direction outcome_direction work_type "
        "pmcid extraction_source fulltext_status reported_result result_direction"
    ).split():
        properties[name] = {"type": "keyword"}
    for name in "title abstract population intervention comparator outcome".split():
        properties[name] = {"type": "text"}
    # Separate retrieval text allows normalization without changing source quotes.
    properties["search_text"] = {"type": "text", "analyzer": "english"}
    for name in "evidence_span why_stopped rationale".split():
        properties[name] = {"type": "text", "index": False}
    for name in (
        "estimate ci_low ci_high p_value null_score ci_level n enrollment_actual enrollment_planned "
        "analysis_estimate analysis_se analysis_ci_low analysis_ci_high mde "
        "n_intervention n_comparator mean_intervention mean_comparator "
        "sd_intervention sd_comparator events_intervention events_comparator"
    ).split():
        properties[name] = {"type": "double"}
    # Arm-level effects on every derivable scale, so a query on any scale can be
    # bucketed over the full match set without reading documents.
    for scale in DERIVED_SCALES:
        for name in ("estimate", "ci_low", "ci_high"):
            properties[derived_field(scale, name)] = {"type": "double"}
    for name in "year cited_by_count".split():
        properties[name] = {"type": "integer"}
    # Citation-authority signals. rank_feature rejects non-positive values, so
    # writers log-scale and leave the field absent instead of storing 0.
    for name in ("authority", "pagerank"):
        properties[name] = {"type": "rank_feature"}
    for name in (
        "is_retracted is_review has_results has_linked_publication has_control spin_flag "
        "possible_abstract_spin reporting_missing significant_but_trivial abstract_available"
    ).split():
        properties[name] = {"type": "boolean"}
    for name in (
        "extracted_at primary_completion_date publication_date received_at reporting_due_date"
    ).split():
        properties[name] = {"type": "date"}
    properties["embedding"] = {
        "type": "dense_vector",
        "dims": dimensions,
        "index": True,
        "similarity": "cosine",
        "index_options": {"type": "int8_hnsw"},
    }
    for name in (
        "registry_analyses",
        "extraction_evidence",
        "metadata",
        "linked_papers",
        "snapshot_provenance",
    ):
        properties[name] = {"type": "object", "enabled": False}
    return {"dynamic": False, "properties": properties}


_QUERY_STOPWORDS = set(
    """
a an the and or of in on at to for from with without by versus vs as is are was were be been
being do does did can could should would will may might whether that this these those their
its our your among into over under than compared comparing comparison effect effects efficacy
impact influence intervention interventions treatment treatments therapy therapies supplementation
supplement supplements supplemental administration administered oral daily receiving receive
change changes changing increase increased increasing decrease decreased decreasing reduce reduced
reducing reduction improvement improved improving improve decline severity symptoms symptom
outcome outcomes measure measures measured score scores level levels rate rates global function
functional study studies trial trials controlled randomized randomised placebo control group groups
expression expressed elevated elevation higher lower associated association status presence
""".split()
)
_QUERY_STEMS = ("depress", "cognit", "arthroscop", "osteoarthrit", "hypertens")
# Two or more letters then digits (kim1, il6, covid19). A single letter is left alone
# so the vitamin d3/d2 handling keeps its own tokens.
_JOINED_DESIGNATOR = re.compile(r"([a-z]{2,})(\d+)")


def _search_normalize(value: str) -> str:
    folded = "".join(
        char for char in unicodedata.normalize("NFKD", value) if not unicodedata.combining(char)
    ).casefold()
    # NFKD turns subscript ₃ into 3; also align VitaminD3 and vitamin D(3).
    return re.sub(
        r"\bvitamin[\s-]*d(?:[\s-]*\(?([23])\)?)?(?![a-z0-9])",
        lambda match: "vitamin d" + (match[1] or ""),
        folded,
    )


def _concept_terms(value: str) -> list[str]:
    terms = []
    for word in re.findall(r"[a-z0-9]+", _search_normalize(value)):
        if word in _QUERY_STOPWORDS:
            continue
        token = next((stem for stem in _QUERY_STEMS if word.startswith(stem)), word)
        if token not in terms:
            terms.append(token)
    return terms


def _concept_query(terms: list[str], fields: list[str], *, require_all: bool) -> dict:
    clauses = []
    for term in terms:
        if term in _QUERY_STEMS:
            # The stored standard analyzer does not stem depression/depressive.
            # A small, explicit morphology set avoids changing an existing index.
            clauses.append(
                {
                    "bool": {
                        "should": [
                            {"prefix": {field.split("^")[0]: {"value": term}}} for field in fields
                        ],
                        "minimum_should_match": 1,
                    }
                }
            )
        elif joined := _JOINED_DESIGNATOR.fullmatch(term):
            # The standard analyzer indexes "KIM-1" as kim + 1, so a typed "kim1" would
            # otherwise match only the few documents that spell it without the hyphen.
            spaced = " ".join(joined.groups())
            clauses.append(
                {
                    "bool": {
                        "should": [
                            {"multi_match": {"query": term, "fields": fields}},
                            {"multi_match": {"query": spaced, "fields": fields, "type": "phrase"}},
                        ],
                        "minimum_should_match": 1,
                    }
                }
            )
        else:
            clauses.append({"multi_match": {"query": term, "fields": fields}})
    minimum = len(clauses) if require_all else max(1, math.ceil(len(clauses) * 0.6))
    return {"bool": {"should": clauses, "minimum_should_match": minimum}}


_POPULATION_GENERIC = set(
    """
adult adults patient patients participant participants people person persons individual individuals
population populations subject subjects volunteer volunteers men women male males female females
child children pediatric paediatric adolescent adolescents young younger old older elderly aged age
ages aging ageing year years month months day days healthy general community dwelling communitydwelling
post menopausal postmenopausal premenopausal middle middleaged senior seniors who whom have has had
having diagnosed diagnosis confirmed suspected established symptomatic asymptomatic mild moderate severe
major minor history risk risks high higher low lower at living suffering experiencing presenting
scheduled elective undergoing eligible enrolled treated untreated baseline underlying previously
currently type mellitus chronic acute recent newly stage stages all any approximately least more less
""".split()
)
_ANATOMICAL_TERMS = set(
    """
knee knees hip hips elbow elbows shoulder shoulders ankle ankles foot feet wrist wrists hand hands
spine spinal lumbar cervical thoracic lung lungs breast prostate kidney kidneys renal heart cardiac
brain colon colorectal rectal liver hepatic skin eye eyes ear ears dental tooth teeth jaw
""".split()
)


# An alias made only of these words would admit nearly every clinical record.
_ALIAS_TOO_BROAD = set(
    "disease diseases disorder disorders syndrome syndromes condition conditions illness "
    "illnesses complication complications health".split()
)
_MAX_POPULATION_ALIASES = 4


def _specific_terms(value: str) -> list[str]:
    return [
        term
        for term in _concept_terms(value)
        if term not in _POPULATION_GENERIC and not term.isdecimal()
    ]


def _population_terms(pico: dict) -> list[str]:
    return _specific_terms(pico.get("population") or "")


def _population_alias_terms(pico: dict) -> list[list[str]]:
    """Model-proposed equivalent condition names, reduced to specific terms.

    Aliases widen the full match set and therefore every bucket count, so an
    alias that adds nothing specific, or merely repeats the population, is dropped.
    """
    primary = set(_population_terms(pico))
    result: list[list[str]] = []
    for alias in list(pico.get("populationAliases") or [])[:_MAX_POPULATION_ALIASES]:
        terms = _specific_terms(str(alias))
        if not terms or terms in result:
            continue
        if all(term in _ALIAS_TOO_BROAD or term in _ANATOMICAL_TERMS for term in terms):
            continue
        specific = {term for term in terms if term not in _ALIAS_TOO_BROAD}
        if specific <= primary or primary <= specific:
            # A subset is broader than the population; a superset is already matched by it.
            continue
        result.append(terms)
    return result


def population_query(pico: dict) -> dict | None:
    """Constrain explicit conditions/sites; generic demographic descriptions only rank.

    This guard also applies to reference expansion. It deliberately does not try
    to infer diagnostic synonyms itself: absent textual support is a retrieval limitation.
    The parser may supply ``populationAliases`` (equivalent names of the same condition);
    each is a full alternative to the condition clause and is ignored without a population.
    Multiple anatomical sites are alternatives under the same condition.
    """
    terms = _population_terms(pico)
    if not terms:
        return None
    fields = ["population^3", "title^3", "abstract", "search_text"]
    anatomy = [term for term in terms if term in _ANATOMICAL_TERMS]
    condition = [term for term in terms if term not in _ANATOMICAL_TERMS]
    clauses = []
    if anatomy:
        clauses.append(
            {
                "bool": {
                    "should": [
                        _concept_query([term], fields, require_all=True) for term in anatomy
                    ],
                    "minimum_should_match": 1,
                }
            }
        )
    if condition:
        clauses.append(_concept_query(condition, fields, require_all=True))
    primary = {"bool": {"must": clauses}}
    alternatives = []
    for alias in _population_alias_terms(pico):
        must = [_concept_query(alias, fields, require_all=True)]
        # An alias that drops the anatomical site must not escape it: "OA" still needs "knee".
        if anatomy and not any(term in _ANATOMICAL_TERMS for term in alias):
            must.append(clauses[0])
        alternatives.append({"bool": {"must": must}})
    if not alternatives:
        return primary
    return {"bool": {"should": [primary, *alternatives], "minimum_should_match": 1}}


# ClinicalTrials.gov records have no citation count: ingestion writes 0 for every
# registry row. A citation bound applied to them would delete exactly the
# terminated and never-reported trials this tool exists to surface, so registry
# rows are exempt from citation bounds and the pipeline warns that they are.
# Publication dates do exist for registry rows (the primary completion date), so
# a date bound applies to papers and trials alike.
CITATION_EXEMPT_SOURCES = ("ctgov", "merged")


def citation_range(filters: dict | None) -> dict | None:
    """The citation bound on its own, or None when the request set neither end."""
    if not filters:
        return None
    bounds = {}
    if filters.get("minCitations") is not None:
        bounds["gte"] = int(filters["minCitations"])
    if filters.get("maxCitations") is not None:
        bounds["lte"] = int(filters["maxCitations"])
    return {"range": {"cited_by_count": bounds}} if bounds else None


def registry_citation_exemption_query(query: dict, filters: dict | None) -> dict | None:
    """Rows in the match set only because citation bounds skip the registry.

    Counting these makes the exemption visible: it is the number of trial records
    the citation requirement would have removed had it applied to them.
    """
    bound = citation_range(filters)
    if bound is None:
        return None
    return {
        "bool": {
            "must": [query],
            "filter": [{"terms": {"source": list(CITATION_EXEMPT_SOURCES)}}],
            "must_not": [bound],
        }
    }


def filter_clauses(filters: dict | None) -> list[dict]:
    """Pre-search corpus restrictions, as filter clauses over the full match set.

    These belong in the query's ``filter``, never in ``should``: the caller's
    expanded review references and every aggregation share that clause list, so a
    filtered-out record cannot re-enter the study list while the counts exclude it.
    A record with no ``publication_date`` cannot satisfy a date bound and drops out.
    """
    if not filters:
        return []
    clauses: list[dict] = []
    dates = {}
    if filters.get("yearFrom") is not None:
        dates["gte"] = f"{int(filters['yearFrom']):04d}-01-01"
    if filters.get("yearTo") is not None:
        dates["lte"] = f"{int(filters['yearTo']):04d}-12-31"
    if dates:
        clauses.append({"range": {"publication_date": dates}})
    citations = citation_range(filters)
    if citations is not None:
        clauses.append(
            {
                "bool": {
                    "should": [
                        citations,
                        {"terms": {"source": list(CITATION_EXEMPT_SOURCES)}},
                    ],
                    "minimum_should_match": 1,
                }
            }
        )
    return clauses


def lexical_query(
    pico: dict,
    idea: str,
    expanded_ids: list[str] | None = None,
    filters: dict | None = None,
) -> dict:
    """Require intervention/outcome plus explicit disease or anatomical scope.

    Untyped model synonyms are boosts, not alternative mandatory concepts: a mood
    synonym must not let a depression-only paper satisfy the vitamin D clause.
    Verified review references remain an explicit extension of this match universe.
    """
    intervention = _concept_terms(pico.get("intervention") or "")
    outcome = _concept_terms(pico.get("outcome") or "")
    intervention_fields = ["intervention^5", "title^3", "abstract", "search_text"]
    outcome_fields = ["outcome^5", "title^3", "abstract", "search_text"]
    must = []
    if intervention and outcome:
        if "arthroscop" in intervention:
            # "Arthroscopic" already supplies the surgical anchor. Preserve
            # an explicit choice of lavage OR debridement instead of requiring both.
            intervention = [term for term in intervention if term not in {"surgery", "surgical"}]
        primary_intervention = _concept_query(intervention, intervention_fields, require_all=True)
        if (
            "arthroscop" in intervention
            and {"lavage", "debridement"}.issubset(intervention)
            and re.search(r"\bor\b|/", _search_normalize(pico.get("intervention") or ""))
        ):
            anchor = [term for term in intervention if term not in {"lavage", "debridement"}]
            primary_intervention = {
                "bool": {
                    "must": [
                        _concept_query(anchor, intervention_fields, require_all=True),
                        {
                            "bool": {
                                "should": [
                                    _concept_query(
                                        [procedure], intervention_fields, require_all=True
                                    )
                                    for procedure in ("lavage", "debridement")
                                ],
                                "minimum_should_match": 1,
                            }
                        },
                    ]
                }
            }
        intervention_options = [primary_intervention]
        # Established intervention aliases for the three seeded clinical topics.
        aliases = list(pico.get("interventionAliases", []))[:6]
        if "vitamin" in intervention and any(term in intervention for term in ("d", "d3", "d2")):
            aliases += [
                "cholecalciferol",
                "ergocalciferol",
                "calciferol",
                "vitamin d3",
                "vitamin d2",
            ]
        elif "omega" in intervention and "3" in intervention:
            aliases += [
                "fish oil",
                "n 3 fatty acids",
                "eicosapentaenoic acid",
                "docosahexaenoic acid",
            ]
        for alias in aliases:
            terms = _concept_terms(alias)
            if terms:
                intervention_options.append(
                    _concept_query(terms, intervention_fields, require_all=True)
                )
        outcome_options = [_concept_query(outcome, outcome_fields, require_all=False)]
        outcome_aliases = list(pico.get("outcomeAliases", []))[:6]
        population_terms = _population_terms(pico)
        if "pain" in outcome and "knee" in population_terms and "osteoarthrit" in population_terms:
            # WOMAC is a knee/hip OA pain-stiffness-function composite. Retrieve
            # it in this narrow context; retain the actual endpoint for statistics.
            # Instrument validation: https://pubmed.ncbi.nlm.nih.gov/3068365/
            outcome_aliases.append("WOMAC")
        for alias in outcome_aliases:
            terms = _concept_terms(alias)
            if terms:
                outcome_options.append(_concept_query(terms, outcome_fields, require_all=True))
        must = [
            {"bool": {"should": intervention_options, "minimum_should_match": 1}},
            {"bool": {"should": outcome_options, "minimum_should_match": 1}},
        ]
    else:
        # With no reliable PICO pair, retain a transparent original-query fallback.
        must = [
            {
                "multi_match": {
                    "query": idea,
                    "fields": [
                        "title^3",
                        "abstract",
                        "intervention^3",
                        "outcome^2",
                        "population",
                        "search_text",
                    ],
                    "minimum_should_match": "60%",
                }
            }
        ]
    boosts = []
    if pico.get("population"):
        boosts.append(
            {
                "multi_match": {
                    "query": pico["population"],
                    "fields": ["population^2", "title", "abstract"],
                    "boost": 0.3,
                }
            }
        )
    synonyms = [str(term) for term in pico.get("synonyms", []) if term]
    if synonyms:
        boosts.append(
            {
                "multi_match": {
                    "query": " ".join(synonyms),
                    "fields": ["title", "abstract", "intervention", "outcome"],
                    "boost": 0.3,
                }
            }
        )
    candidate = {"bool": {"must": must, "should": boosts, "minimum_should_match": 0}}
    should = [candidate]
    if expanded_ids:
        should.append({"ids": {"values": list(dict.fromkeys(expanded_ids))}})
    required = [{"term": {"record_kind": "study"}}]
    population = population_query(pico)
    if population is not None:
        required.append(population)
    required.extend(filter_clauses(filters))
    return {
        "bool": {
            "filter": required,
            "should": should,
            "minimum_should_match": 1,
        }
    }


def authority_query(query: dict) -> dict:
    """Rank the query's own match set by combined citation authority.

    Each ``rank_feature`` leaf contributes a saturation-scaled function of the
    stored log signals (``authority`` = log citation count, ``pagerank`` =
    within-corpus citation graph), and the ``should`` clauses sum them. The
    main query stays a filter, so authority re-ranks relevant hits only —
    an unrelated famous paper must not enter results on reputation alone.
    Documents with neither signal simply absent themselves from this leg.
    """
    return {
        "bool": {
            "filter": [query],
            "should": [
                {"rank_feature": {"field": "authority"}},
                {"rank_feature": {"field": "pagerank"}},
            ],
            "minimum_should_match": 1,
        }
    }


# How many ranked records one search carries forward. It has to cover the relevance screen
# (pipeline.SCREEN_LIMIT): a shorter list would silently cap what can be screened.
RETRIEVE_LIMIT = 500


def rrf_fuse(*rankings: list[dict], limit: int = RETRIEVE_LIMIT) -> list[dict]:
    scores: dict[str, float] = {}
    docs = {}
    for ranking in rankings:
        for rank, doc in enumerate(ranking, 1):
            key = doc["id"]
            scores[key] = scores.get(key, 0) + 1 / (60 + rank)
            docs[key] = doc
    return [docs[key] for key in sorted(scores, key=lambda key: (-scores[key], key))[:limit]]


# All numerical validation, transformations, reconstruction and failure rules run
# in statistics.assign_bucket at write time. This script only reapplies the query's
# margin to those validated 95% bounds, plus the time-sensitive reporting deadline.
# Pass doc explicitly to functions: Painless functions do not capture script locals.
# An inconclusive row is emitted as 'inconclusive:<reason>' (statistics.INCONCLUSIVE_REASONS)
# so one pass yields both the bucket and why; aggregate() folds the reasons back together.
BUCKET_SCRIPT = """
boolean present(def values, String key) {
    return values.containsKey(key) && values[key].size() > 0;
}
String text(def values, String key) {
    return present(values, key) ? values[key].value.toString() : '';
}
boolean flag(def values, String key) {
    return present(values, key) && values[key].value == true;
}
if (text(doc, 'bucket') == 'failed') { emit('failed'); return; }
if (flag(doc, 'is_review')) { emit('inconclusive:review'); return; }
if (flag(doc, 'reporting_missing') && present(doc, 'reporting_due_date')
    && doc['reporting_due_date'].value.toInstant().toEpochMilli() < params.today) {
    emit('unreported'); return;
}
if (!present(doc, 'analysis_ci_low') || !present(doc, 'analysis_ci_high')) {
    String source = text(doc, 'source');
    boolean comparative = flag(doc, 'has_control') || source == 'ctgov' || source == 'merged'
        || present(doc, 'nct_ids');
    boolean read = text(doc, 'extraction_status') == 'verified';
    String label = comparative && present(doc, 'reported_result') ? text(doc, 'reported_result')
        : (read && !comparative ? '' : text(doc, 'result_label'));
    emit(label == 'positive' ? 'effect' : (label == 'null' ? 'reported_null'
        : (label == 'mixed' ? 'inconclusive:mixed_result' : 'inconclusive:no_result')));
    return;
}
if (params.delta == null) { emit('inconclusive:no_threshold'); return; }
// The stored analysis is on the default scale; a query on another scale falls
// back to the arm-level effect derived for that scale, mirroring _numeric_evidence.
String prefix = 'analysis';
if (text(doc, 'analysis_effect_type') != params.effect) {
    if (params.derived == null || !present(doc, params.derived + '_ci_low')
        || !present(doc, params.derived + '_ci_high')) { emit('inconclusive:other_scale'); return; }
    prefix = params.derived;
}
double low = doc[prefix + '_ci_low'].value;
double high = doc[prefix + '_ci_high'].value;
if (low > -params.delta && high < params.delta) { emit('credible_null'); return; }
if ((low > 0 || high < 0) && present(doc, prefix + '_estimate')
    && Math.abs(doc[prefix + '_estimate'].value) >= params.delta) { emit('effect'); return; }
emit('inconclusive:wide_interval');
"""


def bucket_runtime(sesoi: float, effect_type: str, today: datetime | None = None) -> dict:
    """Runtime field applying BUCKET_SCRIPT with one query's margin and effect scale."""
    today = (today or datetime.now(UTC)).replace(hour=0, minute=0, second=0, microsecond=0)
    scale = _analysis_type(effect_type)
    return {
        "query_bucket": {
            "type": "keyword",
            "script": {
                "source": BUCKET_SCRIPT,
                "params": {
                    "delta": _margin(sesoi, effect_type),
                    "effect": scale,
                    "derived": derived_field(scale, "")[:-1] if scale in DERIVED_SCALES else None,
                    "today": int(today.timestamp() * 1000),
                },
            },
        }
    }


def _prepare_document(incoming: dict, existing: dict | None = None) -> dict:
    """Refresh source data while preserving a cache tied to unchanged source text."""
    old = existing or {}
    doc = {**old, **incoming}
    doc["record_kind"] = old.get("record_kind", incoming.get("record_kind", "study"))
    doc.setdefault("is_review", False)
    # A later registry refresh must not undo a proven paper/trial link.
    if old.get("source") == "merged" and incoming.get("source") == "ctgov":
        for key in _LINK_FIELDS | _PAPER_FIELDS:
            if key in old:
                doc[key] = old[key]
        doc.update(source="merged", has_linked_publication=True)
        for key in ("pmids", "result_pmids", "referenced_works"):
            doc[key] = sorted(set(old.get(key) or []) | set(incoming.get(key) or []))
    # Reingestion of a linked paper must not resurrect a second patient sample.
    if old.get("record_kind") == "linked_publication":
        for key in ("canonical_id", "canonical_ids"):
            if key in old:
                doc[key] = old[key]
    unchanged = old.get("abstract", "") == doc.get("abstract", "")
    cached = bool(old.get("extracted_at"))
    source_has_new_registry_numbers = incoming.get("source") in {"ctgov", "merged"} and bool(
        incoming.get("numeric_source")
    )
    if unchanged and cached and not source_has_new_registry_numbers:
        for key in _CACHE_FIELDS:
            if key in old:
                doc[key] = old[key]
    elif cached and (not unchanged or source_has_new_registry_numbers):
        # Partial updates need explicit nulls, otherwise removed cache keys survive.
        for key in _CACHE_FIELDS:
            doc[key] = incoming.get(key)
        # Registry-linked rows keep searchable literature text and its provenance.
        if old.get("source") == "merged" and incoming.get("source") == "ctgov":
            doc["evidence_span"] = incoming.get("evidence_span") or old.get("evidence_span", "")
    if not unchanged and "embedding" not in incoming:
        doc["embedding"] = None
    elif unchanged and incoming.get("embedding") is None:
        # get/mget may intentionally omit vector source; partial update preserves it.
        doc.pop("embedding", None)
    if not doc.get("publication_date"):
        year = doc.get("year")
        doc["publication_date"] = (
            f"{int(year):04d}-01-01" if year and 1 <= int(year) <= 9999 else None
        )
    doc["search_text"] = _search_normalize(
        " ".join(
            str(doc.get(field) or "")
            for field in (
                "title",
                "abstract",
                "population",
                "intervention",
                "comparator",
                "outcome",
            )
        )
    )
    # Normalization is the single source of truth for query-time numerical rules.
    doc.update(assign_bucket(doc))
    doc.update(stored_analysis(doc))
    completion = _completion_date(doc.get("primary_completion_date"))
    doc["primary_completion_date"] = completion.isoformat() if completion else None
    # Substitute a known past date only to ask the same strict metadata predicate
    # whether results are confirmed absent. Recency is evaluated separately.
    doc["reporting_missing"] = _unreported(
        {**doc, "primary_completion_date": "1900-01-01"}, date(2000, 1, 1)
    )
    doc["reporting_due_date"] = None
    if completion is not None and completion.year < 9999:
        anniversary = completion.replace(
            year=completion.year + 1,
            day=min(completion.day, calendar.monthrange(completion.year + 1, completion.month)[1]),
        )
        doc["reporting_due_date"] = anniversary.isoformat()
    # rank_feature rejects non-positive values, so authority is absent rather
    # than 0. pagerank is written only by the offline citation-graph pass and
    # survives reingest through the {**old, **incoming} merge above.
    cited_by = doc.get("cited_by_count")
    if (
        isinstance(cited_by, (int, float))
        and not isinstance(cited_by, bool)
        and math.isfinite(cited_by)
        and cited_by > 0
    ):
        doc["authority"] = math.log1p(cited_by)
    else:
        doc.pop("authority", None)
    return doc


class ElasticRepository:
    def __init__(self, config: Settings = settings, client: AsyncElasticsearch | None = None):
        self.config = config
        self.index = config.elastic_index
        self.client = client or AsyncElasticsearch(
            config.elastic_url,
            api_key=config.elastic_api_key
            if config.elastic_api_key and not config.elastic_local
            else None,
            request_timeout=30,
            max_retries=2,
            retry_on_timeout=True,
        )
        self._index_ready = False
        self._vector_source_filter = True
        self._index_lock = asyncio.Lock()

    async def close(self) -> None:
        await self.client.close()

    async def ensure_index(self) -> None:
        if self._index_ready:
            return
        async with self._index_lock:
            if self._index_ready:
                return
            mapping = index_mapping(self.config.embedding_dimensions)
            if not await self.client.indices.exists(index=self.index):
                try:
                    await self.client.indices.create(index=self.index, mappings=mapping)
                except BadRequestError as exc:
                    if "resource_already_exists_exception" not in str(exc):
                        raise
                    await self.client.indices.put_mapping(index=self.index, **mapping)
            else:
                # Add newly introduced explicit fields on existing deployments.
                await self.client.indices.put_mapping(index=self.index, **mapping)
            self._index_ready = True

    async def health(self) -> dict:
        await self.client.info()
        exists = await self.client.indices.exists(index=self.index)
        count = (
            await self.client.count(index=self.index, query={"term": {"record_kind": "study"}})
            if exists
            else {"count": 0}
        )
        return {"connected": True, "index": self.index, "studies": count["count"]}

    async def bulk_upsert(self, studies: list[dict]) -> int:
        if not studies:
            return 0
        await self.ensure_index()
        existing = {
            doc["id"]: doc
            for doc in await self.get_many(
                [study["id"] for study in studies], include_vectors=False
            )
        }
        actions = []
        for item in studies:
            doc = _prepare_document(item, existing.get(item["id"]))
            actions.append(
                {
                    "_op_type": "update",
                    "_index": self.index,
                    "_id": doc["id"],
                    "doc": doc,
                    "doc_as_upsert": True,
                }
            )
        count, _ = await async_bulk(self.client, actions, refresh="wait_for")
        return count

    async def get(self, study_id: str) -> dict | None:
        try:
            result = await self.client.get(index=self.index, id=study_id)
            return dict(result["_source"], id=result["_id"])
        except NotFoundError:
            return None

    async def get_many(self, ids: list[str], *, include_vectors: bool = True) -> list[dict]:
        if not ids:
            return []
        identifiers = list(dict.fromkeys(ids))
        if not include_vectors:
            result = await self.client.mget(
                index=self.index, ids=identifiers, source_excludes=["embedding"]
            )
        elif not self._vector_source_filter:
            result = await self.client.mget(index=self.index, ids=identifiers)
        else:
            # 9.2+ defaults to excluding vectors; 9.1 keeps them but does not
            # accept this newer source-filter option inside mget documents.
            try:
                result = await self.client.mget(
                    index=self.index,
                    docs=[
                        {"_id": identifier, "_source": {"exclude_vectors": False}}
                        for identifier in identifiers
                    ],
                )
            except BadRequestError as exc:
                message = str(exc).lower()
                if not any(
                    fragment in message
                    for fragment in ("exclude_vectors", "expected text", "unknown field")
                ):
                    raise
                self._vector_source_filter = False
                result = await self.client.mget(index=self.index, ids=identifiers)
        return [
            dict(item["_source"], id=item["_id"]) for item in result["docs"] if item.get("found")
        ]

    @staticmethod
    def hits(response: Any) -> list[dict]:
        return [dict(h["_source"], id=h["_id"]) for h in response["hits"]["hits"]]

    async def sample_embedded(self, limit: int = 4000, seed: int = 0) -> dict:
        """A deterministic random sample of embedded studies, plus what it omits.

        Random rather than top-cited: a map of where the literature is thin must
        not be drawn from its most popular corner. The counts travel with the
        sample so a caller can say which fraction of the corpus it describes.
        """
        await self.ensure_index()
        query = _EMBEDDED_QUERY
        total = await self.client.count(index=self.index, query=query)
        body = {
            "index": self.index,
            "size": max(0, limit),
            "query": {
                "function_score": {
                    "query": query,
                    "random_score": {"seed": seed, "field": "_seq_no"},
                    # A filter-only query scores every document 0, and the default
                    # multiply boost would turn the random score into 0 as well,
                    # making "the sample" the first `size` documents in index order
                    # — measured against the live index, that returned 4000 registry
                    # records and not one of the 567k papers.
                    "boost_mode": "replace",
                }
            },
            "source_includes": _MAP_FIELDS,
        }
        result = await self._search_with_vectors(body)
        return {"documents": self.hits(result), "corpus": total["count"]}

    async def count_embedded(self) -> int:
        """How many embedded studies exist, so coverage can be stated as a fraction."""
        await self.ensure_index()
        result = await self.client.count(index=self.index, query=_EMBEDDED_QUERY)
        return int(result["count"])

    async def scan_embedded(
        self,
        batch_size: int = 2000,
        slices: int = 8,
        limit: int = 0,
    ) -> AsyncIterator[list[dict]]:
        """Every embedded study, in batches, over one point in time.

        Paginated rather than fetched at once: the embedded corpus is millions of
        384-float vectors, so a single oversized search would neither fit in one
        response nor let a caller show anything before the last document arrived.
        Slices run concurrently because a single `search_after` walk is limited by
        round-trip latency, not by Elasticsearch — measured against the live index,
        one walk moved ~3.2k documents/s and sixteen slices ~11k/s.

        A point in time keeps the walk consistent, so the same document is never
        yielded twice and none is missed while the index is written to.
        """
        await self.ensure_index()
        slices = max(1, slices)
        pit = (await self.client.open_point_in_time(index=self.index, keep_alive=_PIT_KEEP_ALIVE))
        pit_id = pit["id"]
        queue: asyncio.Queue = asyncio.Queue(maxsize=slices * 2)
        budgets = _split_budget(limit, slices)

        async def walk(slice_id: int, budget: int | None) -> None:
            after: list | None = None
            taken = 0
            while budget is None or taken < budget:
                size = batch_size if budget is None else min(batch_size, budget - taken)
                # No index: a point in time already names one, and Elasticsearch
                # rejects a search that specifies both.
                body: dict[str, Any] = {
                    "size": size,
                    "query": _EMBEDDED_QUERY,
                    "pit": {"id": pit_id, "keep_alive": _PIT_KEEP_ALIVE},
                    "sort": [{"_shard_doc": "asc"}],
                    "source_includes": _MAP_FIELDS,
                }
                if slices > 1:
                    body["slice"] = {"id": slice_id, "max": slices}
                if after is not None:
                    body["search_after"] = after
                response = await self._search_with_vectors(body)
                hits = response["hits"]["hits"]
                if not hits:
                    return
                after = hits[-1]["sort"]
                taken += len(hits)
                await queue.put(self.hits(response))

        async def produce() -> None:
            try:
                await asyncio.gather(*(walk(index, budgets[index]) for index in range(slices)))
            finally:
                await queue.put(None)

        producer = asyncio.create_task(produce())
        try:
            while True:
                batch = await queue.get()
                if batch is None:
                    break
                yield batch
            await producer
        finally:
            producer.cancel()
            with contextlib.suppress(asyncio.CancelledError, Exception):
                await producer
            with contextlib.suppress(Exception):  # the walk is over either way
                await self.client.close_point_in_time(id=pit_id)

    async def _search_with_vectors(self, body: dict) -> Any:
        """Search keeping the stored vectors in `_source`, on 9.1 and on 9.2+."""
        if not self._vector_source_filter:
            return await self.client.search(**body)
        try:
            return await self.client.search(**body, source_exclude_vectors=False)
        except (BadRequestError, TypeError):
            self._vector_source_filter = False
            return await self.client.search(**body)

    async def knn_studies(
        self, vector: list[float], limit: int, filters: dict | None = None
    ) -> list[dict]:
        """Nearest studies to an arbitrary vector, each with its own embedding.

        The vectors travel back because the caller reports a cosine per concept,
        which cannot be recomputed without them. Elasticsearch scores a `cosine`
        dense vector as `(1 + cos) / 2`, so the score is converted back to the
        cosine rather than reported as an opaque relevance number.
        """
        await self.ensure_index()
        body = {
            "index": self.index,
            "size": limit,
            "knn": {
                "field": "embedding",
                "query_vector": vector,
                "k": limit,
                "num_candidates": min(10 * limit, 1000),
                "filter": [{"term": {"record_kind": "study"}}, *filter_clauses(filters)],
            },
            "source_includes": [
                "embedding",
                "title",
                "year",
                "url",
                "source",
                "bucket",
                "query_bucket",
                "cited_by_count",
            ],
        }
        result = await self._search_with_vectors(body)
        return [
            dict(hit["_source"], id=hit["_id"], cosine=2 * float(hit["_score"]) - 1)
            for hit in result["hits"]["hits"]
        ]

    async def retrieve(self, query: dict, vector: list[float] | None) -> tuple[list[dict], str]:
        base = {"index": self.index, "size": RETRIEVE_LIMIT, "source_excludes": ["embedding"]}
        # Third ranking signal: citation authority over the same match set.
        authority = authority_query(query)
        if vector is None:
            lexical, ranked = await asyncio.gather(
                self.client.search(**base, query=query),
                self.client.search(**base, query=authority),
            )
            return rrf_fuse(self.hits(lexical), self.hits(ranked)), "bm25"
        knn = {
            "field": "embedding",
            "query_vector": vector,
            "k": RETRIEVE_LIMIT,
            "num_candidates": 2 * RETRIEVE_LIMIT,
            "filter": query,
        }
        try:
            result = await self.client.search(
                **base,
                retriever={
                    "rrf": {
                        "rank_window_size": RETRIEVE_LIMIT,
                        "rank_constant": 60,
                        "retrievers": [
                            {"standard": {"query": query}},
                            {"knn": knn},
                            {"standard": {"query": authority}},
                        ],
                    }
                },
            )
            return self.hits(result), "hybrid_rrf"
        except (BadRequestError, AuthorizationException) as exc:
            # Only a license restriction permits fallback; auth/DSL errors must surface.
            message = str(exc).lower()
            if "license" not in message or not any(
                term in message for term in ("rrf", "rank fusion", "retriever")
            ):
                raise
            lexical, semantic, ranked = await asyncio.gather(
                self.client.search(**base, query=query),
                self.client.search(**base, knn=knn),
                self.client.search(**base, query=authority),
            )
            return (
                rrf_fuse(self.hits(lexical), self.hits(semantic), self.hits(ranked)),
                "hybrid_client_rrf",
            )

    async def screen_population(
        self, ids: list[str], pico: dict, filters: dict | None = None
    ) -> set[str]:
        """Apply the population guard and the corpus filters to expanded rows.

        Review references enter the study list without passing the lexical query,
        so they are screened here against the same conditions the aggregation uses.
        """
        identifiers = list(dict.fromkeys(ids))
        population = population_query(pico)
        restrictions = filter_clauses(filters)
        if not identifiers or (population is None and not restrictions):
            return set(identifiers)
        clauses = [
            {"ids": {"values": identifiers}},
            {"term": {"record_kind": "study"}},
        ]
        if population is not None:
            clauses.append(population)
        clauses.extend(restrictions)
        result = await self.client.search(
            index=self.index,
            size=len(identifiers),
            source=False,
            query={"bool": {"filter": clauses}},
        )
        return {hit["_id"] for hit in result["hits"]["hits"]}

    async def registry_sweep(self, query: dict) -> list[dict]:
        result = await self.client.search(
            index=self.index,
            size=100,
            query={
                "bool": {"must": [query], "filter": [{"terms": {"source": ["ctgov", "merged"]}}]}
            },
            source_excludes=["embedding"],
        )
        return self.hits(result)

    async def cache_extraction(self, study_id: str, extraction: dict) -> None:
        current = await self.get(study_id)
        if current is None:
            raise ValueError("Cannot cache extraction for a missing study")
        updated = _prepare_document({**current, **extraction})
        # Only cache and derived fields change; source vectors remain intact.
        derived = set(assign_bucket(updated)) | {
            "reporting_missing",
            "reporting_due_date",
            "search_text",
        }
        patch = {key: updated.get(key) for key in set(extraction) | derived}
        await self.client.update(index=self.index, id=study_id, doc=patch, refresh="wait_for")

    async def persist_links(self, merged: list[dict]) -> None:
        """Persist each canonical trial, then hide its duplicate publication rows."""
        if not merged:
            return
        paper_ids = list(
            dict.fromkeys(
                paper["id"]
                for trial in merged
                for paper in trial.get("linked_papers", [])
                if paper.get("id")
            )
        )
        papers = {paper["id"]: paper for paper in await self.get_many(paper_ids)}
        canonical_ids = {trial["id"] for trial in merged}
        links: dict[str, set[str]] = {}
        prepared = []
        for trial in merged:
            doc = dict(trial)
            doc["linked_papers"] = list(
                {
                    paper["id"]: paper
                    for paper in trial.get("linked_papers", [])
                    if paper.get("id")
                }.values()
            )
            for paper in doc["linked_papers"]:
                pid = paper["id"]
                if pid in canonical_ids:
                    continue
                links.setdefault(pid, set()).add(doc["id"])
                full_paper = papers.get(pid, {})
                if (
                    full_paper.get("abstract") == doc.get("abstract")
                    and full_paper.get("embedding") is not None
                ):
                    doc["embedding"] = full_paper["embedding"]
            prepared.append(doc)
        await self.bulk_upsert(prepared)
        actions = []
        for paper_id, targets in links.items():
            if paper_id not in papers:
                continue
            existing = papers[paper_id].get("canonical_ids") or []
            if isinstance(existing, str):
                existing = [existing]
            targets.update(existing)
            actions.append(
                {
                    "_op_type": "update",
                    "_index": self.index,
                    "_id": paper_id,
                    "doc": {"record_kind": "linked_publication", "canonical_ids": sorted(targets)},
                }
            )
        if actions:
            await async_bulk(self.client, actions, refresh="wait_for")

    @staticmethod
    def match_set(query: dict) -> dict:
        """The counted population: primary study records matching the query."""
        return {
            "bool": {
                "must": [query],
                "filter": [{"term": {"record_kind": "study"}}],
                "must_not": [{"term": {"is_review": True}}],
            }
        }

    async def count_studies(self, query: dict) -> int:
        """Size of one match set, so a filtered search can say what it removed.

        Uses the same predicate as ``aggregate``; otherwise the difference between
        a filtered and an unfiltered count would not be attributable to the filters.
        """
        await self.ensure_index()
        result = await self.client.count(index=self.index, query=self.match_set(query))
        return result["count"]

    async def aggregate(self, query: dict, sesoi: float, effect_type: str) -> dict:
        today = datetime.now(UTC).replace(hour=0, minute=0, second=0, microsecond=0)
        runtime = bucket_runtime(sesoi, effect_type, today)
        q = self.match_set(query)
        completed = {
            "bool": {
                "filter": [
                    {"term": {"overall_status": "COMPLETED"}},
                    {"terms": {"source": ["ctgov", "merged"]}},
                ]
            }
        }
        result = await self.client.search(
            index=self.index,
            query=q,
            size=0,
            track_total_hits=True,
            runtime_mappings=runtime,
            aggs={
                "buckets": {
                    "terms": {
                        "field": "query_bucket",
                        "size": len(BUCKETS) + len(INCONCLUSIVE_REASONS),
                    }
                },
                "years": {
                    "date_histogram": {
                        "field": "publication_date",
                        "calendar_interval": "year",
                        "min_doc_count": 1,
                    }
                },
                "nulls": {
                    "filter": {"terms": {"query_bucket": ["credible_null", "reported_null"]}},
                    "aggs": {
                        "sample": {
                            "sampler": {"shard_size": 300},
                            "aggs": {
                                "terms": {
                                    "significant_text": {
                                        "field": "abstract",
                                        "size": 12,
                                        "filter_duplicate_text": True,
                                        "background_filter": q,
                                    }
                                }
                            },
                        }
                    },
                },
                "completed": {
                    "filter": completed,
                    "aggs": {
                        "missing": {"filter": {"term": {"reporting_missing": True}}},
                        "overdue": {
                            "filter": {
                                "bool": {
                                    "filter": [
                                        {"term": {"reporting_missing": True}},
                                        {
                                            "range": {
                                                "reporting_due_date": {"lt": today.isoformat()}
                                            }
                                        },
                                    ]
                                }
                            }
                        },
                    },
                },
                "effect_directions": {
                    "filter": {"term": {"query_bucket": "effect"}},
                    "aggs": {
                        "directions": {
                            "terms": {
                                "field": "result_direction",
                                "missing": "unclear",
                                "size": len(EFFECT_DIRECTIONS),
                            }
                        }
                    },
                },
                "spin_candidates": {
                    "filter": {
                        "bool": {
                            "filter": [
                                {"term": {"source": "merged"}},
                                {"term": {"has_results": True}},
                            ]
                        }
                    },
                    "aggs": {"spin": {"filter": {"term": {"possible_abstract_spin": True}}}},
                },
            },
        )
        aggs = result["aggregations"]
        counts = dict.fromkeys(BUCKETS, 0)
        reasons = dict.fromkeys(INCONCLUSIVE_REASONS, 0)
        for row in aggs["buckets"]["buckets"]:
            bucket, _, reason = row["key"].partition(":")
            counts[bucket] += row["doc_count"]
            if reason:
                reasons[reason] += row["doc_count"]
        directions = dict.fromkeys(EFFECT_DIRECTIONS.values(), 0)
        for row in aggs["effect_directions"]["directions"]["buckets"]:
            # An unrecognized direction is not a fourth answer; it is one nobody stated.
            directions[EFFECT_DIRECTIONS.get(row["key"], "unclear")] += row["doc_count"]
        registered = aggs["completed"]["doc_count"]
        unreported = aggs["completed"]["missing"]["doc_count"]
        return {
            "total": result["hits"]["total"]["value"],
            "bucketCounts": counts,
            "inconclusiveReasons": reasons,
            "effectDirections": directions,
            "yearCounts": [
                {"year": int(b["key_as_string"][:4]), "count": b["doc_count"]}
                for b in aggs["years"]["buckets"]
            ],
            "nullTerms": [
                {"term": b["key"], "score": b["score"], "count": b["doc_count"]}
                for b in aggs["nulls"]["sample"]["terms"]["buckets"]
            ],
            "fileDrawer": {
                "completed": registered,
                "unreported": unreported,
                "overdue": aggs["completed"]["overdue"]["doc_count"],
                "share": unreported / registered if registered else None,
            },
            "spin": {
                "eligible": aggs["spin_candidates"]["doc_count"],
                "disagreements": aggs["spin_candidates"]["spin"]["doc_count"],
            },
        }