|
|
|
@ -930,7 +930,9 @@ public class DatabaseInitConfig { |
|
|
|
|
|
|
|
/** |
|
|
|
* 为 vector_store 表添加全文检索支持(tsvector 列 + GIN 索引 + 触发器) |
|
|
|
* 幂等操作:已存在则跳过 |
|
|
|
* 全幂等自愈:每一步都用幂等 SQL(ADD COLUMN IF NOT EXISTS / CREATE INDEX IF NOT EXISTS / |
|
|
|
* CREATE OR REPLACE + DROP TRIGGER IF EXISTS),不做「列已存在就 return」的早退, |
|
|
|
* 避免半途失败或触发器被手动删除后新文档 content_tsvector 恒为 NULL 的静默缺口。 |
|
|
|
*/ |
|
|
|
private void initVectorStoreFullTextSearch() { |
|
|
|
try { |
|
|
|
@ -940,19 +942,12 @@ public class DatabaseInitConfig { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 检查 content_tsvector 列是否已存在 |
|
|
|
String checkSql = "SELECT COUNT(*) FROM information_schema.columns WHERE table_name = 'vector_store' AND column_name = 'content_tsvector'"; |
|
|
|
Integer count = jdbcTemplate.queryForObject(checkSql, Integer.class); |
|
|
|
if (count != null && count > 0) { |
|
|
|
return; // 已初始化 |
|
|
|
} |
|
|
|
|
|
|
|
log.info("为 vector_store 添加全文检索支持"); |
|
|
|
log.info("初始化 vector_store 全文检索支持"); |
|
|
|
|
|
|
|
// 添加 tsvector 列 |
|
|
|
jdbcTemplate.execute("ALTER TABLE vector_store ADD COLUMN content_tsvector tsvector"); |
|
|
|
// 添加 tsvector 列(已存在则跳过) |
|
|
|
jdbcTemplate.execute("ALTER TABLE vector_store ADD COLUMN IF NOT EXISTS content_tsvector tsvector"); |
|
|
|
|
|
|
|
// 填充已有数据 |
|
|
|
// 回填已有数据(幂等:只补 NULL 行) |
|
|
|
jdbcTemplate.execute("UPDATE vector_store SET content_tsvector = to_tsvector('simple', coalesce(content, '')) WHERE content_tsvector IS NULL"); |
|
|
|
|
|
|
|
// GIN 索引 |
|
|
|
|