fix: 修复Server搜索问题
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m7s

- 将meilisearch搜索服务移至Server端
This commit is contained in:
2025-11-14 14:57:55 +08:00
parent b2b631ed46
commit 17bb8adee3
6 changed files with 148 additions and 175 deletions

37
server/api/search.get.ts Normal file
View File

@ -0,0 +1,37 @@
import { toSearchItemView } from '../mappers/searchItemMapper';
import { getMeiliService } from '../services/search';
export default defineEventHandler(async (event) => {
const query = getQuery(event);
const q = String(query.query || '');
const locale = getHeader(event, 'x-locale') || 'zh-CN';
logger.info(`Search query: "${q}" for locale: "${locale}"`);
if (!q) return [];
const meili = getMeiliService();
const sections = await meili.search(q, { limit: 12 }, locale);
// 空Section过滤
const filteredSections = sections.filter(
(section) => section.hits.length > 0
);
const typeMap = {
products: 'products',
solutions: 'solutions',
questions: 'questions',
product_documents: 'product_documents',
} as const;
const hits = filteredSections.flatMap((section) => {
const type = typeMap[section.rawIndex as keyof typeof typeMap];
if (!type) return [];
return section.hits.map((hit) => ({ type, content: hit }));
});
return hits.map((hit) => {
return toSearchItemView(hit.content, hit.type);
});
});