rebuild_rag_schema.sql
5.74 KB
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
-- Rebuild the independent RAG schema from scratch.
-- This script is intentionally destructive for RAG tables only.
-- Before running it in a shared environment, back up data and recreate the
-- Qdrant collection so DB rows and vector payloads stay consistent.
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS rag_query_logs;
DROP TABLE IF EXISTS rag_index_runs;
DROP TABLE IF EXISTS rag_chunks;
DROP TABLE IF EXISTS rag_files;
DROP TABLE IF EXISTS rag_documents;
SET FOREIGN_KEY_CHECKS = 1;
CREATE TABLE rag_documents (
id VARCHAR(36) NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
file_type VARCHAR(50) NOT NULL,
size BIGINT NOT NULL DEFAULT 0,
word_count INT NOT NULL DEFAULT 0,
status VARCHAR(50) NOT NULL DEFAULT 'Pending',
source_path VARCHAR(1024) NOT NULL,
content_hash VARCHAR(128) NULL,
metadata_json LONGTEXT NULL,
parser_version VARCHAR(50) NULL,
chunker_version VARCHAR(50) NULL,
block_count INT NOT NULL DEFAULT 0,
chunk_count INT NOT NULL DEFAULT 0,
parent_chunk_count INT NOT NULL DEFAULT 0,
child_chunk_count INT NOT NULL DEFAULT 0,
index_duration_ms INT NOT NULL DEFAULT 0,
vlm_page_count INT NOT NULL DEFAULT 0,
vlm_total_tokens INT NOT NULL DEFAULT 0,
vlm_duration_ms INT NOT NULL DEFAULT 0,
error_code VARCHAR(100) NULL,
error_detail_json LONGTEXT NULL,
error_message LONGTEXT NULL,
created_at DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
index_started_at DATETIME NULL,
indexed_at DATETIME NULL,
INDEX idx_rag_documents_status (status),
INDEX idx_rag_documents_hash (content_hash),
INDEX idx_rag_documents_name_status (name, status),
INDEX idx_rag_documents_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
COMMENT='RAG 文档表,记录进入独立 RAG 索引的源文件及索引统计信息';
-- 精简切片表:只保留检索/邻居扩展/定位/UI 真正用到的列;
-- 其余结构化标签统一进 metadata_json。检索过滤/打分走 Qdrant payload,不读这些列。
CREATE TABLE rag_chunks (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
chunk_id VARCHAR(64) NOT NULL,
document_id VARCHAR(36) NOT NULL,
parent_chunk_id VARCHAR(64) NULL,
chunk_level VARCHAR(20) NOT NULL,
chunk_index INT NOT NULL DEFAULT 0,
content LONGTEXT NOT NULL,
content_hash VARCHAR(128) NOT NULL,
token_estimate INT NOT NULL DEFAULT 0,
title_path VARCHAR(1024) NULL,
source_name VARCHAR(255) NOT NULL,
page_start INT NULL,
page_end INT NULL,
row_start INT NULL,
row_end INT NULL,
prev_chunk_id VARCHAR(64) NULL,
next_chunk_id VARCHAR(64) NULL,
status VARCHAR(50) NOT NULL DEFAULT 'active',
brand_series VARCHAR(100) NULL,
business_category VARCHAR(100) NULL,
content_kind VARCHAR(50) NULL,
section_title VARCHAR(255) NULL,
parse_status VARCHAR(50) NULL,
qdrant_point_id VARCHAR(64) NULL,
metadata_json LONGTEXT NULL,
created_at DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY uq_rag_chunks_chunk_id (chunk_id),
INDEX idx_rag_chunks_document_level (document_id, chunk_level),
INDEX idx_rag_chunks_document_level_index (document_id, chunk_level, chunk_index),
INDEX idx_rag_chunks_parent (parent_chunk_id),
INDEX idx_rag_chunks_status (status),
INDEX idx_rag_chunks_content_kind (content_kind),
INDEX idx_rag_chunks_business (business_category),
INDEX idx_rag_chunks_brand (brand_series),
INDEX idx_rag_chunks_source_loc (page_start, page_end)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
COMMENT='RAG 切片表,保存文档切片内容、层级关系、定位信息和轻量检索字段';
CREATE TABLE rag_index_runs (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
document_id VARCHAR(36) NOT NULL,
status VARCHAR(50) NOT NULL,
parser_version VARCHAR(50) NULL,
chunker_version VARCHAR(50) NULL,
block_count INT NOT NULL DEFAULT 0,
parent_chunk_count INT NOT NULL DEFAULT 0,
child_chunk_count INT NOT NULL DEFAULT 0,
vector_count INT NOT NULL DEFAULT 0,
duration_ms INT NOT NULL DEFAULT 0,
vlm_page_count INT NOT NULL DEFAULT 0,
vlm_total_tokens INT NOT NULL DEFAULT 0,
error_code VARCHAR(100) NULL,
error_detail_json LONGTEXT NULL,
error_message LONGTEXT NULL,
created_at DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
INDEX idx_rag_index_runs_document (document_id),
INDEX idx_rag_index_runs_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
COMMENT='RAG 索引任务运行记录表,保存每次索引执行的统计和错误信息';
CREATE TABLE rag_query_logs (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
session_id VARCHAR(100) NULL,
query_text LONGTEXT NOT NULL,
rewritten_query LONGTEXT NULL,
intent_result_json LONGTEXT NULL,
retrieval_plan_json LONGTEXT NULL,
query_plan_json LONGTEXT NULL,
filters_json LONGTEXT NULL,
ranked_json LONGTEXT NULL,
sources_json LONGTEXT NULL,
adopted_chunk_ids_json LONGTEXT NULL,
citation_map_json LONGTEXT NULL,
answer_text LONGTEXT NULL,
model_name VARCHAR(100) NULL,
total_tokens INT NOT NULL DEFAULT 0,
duration_ms INT NOT NULL DEFAULT 0,
status VARCHAR(50) NOT NULL DEFAULT 'Success',
error_message LONGTEXT NULL,
created_at DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
INDEX idx_rag_query_logs_session (session_id),
INDEX idx_rag_query_logs_status (status),
INDEX idx_rag_query_logs_session_status_created (session_id, status, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
COMMENT='RAG 查询日志表,记录用户查询、改写、检索过程、引用和回答结果';