Blame view

frontend/static/js/app.js 18.9 KB
4d824a77   tangwang   所有租户共用一套统一配置.tena...
1
  // SearchEngine Frontend - Modern UI (Multi-Tenant)
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
2
  
4d824a77   tangwang   所有租户共用一套统一配置.tena...
3
  const API_BASE_URL = 'http://localhost:6002';
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
4
5
  document.getElementById('apiUrl').textContent = API_BASE_URL;
  
4d824a77   tangwang   所有租户共用一套统一配置.tena...
6
7
8
9
10
11
12
13
14
  // Get tenant ID from input
  function getTenantId() {
      const tenantInput = document.getElementById('tenantInput');
      if (tenantInput) {
          return tenantInput.value.trim();
      }
      return '1'; // Default fallback
  }
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
15
16
17
18
19
20
21
  // State Management
  let state = {
      query: '',
      currentPage: 1,
      pageSize: 20,
      totalResults: 0,
      filters: {},
6aa246be   tangwang   问题:Pydantic 应该能自动...
22
      rangeFilters: {},
a7a8c6cb   tangwang   测试过滤、聚合、排序
23
24
      sortBy: '',
      sortOrder: 'desc',
6aa246be   tangwang   问题:Pydantic 应该能自动...
25
      facets: null,
1f071951   tangwang   补充调试信息,记录包括各个阶段的 ...
26
27
      lastSearchData: null,
      debug: true  // Always enable debug mode for test frontend
a7a8c6cb   tangwang   测试过滤、聚合、排序
28
29
30
31
32
  };
  
  // Initialize
  document.addEventListener('DOMContentLoaded', function() {
      console.log('SearchEngine loaded');
1f071951   tangwang   补充调试信息,记录包括各个阶段的 ...
33
34
      console.log('Debug mode: always enabled (test frontend)');
      
a7a8c6cb   tangwang   测试过滤、聚合、排序
35
      document.getElementById('searchInput').focus();
a7a8c6cb   tangwang   测试过滤、聚合、排序
36
37
38
  });
  
  // Keyboard handler
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
39
40
41
42
43
44
  function handleKeyPress(event) {
      if (event.key === 'Enter') {
          performSearch();
      }
  }
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
45
46
47
48
  // Toggle filters visibility
  function toggleFilters() {
      const filterSection = document.getElementById('filterSection');
      filterSection.classList.toggle('hidden');
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
49
50
  }
  
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
51
  // Perform search
