app.js
7.5 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// SearchEngine Frontend JavaScript
// API endpoint
const API_BASE_URL = 'http://localhost:8000';
// Update API URL display
document.getElementById('apiUrl').textContent = API_BASE_URL;
// Handle Enter key in search input
function handleKeyPress(event) {
if (event.key === 'Enter') {
performSearch();
}
}
// Set query from example buttons
function setQuery(query) {
document.getElementById('searchInput').value = query;
performSearch();
}
// Perform search
async function performSearch() {
const query = document.getElementById('searchInput').value.trim();
if (!query) {
alert('请输入搜索关键词');
return;
}
// Get options
const size = parseInt(document.getElementById('resultSize').value);
const enableTranslation = document.getElementById('enableTranslation').checked;
const enableEmbedding = document.getElementById('enableEmbedding').checked;
const enableRerank = document.getElementById('enableRerank').checked;
// Show loading
document.getElementById('loading').style.display = 'block';
document.getElementById('results').innerHTML = '';
document.getElementById('queryInfo').innerHTML = '';
try {
const response = await fetch(`${API_BASE_URL}/search/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: query,
size: size,
enable_translation: enableTranslation,
enable_embedding: enableEmbedding,
enable_rerank: enableRerank
})
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
displayResults(data);
displayQueryInfo(data.query_info);
} catch (error) {
console.error('Search error:', error);
document.getElementById('results').innerHTML = `
<div class="error-message">
<strong>搜索出错:</strong> ${error.message}
<br><br>
<small>请确保后端服务正在运行 (http://localhost:8000)</small>
</div>
`;
} finally {
document.getElementById('loading').style.display = 'none';
}
}
// Display search results
function displayResults(data) {
const resultsDiv = document.getElementById('results');
if (!data.hits || data.hits.length === 0) {
resultsDiv.innerHTML = `
<div class="no-results">
<h3>😔 没有找到结果</h3>
<p>请尝试其他关键词</p>
</div>
`;
return;
}
let html = `
<div class="results-header">
<h2>搜索结果</h2>
<div class="results-stats">
找到 <strong>${data.total}</strong> 个结果,
耗时 <strong>${data.took_ms}</strong> 毫秒,
最高分 <strong>${data.max_score.toFixed(4)}</strong>
</div>
</div>
`;
data.hits.forEach((hit, index) => {
const source = hit._source;
const score = hit._custom_score || hit._score;
html += `
<div class="result-item">
<div class="result-header">
<div>
<div class="result-title">${index + 1}. ${escapeHtml(source.name || 'N/A')}</div>
${source.enSpuName ? `<div style="color: #666; font-size: 14px;">${escapeHtml(source.enSpuName)}</div>` : ''}
${source.ruSkuName ? `<div style="color: #999; font-size: 13px;">${escapeHtml(source.ruSkuName)}</div>` : ''}
</div>
<div class="result-score">
${score.toFixed(4)}
</div>
</div>
<div class="result-meta">
${source.categoryName ? `<span>📁 ${escapeHtml(source.categoryName)}</span>` : ''}
${source.brandName ? `<span>🏷️ ${escapeHtml(source.brandName)}</span>` : ''}
${source.supplierName ? `<span>🏭 ${escapeHtml(source.supplierName)}</span>` : ''}
${source.create_time ? `<span>📅 ${formatDate(source.create_time)}</span>` : ''}
</div>
${source.imageUrl ? `
<img src="${escapeHtml(source.imageUrl)}"
alt="${escapeHtml(source.name)}"
class="result-image"
onerror="this.style.display='none'">
` : ''}
<div style="margin-top: 10px; font-size: 12px; color: #999;">
ID: ${source.skuId || 'N/A'}
</div>
</div>
`;
});
resultsDiv.innerHTML = html;
}
// Display query processing information
function displayQueryInfo(queryInfo) {
if (!queryInfo) return;
const queryInfoDiv = document.getElementById('queryInfo');
let html = `
<h3>查询处理信息</h3>
<div class="info-grid">
<div class="info-item">
<strong>原始查询</strong>
${escapeHtml(queryInfo.original_query || 'N/A')}
</div>
<div class="info-item">
<strong>重写后查询</strong>
${escapeHtml(queryInfo.rewritten_query || 'N/A')}
</div>
<div class="info-item">
<strong>检测语言</strong>
${getLanguageName(queryInfo.detected_language)}
</div>
<div class="info-item">
<strong>查询域</strong>
${escapeHtml(queryInfo.domain || 'default')}
</div>
</div>
`;
// Show translations if any
if (queryInfo.translations && Object.keys(queryInfo.translations).length > 0) {
html += '<h4 style="margin-top: 20px; margin-bottom: 10px;">翻译结果</h4><div class="info-grid">';
for (const [lang, translation] of Object.entries(queryInfo.translations)) {
if (translation) {
html += `
<div class="info-item">
<strong>${getLanguageName(lang)}</strong>
${escapeHtml(translation)}
</div>
`;
}
}
html += '</div>';
}
// Show embedding info
if (queryInfo.has_vector) {
html += `
<div style="margin-top: 15px; padding: 10px; background: #e8f5e9; border-radius: 5px;">
✓ 使用了语义向量搜索
</div>
`;
}
queryInfoDiv.innerHTML = html;
}
// Helper functions
function escapeHtml(text) {
if (!text) return '';
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function formatDate(dateStr) {
try {
const date = new Date(dateStr);
return date.toLocaleDateString('zh-CN');
} catch {
return dateStr;
}
}
function getLanguageName(code) {
const names = {
'zh': '中文',
'en': 'English',
'ru': 'Русский',
'ar': 'العربية',
'ja': '日本語',
'unknown': '未知'
};
return names[code] || code;
}
// Initialize page
document.addEventListener('DOMContentLoaded', function() {
console.log('SearchEngine Frontend loaded');
console.log('API Base URL:', API_BASE_URL);
// Focus on search input
document.getElementById('searchInput').focus();
});