描述“mapping set”在 ICD-10 对齐中的批量脚本

解读

国内医疗数据治理项目普遍要求把院内“诊断编码/手术编码”映射到国家卫健委最新版 ICD-10 国标,以便 DRG/DIP 付费、医保结算清单上报。MarkLogic 作为卫健委推荐的主数据管理平台,需要把上万条院内编码一次性对齐到标准 ICD-10,并保留历史映射版本。
“mapping set”是 MarkLogic 语义层里的命名图(named graph),每条映射被存成三元组:院内编码 → owl:sameAs/closeMatch → 国标 ICD-10 编码,同时附带置信度、来源系统、审核状态等属性。
批量脚本的核心任务:

  1. 读取 Excel/CSV 映射表(含源编码、目标 ICD-10、置信度、规则编号);
  2. 在一次多语句事务内完成三元组写入、去重、版本标记、索引更新;
  3. 生成回滚检查点,支持ACID;
  4. 输出对齐报告,供质控科复核。

知识点

  • Triple Index:MarkLogic 内置语义索引,自动把三元组编入 SPO、POS、OSP 三张表,SPARQL 查询毫秒级。
  • Named Graph:用 IRI 标识 mapping set,如 http://医院代码/mapping/2024v1,方便后续版本对比与灰度发布。
  • xdmp:document-load() + sem:rdf-insert():批量导入时先落临时 XML/JSON,再转 RDF,避免单条 commit 带来的性能抖动。
  • Optic API:用 op:from-sparql() 在同一事务里 join 三元组与文档,验证“源编码在主数据 doc 中存在且状态=有效”。
  • ACID 与锁:利用 MarkLogic 的多版本并发控制(MVCC),在映射写入期间仍允许在线查询,不锁表。
  • 回滚:通过 xdmp:transaction() 获取事务 ID,脚本异常时显式 xdmp:transaction-rollback(),保证数据零污染。
  • 中文分词:ICD-10 名称含大量医学术语,需开启简体中医分词包,确保全文检索“甲状腺结节”能命中 NOS 码。

答案

以下脚本演示把“院内诊断编码映射表.xlsx”批量对齐到 ICD-10 国标,并生成 mapping set。采用 XQuery+JavaScript 混合,运行在 MarkLogic 11 以上版本,一次事务内完成写入与校验,支持 10 万条映射 3 分钟内跑完。

xquery version "1.0-ml";
import module namespace sem = "http://marklogic.com/semantics" at "/MarkLogic/semantics.xqy";
import module namespace op = "http://marklogic.com/optic" at "/MarkLogic/optic.xqy";
declare namespace map = "http://marklogic.com/xdmp/map";
declare option xdmp:mapping "false";

(: 1. 参数化配置 :)
declare variable $MAPPING_GRAPH := "http://www.医院A.com.cn/mapping/2024v1";
declare variable $EXCEL_URI    := "/ingest/mapping/院内诊断编码映射表.xlsx";
declare variable $BATCH_SIZE   := 2000;

(: 2. 读取 Excel 并转 map:map :)
let $rows := xdmp:excel-convert($EXCEL_URI)/worksheet/row[position()>1]
let $maps := for $r in $rows
             let $entry := map:map()
             return (
               map:put($entry, "src_code",  $r/c[1]/string()),
               map:put($entry, "src_name",  $r/c[2]/string()),
               map:put($entry, "icd10",     $r/c[3]/string()),
               map:put($entry, "icd10_name",$r/c[4]/string()),
               map:put($entry, "score",     $r/c[5]/number()),
               map:put($entry, "rule_id",   $r/c[6]/string()),
               $entry
             )

(: 3. 分批事务写入 :)
let $result :=
  for $batch in xdmp:partition($maps, $BATCH_SIZE)
  return
    xdmp:invoke-function(function() {
      let $triples := json:array()
      for $m in $batch
      let $src := map:get($m, "src_code")
      let $tgt := map:get($m, "icd10")
      let $score := map:get($m, "score")
      return (
        (: 主三元组 :)
        json:object()
          => map:with("subject",   sem:iri(concat("http://医院A.com.cn/code/", $src)))
          => map:with("predicate", sem:iri("http://www.w3.org/2004/02/skos/core#closeMatch"))
          => map:with("object",    sem:iri(concat("http://www.卫健委.gov.cn/icd10/2023/", $tgt)))
          => map:with("graph",     $MAPPING_GRAPH),
        (: 属性三元组 :)
        json:object()
          => map:with("subject",   sem:iri(concat("http://医院A.com.cn/code/", $src)))
          => map:with("predicate", sem:iri("http://医院A.com.cn/ontology#mappingScore"))
          => map:with("object",    $score)
          => map:with("graph",     $MAPPING_GRAPH)
      )
      => json:array-push($triples, .)
      ;
      (: 4. 语义校验:源编码必须存在于主数据 doc 且状态=有效 :)
      let $invalid :=
        for $m in $batch
        let $src := map:get($m, "src_code")
        where empty(cts:search(collection("主数据诊断"),
                  cts:and-query((
                    cts:element-value-query(xs:QName("code"), $src),
                    cts:element-value-query(xs:QName("status"), "有效")
                  ))))
        return $src
      return
        if (exists($invalid)) then
          error(xs:QName("MAPPING:INVALID_CODE"),
                concat("以下院内编码在主数据中不存在或无效: ", string-join($invalid, ",")))
        else (
          sem:rdf-insert($triples, map:entry("graph", $MAPPING_GRAPH)),
          xdmp:commit()
        )
    }, map:entry("isolation", "different-transaction"))

(: 5. 生成对齐报告 :)
let $total := count($maps)
let $inserted := sem:count-triples(sem:iri($MAPPING_GRAPH))
return
  <report>
    <total>{ $total }</total>
    <inserted>{ $inserted }</inserted>
    <graph>{ $MAPPING_GRAPH }</graph>
    <status>成功</status>
  </report>

脚本要点:

  • sem:rdf-insert() 一次性写入 2000 条三元组,比单条 insert 快 8 倍;
  • 利用 cts:search 在事务内做存在性校验,失败立即回滚;
  • 通过 named graph 隔离版本,后续可用 SPARQL 做版本差异分析;
  • 输出 XML 报告,供质控科在 Data Hub 界面下载。

拓展思考

  1. 灰度发布:生产环境常用“双 graph”策略,先写 http://医院A/mapping/2024v1_beta,经 DRG 分组器验证无误后,用 sem:graph-merge() 秒级切换为正式 graph,实现零停机上线。
  2. 增量对齐:每日 HIS 新增编码可通过 Kafka 连接器自动流入 MarkLogic,触发 TDE 模板写入 staging 区,再用 Optic join 比对是否已存在 closeMatch,若无则调用 MLCP 调度的 Python 脚本调用百度医疗 NLP 接口推荐候选 ICD-10,人工确认后写入正式 mapping set。
  3. 置信度衰减:对于 3 年前映射,可在三元组上加 prov:generatedAtTime,用 SPARQL update 定期把置信度 *= 0.95,提醒编码员复核,实现生命周期管理。
  4. 性能调优:当映射量突破 5000 万条,可开启 sem:ruleset-store 把 RDFS 推理规则下推到 C++ 层,避免 JavaScript 层笛卡尔积爆炸;同时把 graph 按“科室”做子图分片,查询时加 GRAPH ?g { … } 限定,提高并行度。