fix: 调整筛选条目排序权重
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m46s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m46s
- 以搜索条目匹配程度为标准排序 - 移除拼音支持
This commit is contained in:
@ -26,25 +26,12 @@ export function fuzzyMatch<T extends object>(
|
||||
source: T[],
|
||||
options: FuzzyFilterOptions<T>
|
||||
): T[] {
|
||||
const {
|
||||
keyword,
|
||||
keys,
|
||||
threshold = 0.35,
|
||||
minMatchCharLength = 1,
|
||||
locale = 'auto',
|
||||
} = options;
|
||||
|
||||
// --- 多语言比较器 ---
|
||||
const collator = new Intl.Collator(locale === 'auto' ? undefined : locale, {
|
||||
sensitivity: 'base',
|
||||
ignorePunctuation: true,
|
||||
});
|
||||
const { keyword, keys, threshold = 0.35, minMatchCharLength = 1 } = options;
|
||||
|
||||
// --- 文本标准化函数 ---
|
||||
const normalizeText = (text: string): string => {
|
||||
const normalizedText = text.normalize('NFKC').toLowerCase().trim();
|
||||
const translit = transliterateText(normalizedText);
|
||||
return `${normalizedText} ${translit}`;
|
||||
return normalizedText;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -102,29 +89,14 @@ export function fuzzyMatch<T extends object>(
|
||||
.replace(/\s+/g, '')
|
||||
.toLowerCase()
|
||||
.trim();
|
||||
const translit = transliterateText(normalized);
|
||||
logger.debug(`${normalized} => ${translit}`);
|
||||
// 拼接原文 + 转写,提升中外文混合匹配效果
|
||||
return `${normalized} ${translit}`;
|
||||
return normalized;
|
||||
}
|
||||
return value;
|
||||
},
|
||||
sortFn: (a, b) => {
|
||||
const itemA = a.item as T;
|
||||
const itemB = b.item as T;
|
||||
const key = keys[0];
|
||||
const aValue = itemA[key];
|
||||
const bValue = itemB[key];
|
||||
if (typeof aValue === 'string' && typeof bValue === 'string') {
|
||||
return collator.compare(aValue, bValue);
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
});
|
||||
|
||||
logger.debug('Fuzzy search options:', options);
|
||||
logger.debug('Fuzzy search keyword:', k);
|
||||
return fuse.search(k).map((result) => result.item);
|
||||
const result = fuse.search(k);
|
||||
return result.map((result) => result.item);
|
||||
}
|
||||
|
||||
return source.filter((item) =>
|
||||
|
||||
@ -29,7 +29,6 @@
|
||||
"meilisearch": "^0.53.0",
|
||||
"nuxt": "^4.0.3",
|
||||
"nuxt-directus": "5.7.0",
|
||||
"pinyin-pro": "^3.27.0",
|
||||
"sass": "^1.90.0",
|
||||
"sharp": "^0.34.3",
|
||||
"vue": "^3.5.18",
|
||||
|
||||
8
pnpm-lock.yaml
generated
8
pnpm-lock.yaml
generated
@ -62,9 +62,6 @@ importers:
|
||||
nuxt-directus:
|
||||
specifier: 5.7.0
|
||||
version: 5.7.0(magicast@0.3.5)
|
||||
pinyin-pro:
|
||||
specifier: ^3.27.0
|
||||
version: 3.27.0
|
||||
sass:
|
||||
specifier: ^1.90.0
|
||||
version: 1.92.1
|
||||
@ -4402,9 +4399,6 @@ packages:
|
||||
typescript:
|
||||
optional: true
|
||||
|
||||
pinyin-pro@3.27.0:
|
||||
resolution: {integrity: sha512-Osdgjwe7Rm17N2paDMM47yW+jUIUH3+0RGo8QP39ZTLpTaJVDK0T58hOLaMQJbcMmAebVuK2ePunTEVEx1clNQ==}
|
||||
|
||||
pkg-types@1.3.1:
|
||||
resolution: {integrity: sha512-/Jm5M4RvtBFVkKWRu2BLUTNP8/M2a+UwuAX+ae4770q1qVGtfjG+WTCupoZixokjmHiry8uI+dlY8KXYV5HVVQ==}
|
||||
|
||||
@ -10624,8 +10618,6 @@ snapshots:
|
||||
optionalDependencies:
|
||||
typescript: 5.9.2
|
||||
|
||||
pinyin-pro@3.27.0: {}
|
||||
|
||||
pkg-types@1.3.1:
|
||||
dependencies:
|
||||
confbox: 0.1.8
|
||||
|
||||
Reference in New Issue
Block a user