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

View File

@ -21,58 +21,36 @@
// i18n相关
const { t } = useI18n();
const { getDirectusLocale } = useLocalizations();
const directusLocale = getDirectusLocale();
const locale = getDirectusLocale();
// 路由相关
const route = useRoute();
// 搜索相关
const { search } = useMeilisearch();
const keyword = ref('');
if (typeof route.query.query === 'string' && route.query.query.trim()) {
keyword.value = route.query.query;
}
const {
data: sections,
data: searchItems,
pending: loading,
error,
refresh,
} = await useAsyncData(
() => `search-${directusLocale}-${route.query.query ?? ''}`,
async () => {
const q = String(route.query.query ?? '').trim();
if (!q) return [];
return await search(q, { limit: 12 }, directusLocale);
} = useAsyncData(`meilisearch-${keyword.value}-${locale}`, async () => {
try {
const data = await $fetch(`/api/search?query=${keyword.value}`, {
headers: { 'x-locale': locale },
});
return data;
} catch (error) {
logger.error('Error fetching search results: ', error);
throw error;
}
);
});
// 空Section过滤
const filteredSections = computed(() =>
sections.value.filter((section) => section.hits.length > 0)
);
const typeMap = {
products: 'products',
solutions: 'solutions',
questions: 'questions',
product_documents: 'product_documents',
} as const;
// 展平hits
const hits = computed(() =>
filteredSections.value.flatMap((section) => {
const type = typeMap[section.rawIndex as keyof typeof typeMap];
if (!type) return [];
return section.hits.map((hit) => ({ type, content: hit }));
})
);
const searchItems = computed(() =>
hits.value.map((hit) => {
return toSearchItemView(hit.content, hit.type);
})
);
const hasResults = computed(() =>
filteredSections.value.some((section) => section.hits.length > 0)
);
const hasResults = computed(() => searchItems.value.length > 0);
watch(
() => route.query.query,
@ -84,6 +62,13 @@
}
);
watch(
() => locale,
async () => {
await refresh();
}
);
watch(error, (value) => {
if (value) {
logger.error('数据获取失败: ', value);