#!/usr/bin/env bash # 重建旧 KB 的 parent-child 索引(调用旧 bridge 容器里的 indexer),再回填 text_tsv。 # 旧表会由 indexer 归档为 kb_docs_vN;额外先做一份 kb_docs_backup_ 备份。 set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" DATE="$(date +%Y%m%d_%H%M%S)" docker exec crm-postgres psql -U postgres -d products -c \ "CREATE TABLE IF NOT EXISTS kb_docs_backup_${DATE} AS SELECT * FROM kb_docs;" >/dev/null docker exec crm-postgres psql -U postgres -d products <<'SQL' >/dev/null CREATE OR REPLACE FUNCTION kb_docs_tsv_update() RETURNS trigger AS $$ BEGIN NEW.text_tsv := to_tsvector('english', coalesce(NEW.question,'') || ' ' || coalesce(NEW.answer,'') || ' ' || coalesce(array_to_string(NEW.keywords,' '),'') || ' ' || coalesce(NEW.title,'') || ' ' || coalesce(NEW.section,'')); RETURN NEW; END; $$ LANGUAGE plpgsql; SQL docker exec crm-bridge node -e " const {loadConfig}=require('./config/loader'); const {createEmbeddingClient}=require('./utils/embedding'); const {createKbIndexer}=require('./faq/kbIndexer'); (async()=>{ const config=loadConfig(); const emb=createEmbeddingClient(config.emb, config.PG_URL); const idx=createKbIndexer({pool:emb.pool, embeddingClient:emb, keepVersions:2}); const res=await idx.indexNewVersion({skipIfUnchanged:false, note:'parent-child rebuild', aiGenerated:false}); console.log(JSON.stringify(res)); await emb.pool.end(); })().catch(e=>{console.error(e.message);process.exit(1);}); " cd "$ROOT" && node scripts/upgrade-kb-schema.js echo "[rebuild-kb-index] done; backup=kb_docs_backup_${DATE}"