rebuild_rag_schema.sql
5.08 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
-- 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,
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)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
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,
source_path VARCHAR(1024) NOT NULL,
file_type VARCHAR(50) NOT NULL,
page_number INT NULL,
page_start INT NULL,
page_end INT NULL,
slide_number INT NULL,
sheet_name VARCHAR(255) NULL,
row_start INT NULL,
row_end INT NULL,
char_start INT NULL,
char_end INT NULL,
prev_chunk_id VARCHAR(64) NULL,
next_chunk_id VARCHAR(64) NULL,
status VARCHAR(50) NOT NULL DEFAULT 'active',
intent_type VARCHAR(50) NULL,
page_type VARCHAR(50) NULL,
brand VARCHAR(100) NULL,
brand_series VARCHAR(100) NULL,
business_category VARCHAR(100) NULL,
sub_business VARCHAR(100) NULL,
content_kind VARCHAR(50) NULL,
section_title VARCHAR(255) NULL,
section_path_json LONGTEXT NULL,
parse_status VARCHAR(50) NULL,
parse_quality VARCHAR(50) NULL,
table_header_json LONGTEXT NULL,
qdrant_point_id VARCHAR(64) NULL,
parser_version VARCHAR(50) NULL,
chunker_version VARCHAR(50) 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_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, sub_business),
INDEX idx_rag_chunks_brand (brand, brand_series),
INDEX idx_rag_chunks_source_loc (page_start, page_end, sheet_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
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,
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;
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)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;