// SearchEngine Frontend - Modern UI const API_BASE_URL = 'http://120.76.41.98:6002'; document.getElementById('apiUrl').textContent = API_BASE_URL; // State Management let state = { query: '', currentPage: 1, pageSize: 20, totalResults: 0, filters: {}, sortBy: '', sortOrder: 'desc', aggregations: null, lastSearchData: null, debug: true // Always enable debug mode for test frontend }; // Initialize document.addEventListener('DOMContentLoaded', function() { console.log('SearchEngine loaded'); console.log('Debug mode: always enabled (test frontend)'); document.getElementById('searchInput').focus(); }); // Keyboard handler function handleKeyPress(event) { if (event.key === 'Enter') { performSearch(); } } // Toggle filters visibility function toggleFilters() { const filterSection = document.getElementById('filterSection'); filterSection.classList.toggle('hidden'); } // Perform search async function performSearch(page = 1) { const query = document.getElementById('searchInput').value.trim(); if (!query) { alert('Please enter search keywords'); return; } state.query = query; state.currentPage = page; state.pageSize = parseInt(document.getElementById('resultSize').value); const from = (page - 1) * state.pageSize; // Define aggregations const aggregations = { "category_stats": { "terms": { "field": "categoryName_keyword", "size": 15 } }, "brand_stats": { "terms": { "field": "brandName_keyword", "size": 15 } }, "supplier_stats": { "terms": { "field": "supplierName_keyword", "size": 10 } }, "price_ranges": { "range": { "field": "price", "ranges": [ {"key": "0-50", "to": 50}, {"key": "50-100", "from": 50, "to": 100}, {"key": "100-200", "from": 100, "to": 200}, {"key": "200+", "from": 200} ] } } }; // Show loading document.getElementById('loading').style.display = 'block'; document.getElementById('productGrid').innerHTML = ''; try { const response = await fetch(`${API_BASE_URL}/search/`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ query: query, size: state.pageSize, from: from, filters: Object.keys(state.filters).length > 0 ? state.filters : null, aggregations: aggregations, sort_by: state.sortBy || null, sort_order: state.sortOrder, debug: state.debug }) }); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } const data = await response.json(); state.lastSearchData = data; state.totalResults = data.total; state.aggregations = data.aggregations; displayResults(data); displayAggregations(data.aggregations); displayPagination(); displayDebugInfo(data); updateProductCount(data.total); updateClearFiltersButton(); } catch (error) { console.error('Search error:', error); document.getElementById('productGrid').innerHTML = `
Search Error: ${error.message}

Please ensure backend service is running (${API_BASE_URL})
`; } finally { document.getElementById('loading').style.display = 'none'; } } // Display results in grid function displayResults(data) { const grid = document.getElementById('productGrid'); if (!data.hits || data.hits.length === 0) { grid.innerHTML = `

No Results Found

Try different keywords or filters

