-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug_database.php
More file actions
155 lines (129 loc) · 4.91 KB
/
Copy pathdebug_database.php
File metadata and controls
155 lines (129 loc) · 4.91 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Debug script to check database content
*/
// Define WordPress path
$wp_load_path = __DIR__ . '/../../../../../wp-load.php';
if (file_exists($wp_load_path)) {
require_once($wp_load_path);
} else {
echo "WordPress not found at expected path\n";
exit;
}
global $wpdb;
// Check if table exists
$table_name = $wpdb->prefix . 'ai_chatbot_chunks';
$table_exists = $wpdb->get_var("SHOW TABLES LIKE '$table_name'");
echo "=== Database Debug ===\n";
echo "Table: $table_name\n";
echo "Exists: " . ($table_exists ? "Yes" : "No") . "\n\n";
if ($table_exists) {
// Get total chunks
$total_chunks = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");
echo "Total chunks: $total_chunks\n\n";
// Search for FAQ/support related content
$faq_chunks = $wpdb->get_results("
SELECT id, post_id, post_type, content_clean, metadata
FROM $table_name
WHERE content_clean LIKE '%support%'
OR content_clean LIKE '%FAQ%'
OR content_clean LIKE '%courseware%'
OR content_clean LIKE '%REAL CHEM%'
OR content_clean LIKE '%price%'
OR content_clean LIKE '%cost%'
OR content_clean LIKE '%student%'
ORDER BY id DESC
LIMIT 10
");
echo "FAQ/Support related chunks found: " . count($faq_chunks) . "\n\n";
foreach ($faq_chunks as $chunk) {
echo "--- Chunk ID: {$chunk->id} ---\n";
echo "Post ID: {$chunk->post_id}\n";
echo "Post Type: {$chunk->post_type}\n";
echo "Content: " . substr($chunk->content_clean, 0, 200) . "...\n";
echo "Metadata: " . $chunk->metadata . "\n\n";
}
// Search for Q: format content
$qa_chunks = $wpdb->get_results("
SELECT id, post_id, post_type, content_clean, metadata
FROM $table_name
WHERE content_clean LIKE '%Q: %'
ORDER BY id DESC
LIMIT 5
");
echo "Q&A format chunks found: " . count($qa_chunks) . "\n\n";
foreach ($qa_chunks as $chunk) {
echo "--- Q&A Chunk ID: {$chunk->id} ---\n";
echo "Post ID: {$chunk->post_id}\n";
echo "Post Type: {$chunk->post_type}\n";
echo "Content: " . substr($chunk->content_clean, 0, 300) . "...\n\n";
}
// Test search query
$test_query = "have Courseware price";
echo "=== Testing Search Query ===\n";
echo "Query: '$test_query'\n\n";
// Simulate the search logic
$cleanQuery = preg_replace('/[^\p{L}\p{N}\s]/u', ' ', $test_query);
$cleanQuery = preg_replace('/\s+/', ' ', $cleanQuery);
$terms = explode(' ', $cleanQuery);
echo "Cleaned query: '$cleanQuery'\n";
echo "Terms: " . implode(', ', $terms) . "\n\n";
// Build search conditions
$whereConditions = [];
$prepareArgs = [];
// Exact phrase match
if (strlen($test_query) > 3) {
$whereConditions[] = "content_clean LIKE %s";
$prepareArgs[] = '%' . $wpdb->esc_like($test_query) . '%';
}
// Individual terms
foreach ($terms as $term) {
if (strlen($term) > 2) {
$whereConditions[] = "content_clean LIKE %s";
$prepareArgs[] = '%' . $wpdb->esc_like($term) . '%';
}
}
// Question-based matching
if (preg_match('/\b(what|how|when|where|why|which|who|is|are|do|does|can|will|should)\b/i', $test_query)) {
$whereConditions[] = "content_clean LIKE %s";
$prepareArgs[] = '%Q: ' . $wpdb->esc_like($test_query) . '%';
}
echo "Search conditions:\n";
foreach ($whereConditions as $i => $condition) {
echo " $i: $condition\n";
if (isset($prepareArgs[$i])) {
echo " Arg: '{$prepareArgs[$i]}'\n";
}
}
echo "\n";
// Execute search
if (!empty($whereConditions)) {
$whereClause = implode(' OR ', $whereConditions);
$prepareArgs[] = 5;
$sql = "
SELECT *,
CHAR_LENGTH(content_clean) as length,
CASE
WHEN content_clean LIKE '%Q: %' THEN 3
WHEN content_clean LIKE '%What is%' THEN 2
WHEN content_clean LIKE '%How is%' THEN 2
WHEN content_clean LIKE '%Why is%' THEN 2
ELSE 1
END as priority_score
FROM $table_name
WHERE $whereClause
ORDER BY priority_score DESC, word_count DESC
LIMIT %d
";
$results = $wpdb->get_results($wpdb->prepare($sql, ...$prepareArgs));
echo "Search results: " . count($results) . "\n\n";
foreach ($results as $result) {
echo "--- Result ID: {$result->id} ---\n";
echo "Priority Score: {$result->priority_score}\n";
echo "Content: " . substr($result->content_clean, 0, 200) . "...\n\n";
}
}
} else {
echo "Table does not exist. Content may not be indexed yet.\n";
}
?>