38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
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: 500 }, 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);
|
|
});
|
|
});
|