fix: 修复Server搜索问题
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m7s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m7s
- 将meilisearch搜索服务移至Server端
This commit is contained in:
37
server/api/search.get.ts
Normal file
37
server/api/search.get.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user