Blame view

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