import MiniSearch from 'minisearch'
const tokenizer = (str) => {
const words = str.match(/[\u4e00-\u9fa5]+|[a-zA-Z0-9]+/g) || [];
const subStrings = [];
words.forEach(word => {
if (/^[a-zA-Z0-9]+$/.test(word)) {
for (let i = 1; i <= word.length; i++) {
subStrings.push(...word.slice(0, i));
}
} else {
for (let i = 1; i <= word.length; i++) {
for (let j = 0; j <= word.length - i; j++) {
subStrings.push(word.substring(j, j + i));
}
}
}
});
return subStrings.filter((value, index, self) => self.indexOf(value) === index);
};
let miniSearch = new MiniSearch({
fields: ['name', 'description'],
storeFields: ['name', 'description', 'url'],
tokenize: tokenizer
})
miniSearch.addAll(documents);
miniSearch.autoSuggest('搜索词', {
filter: (result) => {
searchData.value.push({
name: result.name,
description: result.description,
url: result.url
})
return true
}
})