`; return; } let html = ''; data.hits.forEach((hit) => { const source = hit._source; const score = hit._custom_score || hit._score; html += `
${source.imageUrl ? ` ${escapeHtml(source.name)} ` : `
No Image
`}
${source.price ? `${source.price} ₽` : 'N/A'}
MOQ ${source.moq || 1} Box
${source.quantity || 'N/A'} pcs / Box
${escapeHtml(source.name || source.enSpuName || 'N/A')}
${source.categoryName ? escapeHtml(source.categoryName) : ''} ${source.brandName ? ' | ' + escapeHtml(source.brandName) : ''}
${source.create_time ? `
Listed: ${formatDate(source.create_time)}
` : ''}
`; }); grid.innerHTML = html; } // Display aggregations as filter tags function displayAggregations(aggregations) { if (!aggregations) return; // Category tags if (aggregations.category_stats && aggregations.category_stats.buckets) { const categoryTags = document.getElementById('categoryTags'); let html = ''; aggregations.category_stats.buckets.slice(0, 10).forEach(bucket => { const key = bucket.key; const count = bucket.doc_count; const isActive = state.filters.categoryName_keyword && state.filters.categoryName_keyword.includes(key); html += ` ${escapeHtml(key)} (${count}) `; }); categoryTags.innerHTML = html; } // Brand tags if (aggregations.brand_stats && aggregations.brand_stats.buckets) { const brandTags = document.getElementById('brandTags'); let html = ''; aggregations.brand_stats.buckets.slice(0, 10).forEach(bucket => { const key = bucket.key; const count = bucket.doc_count; const isActive = state.filters.brandName_keyword && state.filters.brandName_keyword.includes(key); html += ` ${escapeHtml(key)} (${count}) `; }); brandTags.innerHTML = html; } // Supplier tags if (aggregations.supplier_stats && aggregations.supplier_stats.buckets) { const supplierTags = document.getElementById('supplierTags'); let html = ''; aggregations.supplier_stats.buckets.slice(0, 8).forEach(bucket => { const key = bucket.key; const count = bucket.doc_count; const isActive = state.filters.supplierName_keyword && state.filters.supplierName_keyword.includes(key); html += ` ${escapeHtml(key)} (${count}) `; }); supplierTags.innerHTML = html; } } // Toggle filter function toggleFilter(field, value) { if (!state.filters[field]) { state.filters[field] = []; } const index = state.filters[field].indexOf(value); if (index > -1) { state.filters[field].splice(index, 1); if (state.filters[field].length === 0) { delete state.filters[field]; } } else { state.filters[field].push(value); } performSearch(1); // Reset to page 1 } // Handle price filter function handlePriceFilter(value) { if (!value) { delete state.filters.price; } else { const priceRanges = { '0-50': { to: 50 }, '50-100': { from: 50, to: 100 }, '100-200': { from: 100, to: 200 }, '200+': { from: 200 } }; if (priceRanges[value]) { state.filters.price = priceRanges[value]; } } performSearch(1); } // Handle time filter function handleTimeFilter(value) { if (!value) { delete state.filters.create_time; } else { const now = new Date(); let fromDate; switch(value) { case 'today': fromDate = new Date(now.setHours(0, 0, 0, 0)); break; case 'week': fromDate = new Date(now.setDate(now.getDate() - 7)); break; case 'month': fromDate = new Date(now.setMonth(now.getMonth() - 1)); break; case '3months': fromDate = new Date(now.setMonth(now.getMonth() - 3)); break; case '6months': fromDate = new Date(now.setMonth(now.getMonth() - 6)); break; } if (fromDate) { state.filters.create_time = { from: fromDate.toISOString() }; } } performSearch(1); } // Clear all filters function clearAllFilters() { state.filters = {}; document.getElementById('priceFilter').value = ''; document.getElementById('timeFilter').value = ''; performSearch(1); } // Update clear filters button visibility function updateClearFiltersButton() { const btn = document.getElementById('clearFiltersBtn'); if (Object.keys(state.filters).length > 0) { btn.style.display = 'inline-block'; } else { btn.style.display = 'none'; } } // Update product count function updateProductCount(total) { document.getElementById('productCount').textContent = `${total.toLocaleString()} products found`; } // Sort functions function setSortByDefault() { // Remove active from all buttons and arrows document.querySelectorAll('.sort-btn').forEach(b => b.classList.remove('active')); document.querySelectorAll('.arrow-up, .arrow-down').forEach(a => a.classList.remove('active')); // Set default button active const defaultBtn = document.querySelector('.sort-btn[data-sort=""]'); if (defaultBtn) defaultBtn.classList.add('active'); state.sortBy = ''; state.sortOrder = 'desc'; performSearch(1); } function sortByField(field, order) { state.sortBy = field; state.sortOrder = order; // Remove active from all buttons (but keep "By default" if no sort) document.querySelectorAll('.sort-btn').forEach(b => b.classList.remove('active')); // Remove active from all arrows document.querySelectorAll('.arrow-up, .arrow-down').forEach(a => a.classList.remove('active')); // Add active to clicked arrow const activeArrow = document.querySelector(`.arrow-up[data-field="${field}"][data-order="${order}"], .arrow-down[data-field="${field}"][data-order="${order}"]`); if (activeArrow) { activeArrow.classList.add('active'); } performSearch(state.currentPage); } // Pagination function displayPagination() { const paginationDiv = document.getElementById('pagination'); if (state.totalResults <= state.pageSize) { paginationDiv.style.display = 'none'; return; } paginationDiv.style.display = 'flex'; const totalPages = Math.ceil(state.totalResults / state.pageSize); const currentPage = state.currentPage; let html = ` `; // Page numbers const maxVisible = 5; let startPage = Math.max(1, currentPage - Math.floor(maxVisible / 2)); let endPage = Math.min(totalPages, startPage + maxVisible - 1); if (endPage - startPage < maxVisible - 1) { startPage = Math.max(1, endPage - maxVisible + 1); } if (startPage > 1) { html += ``; if (startPage > 2) { html += `...`; } } for (let i = startPage; i <= endPage; i++) { html += ` `; } if (endPage < totalPages) { if (endPage < totalPages - 1) { html += `...`; } html += ``; } html += ` `; html += ` Page ${currentPage} of ${totalPages} (${state.totalResults.toLocaleString()} results) `; paginationDiv.innerHTML = html; } function goToPage(page) { const totalPages = Math.ceil(state.totalResults / state.pageSize); if (page < 1 || page > totalPages) return; performSearch(page); // Scroll to top window.scrollTo({ top: 0, behavior: 'smooth' }); } // Display debug info function displayDebugInfo(data) { const debugInfoDiv = document.getElementById('debugInfo'); if (!state.debug || !data.debug_info) { // If debug mode is off or no debug info, show basic query info if (data.query_info) { let html = '
'; html += `
查询: ${escapeHtml(data.query_info.original_query || 'N/A')}
`; html += `
语言: ${getLanguageName(data.query_info.detected_language)}
`; html += '
'; debugInfoDiv.innerHTML = html; } else { debugInfoDiv.innerHTML = ''; } return; } // Display comprehensive debug info when debug mode is on const debugInfo = data.debug_info; let html = '
'; // Query Analysis if (debugInfo.query_analysis) { html += '
查询分析:'; html += `
原始查询: ${escapeHtml(debugInfo.query_analysis.original_query || 'N/A')}
`; html += `
标准化: ${escapeHtml(debugInfo.query_analysis.normalized_query || 'N/A')}
`; html += `
重写后: ${escapeHtml(debugInfo.query_analysis.rewritten_query || 'N/A')}
`; html += `
检测语言: ${getLanguageName(debugInfo.query_analysis.detected_language)}
`; html += `
域: ${escapeHtml(debugInfo.query_analysis.domain || 'default')}
`; html += `
简单查询: ${debugInfo.query_analysis.is_simple_query ? '是' : '否'}
`; if (debugInfo.query_analysis.translations && Object.keys(debugInfo.query_analysis.translations).length > 0) { html += '
翻译: '; for (const [lang, translation] of Object.entries(debugInfo.query_analysis.translations)) { if (translation) { html += `${getLanguageName(lang)}: ${escapeHtml(translation)}; `; } } html += '
'; } if (debugInfo.query_analysis.boolean_ast) { html += `
布尔AST: ${escapeHtml(debugInfo.query_analysis.boolean_ast)}
`; } html += `
向量嵌入: ${debugInfo.query_analysis.has_vector ? '已启用' : '未启用'}
`; html += '
'; } // Feature Flags if (debugInfo.feature_flags) { html += '
功能开关:'; html += `
翻译: ${debugInfo.feature_flags.translation_enabled ? '启用' : '禁用'}
`; html += `
嵌入: ${debugInfo.feature_flags.embedding_enabled ? '启用' : '禁用'}
`; html += `
重排序: ${debugInfo.feature_flags.rerank_enabled ? '启用' : '禁用'}
`; html += '
'; } // ES Response if (debugInfo.es_response) { html += '
ES响应:'; html += `
耗时: ${debugInfo.es_response.took_ms}ms
`; html += `
总命中数: ${debugInfo.es_response.total_hits}
`; html += `
最高分: ${debugInfo.es_response.max_score?.toFixed(3) || 0}
`; html += '
'; } // Stage Timings if (debugInfo.stage_timings) { html += '
各阶段耗时:'; for (const [stage, duration] of Object.entries(debugInfo.stage_timings)) { html += `
${stage}: ${duration.toFixed(2)}ms
`; } html += '
'; } // ES Query if (debugInfo.es_query) { html += '
ES查询DSL:'; html += `
${escapeHtml(JSON.stringify(debugInfo.es_query, null, 2))}
`; html += '
'; } html += '
'; debugInfoDiv.innerHTML = html; } // Helper functions function escapeHtml(text) { if (!text) return ''; const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } function escapeAttr(text) { if (!text) return ''; return text.replace(/'/g, "\\'").replace(/"/g, '"'); } function formatDate(dateStr) { if (!dateStr) return ''; 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': 'Unknown' }; return names[code] || code; }