a7a8c6cb   tangwang   测试过滤、聚合、排序
52
  async function performSearch(page = 1) {
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
53
      const query = document.getElementById('searchInput').value.trim();
4d824a77   tangwang   所有租户共用一套统一配置.tena...
54
      const tenantId = getTenantId();
a7a8c6cb   tangwang   测试过滤、聚合、排序
55
      
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
56
      if (!query) {
a7a8c6cb   tangwang   测试过滤、聚合、排序
57
          alert('Please enter search keywords');
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
58
59
          return;
      }
a7a8c6cb   tangwang   测试过滤、聚合、排序
60
      
4d824a77   tangwang   所有租户共用一套统一配置.tena...
61
62
63
64
65
      if (!tenantId) {
          alert('Please enter tenant ID');
          return;
      }
      
a7a8c6cb   tangwang   测试过滤、聚合、排序
66
67
68
69
70
71
      state.query = query;
      state.currentPage = page;
      state.pageSize = parseInt(document.getElementById('resultSize').value);
      
      const from = (page - 1) * state.pageSize;
      
6aa246be   tangwang   问题:Pydantic 应该能自动...
72
73
74
      // Define facets (简化配置)
      const facets = [
          {
4d824a77   tangwang   所有租户共用一套统一配置.tena...
75
              "field": "category_keyword",
6aa246be   tangwang   问题:Pydantic 应该能自动...
76
77
              "size": 15,
              "type": "terms"
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
78
          },
6aa246be   tangwang   问题:Pydantic 应该能自动...
79
          {
4d824a77   tangwang   所有租户共用一套统一配置.tena...
80
              "field": "vendor_keyword",
6aa246be   tangwang   问题:Pydantic 应该能自动...
81
82
              "size": 15,
              "type": "terms"
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
83
          },
6aa246be   tangwang   问题:Pydantic 应该能自动...
84
          {
4d824a77   tangwang   所有租户共用一套统一配置.tena...
85
              "field": "tags_keyword",
6aa246be   tangwang   问题:Pydantic 应该能自动...
86
87
              "size": 10,
              "type": "terms"
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
88
          },
6aa246be   tangwang   问题:Pydantic 应该能自动...
89
          {
4d824a77   tangwang   所有租户共用一套统一配置.tena...
90
              "field": "min_price",
6aa246be   tangwang   问题:Pydantic 应该能自动...
91
92
93
94
95
96
97
              "type": "range",
              "ranges": [
                  {"key": "0-50", "to": 50},
                  {"key": "50-100", "from": 50, "to": 100},
                  {"key": "100-200", "from": 100, "to": 200},
                  {"key": "200+", "from": 200}
              ]
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
98
          }
6aa246be   tangwang   问题:Pydantic 应该能自动...
99
      ];
a7a8c6cb   tangwang   测试过滤、聚合、排序
100
      
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
101
102
      // Show loading
      document.getElementById('loading').style.display = 'block';
a7a8c6cb   tangwang   测试过滤、聚合、排序
103
104
      document.getElementById('productGrid').innerHTML = '';
      
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
105
106
107
108
109
      try {
          const response = await fetch(`${API_BASE_URL}/search/`, {
              method: 'POST',
              headers: {
                  'Content-Type': 'application/json',
4d824a77   tangwang   所有租户共用一套统一配置.tena...
110
                  'X-Tenant-ID': tenantId,
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
111
112
113
              },
              body: JSON.stringify({
                  query: query,
a7a8c6cb   tangwang   测试过滤、聚合、排序
114
115
116
                  size: state.pageSize,
                  from: from,
                  filters: Object.keys(state.filters).length > 0 ? state.filters : null,
6aa246be   tangwang   问题:Pydantic 应该能自动...
117
118
                  range_filters: Object.keys(state.rangeFilters).length > 0 ? state.rangeFilters : null,
                  facets: facets,
a7a8c6cb   tangwang   测试过滤、聚合、排序
119
                  sort_by: state.sortBy || null,
1f071951   tangwang   补充调试信息,记录包括各个阶段的 ...
120
121
                  sort_order: state.sortOrder,
                  debug: state.debug
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
122
123
              })
          });
a7a8c6cb   tangwang   测试过滤、聚合、排序
124
          
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
125
126
127
          if (!response.ok) {
              throw new Error(`HTTP ${response.status}: ${response.statusText}`);
          }
a7a8c6cb   tangwang   测试过滤、聚合、排序
128
          
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
129
          const data = await response.json();
a7a8c6cb   tangwang   测试过滤、聚合、排序
130
131
          state.lastSearchData = data;
          state.totalResults = data.total;
6aa246be   tangwang   问题:Pydantic 应该能自动...
132
          state.facets = data.facets;
a7a8c6cb   tangwang   测试过滤、聚合、排序
133
          
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
134
          displayResults(data);
6aa246be   tangwang   问题:Pydantic 应该能自动...
135
          displayFacets(data.facets);
a7a8c6cb   tangwang   测试过滤、聚合、排序
136
          displayPagination();
1f071951   tangwang   补充调试信息,记录包括各个阶段的 ...
137
          displayDebugInfo(data);
a7a8c6cb   tangwang   测试过滤、聚合、排序
138
139
140
          updateProductCount(data.total);
          updateClearFiltersButton();
          
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
141
142
      } catch (error) {
          console.error('Search error:', error);
a7a8c6cb   tangwang   测试过滤、聚合、排序
143
          document.getElementById('productGrid').innerHTML = `
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
144
              <div class="error-message">
a7a8c6cb   tangwang   测试过滤、聚合、排序
145
                  <strong>Search Error:</strong> ${error.message}
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
146
                  <br><br>
a7a8c6cb   tangwang   测试过滤、聚合、排序
147
                  <small>Please ensure backend service is running (${API_BASE_URL})</small>
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
148
149
150
151
152
153
154
              </div>
          `;
      } finally {
          document.getElementById('loading').style.display = 'none';
      }
  }
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
155
  // Display results in grid
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
156
  function displayResults(data) {
a7a8c6cb   tangwang   测试过滤、聚合、排序
157
158
      const grid = document.getElementById('productGrid');
      
4d824a77   tangwang   所有租户共用一套统一配置.tena...
159
      if (!data.results || data.results.length === 0) {
a7a8c6cb   tangwang   测试过滤、聚合、排序
160
161
162
163
          grid.innerHTML = `
              <div class="no-results" style="grid-column: 1 / -1;">
                  <h3>No Results Found</h3>
                  <p>Try different keywords or filters</p>
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
164
165
166
167
              </div>
          `;
          return;
      }
a7a8c6cb   tangwang   测试过滤、聚合、排序
168
169
170
      
      let html = '';
      
4d824a77   tangwang   所有租户共用一套统一配置.tena...
171
172
173
174
175
176
177
      data.results.forEach((result) => {
          const product = result;
          const title = product.title || product.name || 'N/A';
          const price = product.min_price || product.price || 'N/A';
          const imageUrl = product.image_url || product.imageUrl || '';
          const category = product.category || product.categoryName || '';
          const vendor = product.vendor || product.brandName || '';
a7a8c6cb   tangwang   测试过滤、聚合、排序
178
          
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
179
          html += `
a7a8c6cb   tangwang   测试过滤、聚合、排序
180
181
              <div class="product-card">
                  <div class="product-image-wrapper">
4d824a77   tangwang   所有租户共用一套统一配置.tena...
182
183
184
                      ${imageUrl ? `
                          <img src="${escapeHtml(imageUrl)}" 
                               alt="${escapeHtml(title)}" 
a7a8c6cb   tangwang   测试过滤、聚合、排序
185
186
187
188
189
                               class="product-image"
                               onerror="this.src='data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%22100%22 height=%22100%22%3E%3Crect fill=%22%23f0f0f0%22 width=%22100%22 height=%22100%22/%3E%3Ctext x=%2250%25%22 y=%2250%25%22 font-size=%2214%22 text-anchor=%22middle%22 dy=%22.3em%22 fill=%22%23999%22%3ENo Image%3C/text%3E%3C/svg%3E'">
                      ` : `
                          <div style="color: #ccc; font-size: 14px;">No Image</div>
                      `}
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
190
                  </div>
a7a8c6cb   tangwang   测试过滤、聚合、排序
191
192
                  
                  <div class="product-price">
4d824a77   tangwang   所有租户共用一套统一配置.tena...
193
                      ${price !== 'N/A' ? ${price}` : 'N/A'}
a7a8c6cb   tangwang   测试过滤、聚合、排序
194
195
196
                  </div>
                  
                  <div class="product-title">
4d824a77   tangwang   所有租户共用一套统一配置.tena...
197
                      ${escapeHtml(title)}
a7a8c6cb   tangwang   测试过滤、聚合、排序
198
199
200
                  </div>
                  
                  <div class="product-meta">
4d824a77   tangwang   所有租户共用一套统一配置.tena...
201
202
                      ${category ? escapeHtml(category) : ''}
                      ${vendor ? ' | ' + escapeHtml(vendor) : ''}
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
203
                  </div>
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
204
205
206
              </div>
          `;
      });
a7a8c6cb   tangwang   测试过滤、聚合、排序
207
208
      
      grid.innerHTML = html;
115047ee   tangwang   为一个租户灌入测试数据;实例的启动...
209
210
  }
  
6aa246be   tangwang   问题:Pydantic 应该能自动...
211
212
213
  // Display facets as filter tags (重构版 - 标准化格式)
  function displayFacets(facets) {
      if (!facets) return;
a7a8c6cb   tangwang   测试过滤、聚合、排序
214
      
6aa246be   tangwang   问题:Pydantic 应该能自动...
215
216
217
218
      facets.forEach(facet => {
          // 根据字段名找到对应的容器
          let containerId = null;
          let maxDisplay = 10;
a7a8c6cb   tangwang   测试过滤、聚合、排序
219
          
4d824a77   tangwang   所有租户共用一套统一配置.tena...
220
          if (facet.field === 'category_keyword') {
6aa246be   tangwang   问题:Pydantic 应该能自动...
221
222
              containerId = 'categoryTags';
              maxDisplay = 10;
4d824a77   tangwang   所有租户共用一套统一配置.tena...
223
          } else if (facet.field === 'vendor_keyword') {
6aa246be   tangwang   问题:Pydantic 应该能自动...
224
225
              containerId = 'brandTags';
              maxDisplay = 10;
4d824a77   tangwang   所有租户共用一套统一配置.tena...
226
          } else if (facet.field === 'tags_keyword') {
6aa246be   tangwang   问题:Pydantic 应该能自动...
227
228
229
              containerId = 'supplierTags';
              maxDisplay = 8;
          }
a7a8c6cb   tangwang   测试过滤、聚合、排序
230
          
6aa246be   tangwang   问题:Pydantic 应该能自动...
231
          if (!containerId) return;
a7a8c6cb   tangwang   测试过滤、聚合、排序
232
          
6aa246be   tangwang   问题:Pydantic 应该能自动...
233
234
          const container = document.getElementById(containerId);
          if (!container) return;
a7a8c6cb   tangwang   测试过滤、聚合、排序
235
          
a7a8c6cb   tangwang   测试过滤、聚合、排序
236
237
          let html = '';
          
6aa246be   tangwang   问题:Pydantic 应该能自动...
238
239
240
241
242
          // 渲染分面值
          facet.values.slice(0, maxDisplay).forEach(facetValue => {
              const value = facetValue.value;
              const count = facetValue.count;
              const selected = facetValue.selected;
a7a8c6cb   tangwang   测试过滤、聚合、排序
243
              
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
244
              html += `
6aa246be   tangwang   问题:Pydantic 应该能自动...
245
246
247
                  <span class="filter-tag ${selected ? 'active' : ''}" 
                        onclick="toggleFilter('${escapeAttr(facet.field)}', '${escapeAttr(value)}')">
                      ${escapeHtml(value)} (${count})
a7a8c6cb   tangwang   测试过滤、聚合、排序
248
                  </span>
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
249
250
              `;
          });
a7a8c6cb   tangwang   测试过滤、聚合、排序
251
          
6aa246be   tangwang   问题:Pydantic 应该能自动...
252
253
          container.innerHTML = html;
      });
a7a8c6cb   tangwang   测试过滤、聚合、排序
254
  }
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
255
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  // 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);
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
270
      }
a7a8c6cb   tangwang   测试过滤、聚合、排序
271
272
273
      
      performSearch(1); // Reset to page 1
  }
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
274
  
6aa246be   tangwang   问题:Pydantic 应该能自动...
275
  // Handle price filter (重构版 - 使用 rangeFilters)
edd38328   tangwang   测试过滤、聚合、排序
276
277
  function handlePriceFilter(value) {
      if (!value) {
4d824a77   tangwang   所有租户共用一套统一配置.tena...
278
          delete state.rangeFilters.min_price;
edd38328   tangwang   测试过滤、聚合、排序
279
280
      } else {
          const priceRanges = {
6aa246be   tangwang   问题:Pydantic 应该能自动...
281
282
283
284
              '0-50': { lt: 50 },
              '50-100': { gte: 50, lt: 100 },
              '100-200': { gte: 100, lt: 200 },
              '200+': { gte: 200 }
edd38328   tangwang   测试过滤、聚合、排序
285
286
287
          };
          
          if (priceRanges[value]) {
4d824a77   tangwang   所有租户共用一套统一配置.tena...
288
              state.rangeFilters.min_price = priceRanges[value];
edd38328   tangwang   测试过滤、聚合、排序
289
290
          }
      }
a7a8c6cb   tangwang   测试过滤、聚合、排序
291
      
edd38328   tangwang   测试过滤、聚合、排序
292
293
294
      performSearch(1);
  }
  
6aa246be   tangwang   问题:Pydantic 应该能自动...
295
  // Handle time filter (重构版 - 使用 rangeFilters)
edd38328   tangwang   测试过滤、聚合、排序
296
297
  function handleTimeFilter(value) {
      if (!value) {
6aa246be   tangwang   问题:Pydantic 应该能自动...
298
          delete state.rangeFilters.create_time;
edd38328   tangwang   测试过滤、聚合、排序
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
      } 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) {
6aa246be   tangwang   问题:Pydantic 应该能自动...
322
323
              state.rangeFilters.create_time = {
                  gte: fromDate.toISOString()
edd38328   tangwang   测试过滤、聚合、排序
324
325
              };
          }
a7a8c6cb   tangwang   测试过滤、聚合、排序
326
327
328
      }
      
      performSearch(1);
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
329
330
  }
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
331
332
333
  // Clear all filters
  function clearAllFilters() {
      state.filters = {};
6aa246be   tangwang   问题:Pydantic 应该能自动...
334
      state.rangeFilters = {};
a7a8c6cb   tangwang   测试过滤、聚合、排序
335
      document.getElementById('priceFilter').value = '';
edd38328   tangwang   测试过滤、聚合、排序
336
      document.getElementById('timeFilter').value = '';
a7a8c6cb   tangwang   测试过滤、聚合、排序
337
338
      performSearch(1);
  }
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
339
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
340
341
342
  // Update clear filters button visibility
  function updateClearFiltersButton() {
      const btn = document.getElementById('clearFiltersBtn');
6aa246be   tangwang   问题:Pydantic 应该能自动...
343
      if (Object.keys(state.filters).length > 0 || Object.keys(state.rangeFilters).length > 0) {
a7a8c6cb   tangwang   测试过滤、聚合、排序
344
345
346
          btn.style.display = 'inline-block';
      } else {
          btn.style.display = 'none';
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
347
      }
a7a8c6cb   tangwang   测试过滤、聚合、排序
348
  }
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
349
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
350
351
352
353
  // Update product count
  function updateProductCount(total) {
      document.getElementById('productCount').textContent = `${total.toLocaleString()} products found`;
  }
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
354
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
355
  // Sort functions
edd38328   tangwang   测试过滤、聚合、排序
356
357
  function setSortByDefault() {
      // Remove active from all buttons and arrows
a7a8c6cb   tangwang   测试过滤、聚合、排序
358
      document.querySelectorAll('.sort-btn').forEach(b => b.classList.remove('active'));
edd38328   tangwang   测试过滤、聚合、排序
359
      document.querySelectorAll('.arrow-up, .arrow-down').forEach(a => a.classList.remove('active'));
a7a8c6cb   tangwang   测试过滤、聚合、排序
360
      
edd38328   tangwang   测试过滤、聚合、排序
361
362
363
      // Set default button active
      const defaultBtn = document.querySelector('.sort-btn[data-sort=""]');
      if (defaultBtn) defaultBtn.classList.add('active');
a7a8c6cb   tangwang   测试过滤、聚合、排序
364
      
edd38328   tangwang   测试过滤、聚合、排序
365
366
      state.sortBy = '';
      state.sortOrder = 'desc';
a7a8c6cb   tangwang   测试过滤、聚合、排序
367
368
369
      
      performSearch(1);
  }
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
370
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
371
372
373
374
  function sortByField(field, order) {
      state.sortBy = field;
      state.sortOrder = order;
      
edd38328   tangwang   测试过滤、聚合、排序
375
      // Remove active from all buttons (but keep "By default" if no sort)
a7a8c6cb   tangwang   测试过滤、聚合、排序
376
      document.querySelectorAll('.sort-btn').forEach(b => b.classList.remove('active'));
edd38328   tangwang   测试过滤、聚合、排序
377
378
379
380
381
382
383
384
385
      
      // 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');
      }
a7a8c6cb   tangwang   测试过滤、聚合、排序
386
387
      
      performSearch(state.currentPage);
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
388
389
  }
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
  // 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 = `
          <button class="page-btn" onclick="goToPage(${currentPage - 1})" 
                  ${currentPage === 1 ? 'disabled' : ''}>
               Previous
          </button>
      `;
      
      // 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 += `<button class="page-btn" onclick="goToPage(1)">1</button>`;
          if (startPage > 2) {
              html += `<span class="page-info">...</span>`;
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
424
          }
a7a8c6cb   tangwang   测试过滤、聚合、排序
425
426
427
428
429
430
431
432
433
434
435
436
437
438
      }
      
      for (let i = startPage; i <= endPage; i++) {
          html += `
              <button class="page-btn ${i === currentPage ? 'active' : ''}" 
                      onclick="goToPage(${i})">
                  ${i}
              </button>
          `;
      }
      
      if (endPage < totalPages) {
          if (endPage < totalPages - 1) {
              html += `<span class="page-info">...</span>`;
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
439
          }
a7a8c6cb   tangwang   测试过滤、聚合、排序
440
          html += `<button class="page-btn" onclick="goToPage(${totalPages})">${totalPages}</button>`;
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
441
      }
a7a8c6cb   tangwang   测试过滤、聚合、排序
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
      
      html += `
          <button class="page-btn" onclick="goToPage(${currentPage + 1})" 
                  ${currentPage === totalPages ? 'disabled' : ''}>
              Next 
          </button>
      `;
      
      html += `
          <span class="page-info">
              Page ${currentPage} of ${totalPages} (${state.totalResults.toLocaleString()} results)
          </span>
      `;
      
      paginationDiv.innerHTML = html;
  }
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
458
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
459
460
461
462
463
464
465
466
  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' });
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
467
468
  }
  
1f071951   tangwang   补充调试信息,记录包括各个阶段的 ...
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
  // 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 = '<div style="padding: 10px;">';
              html += `<div><strong>查询:</strong> ${escapeHtml(data.query_info.original_query || 'N/A')}</div>`;
              html += `<div><strong>语言:</strong> ${getLanguageName(data.query_info.detected_language)}</div>`;
              html += '</div>';
              debugInfoDiv.innerHTML = html;
          } else {
              debugInfoDiv.innerHTML = '';
          }
          return;
      }
a7a8c6cb   tangwang   测试过滤、聚合、排序
486
      
1f071951   tangwang   补充调试信息,记录包括各个阶段的 ...
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
      // Display comprehensive debug info when debug mode is on
      const debugInfo = data.debug_info;
      let html = '<div style="padding: 10px; font-family: monospace; font-size: 12px;">';
      
      // Query Analysis
      if (debugInfo.query_analysis) {
          html += '<div style="margin-bottom: 15px;"><strong style="font-size: 14px;">查询分析:</strong>';
          html += `<div>原始查询: ${escapeHtml(debugInfo.query_analysis.original_query || 'N/A')}</div>`;
          html += `<div>标准化: ${escapeHtml(debugInfo.query_analysis.normalized_query || 'N/A')}</div>`;
          html += `<div>重写后: ${escapeHtml(debugInfo.query_analysis.rewritten_query || 'N/A')}</div>`;
          html += `<div>检测语言: ${getLanguageName(debugInfo.query_analysis.detected_language)}</div>`;
          html += `<div>域: ${escapeHtml(debugInfo.query_analysis.domain || 'default')}</div>`;
          html += `<div>简单查询: ${debugInfo.query_analysis.is_simple_query ? '是' : '否'}</div>`;
          
          if (debugInfo.query_analysis.translations && Object.keys(debugInfo.query_analysis.translations).length > 0) {
              html += '<div>翻译: ';
              for (const [lang, translation] of Object.entries(debugInfo.query_analysis.translations)) {
                  if (translation) {
                      html += `${getLanguageName(lang)}: ${escapeHtml(translation)}; `;
                  }
              }
              html += '</div>';
          }
          
          if (debugInfo.query_analysis.boolean_ast) {
              html += `<div>布尔AST: ${escapeHtml(debugInfo.query_analysis.boolean_ast)}</div>`;
          }
          
          html += `<div>向量嵌入: ${debugInfo.query_analysis.has_vector ? '已启用' : '未启用'}</div>`;
          html += '</div>';
      }
a7a8c6cb   tangwang   测试过滤、聚合、排序
518
      
1f071951   tangwang   补充调试信息,记录包括各个阶段的 ...
519
520
521
522
523
524
525
526
      // Feature Flags
      if (debugInfo.feature_flags) {
          html += '<div style="margin-bottom: 15px;"><strong style="font-size: 14px;">功能开关:</strong>';
          html += `<div>翻译: ${debugInfo.feature_flags.translation_enabled ? '启用' : '禁用'}</div>`;
          html += `<div>嵌入: ${debugInfo.feature_flags.embedding_enabled ? '启用' : '禁用'}</div>`;
          html += `<div>重排序: ${debugInfo.feature_flags.rerank_enabled ? '启用' : '禁用'}</div>`;
          html += '</div>';
      }
a7a8c6cb   tangwang   测试过滤、聚合、排序
527
      
1f071951   tangwang   补充调试信息,记录包括各个阶段的 ...
528
529
530
531
532
533
534
535
      // ES Response
      if (debugInfo.es_response) {
          html += '<div style="margin-bottom: 15px;"><strong style="font-size: 14px;">ES响应:</strong>';
          html += `<div>耗时: ${debugInfo.es_response.took_ms}ms</div>`;
          html += `<div>总命中数: ${debugInfo.es_response.total_hits}</div>`;
          html += `<div>最高分: ${debugInfo.es_response.max_score?.toFixed(3) || 0}</div>`;
          html += '</div>';
      }
a7a8c6cb   tangwang   测试过滤、聚合、排序
536
      
1f071951   tangwang   补充调试信息,记录包括各个阶段的 ...
537
538
539
540
541
      // Stage Timings
      if (debugInfo.stage_timings) {
          html += '<div style="margin-bottom: 15px;"><strong style="font-size: 14px;">各阶段耗时:</strong>';
          for (const [stage, duration] of Object.entries(debugInfo.stage_timings)) {
              html += `<div>${stage}: ${duration.toFixed(2)}ms</div>`;
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
542
          }
1f071951   tangwang   补充调试信息,记录包括各个阶段的 ...
543
          html += '</div>';
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
544
      }
a7a8c6cb   tangwang   测试过滤、聚合、排序
545
      
1f071951   tangwang   补充调试信息,记录包括各个阶段的 ...
546
547
548
549
550
      // ES Query
      if (debugInfo.es_query) {
          html += '<div style="margin-bottom: 15px;"><strong style="font-size: 14px;">ES查询DSL:</strong>';
          html += `<pre style="background: #f5f5f5; padding: 10px; overflow: auto; max-height: 400px;">${escapeHtml(JSON.stringify(debugInfo.es_query, null, 2))}</pre>`;
          html += '</div>';
a7a8c6cb   tangwang   测试过滤、聚合、排序
551
552
553
      }
      
      html += '</div>';
1f071951   tangwang   补充调试信息,记录包括各个阶段的 ...
554
      debugInfoDiv.innerHTML = html;
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
555
556
  }
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
557
558
559
560
561
562
  // Helper functions
  function escapeHtml(text) {
      if (!text) return '';
      const div = document.createElement('div');
      div.textContent = text;
      return div.innerHTML;
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
563
564
  }
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
565
566
567
  function escapeAttr(text) {
      if (!text) return '';
      return text.replace(/'/g, "\\'").replace(/"/g, '&quot;');
c86c8237   tangwang   支持聚合。过滤项补充了逻辑,但是有问题
568
569
  }
  
edd38328   tangwang   测试过滤、聚合、排序
570
571
572
573
574
575
576
577
578
579
  function formatDate(dateStr) {
      if (!dateStr) return '';
      try {
          const date = new Date(dateStr);
          return date.toLocaleDateString('zh-CN');
      } catch {
          return dateStr;
      }
  }
  
a7a8c6cb   tangwang   测试过滤、聚合、排序
580
581
582
583
584
585
586
587
588
589
590
  function getLanguageName(code) {
      const names = {
          'zh': '中文',
          'en': 'English',
          'ru': 'Русский',
          'ar': 'العربية',
          'ja': '日本語',
          'unknown': 'Unknown'
      };
      return names[code] || code;
  }