feat(search): implement Aho-Corasick for efficient substring searching and improve result ordering

This commit is contained in:
2025-08-10 22:29:15 +02:00
parent 7af7d9ecd0
commit 11fda4e11f
2 changed files with 35 additions and 10 deletions

View File

@@ -59,15 +59,19 @@ export const SearchDialog: React.FC = () => {
}, [open, all.length]);
useEffect(() => {
if (query.trim().length === 0) { setResults([]); return; }
const out: Array<SearchResult> = [];
const q = query.trim().toLowerCase();
if (q.length === 0) { setResults([]); return; }
const scored: Array<{ s: SearchResult; idx: number }> = [];
for (const r of all) {
if (automaton.searchHas(r.system.toLowerCase())) {
out.push(r);
if (out.length >= 10) break;
}
const idx = automaton.searchFirstIndex(r.system.toLowerCase());
if (idx >= 0) scored.push({ s: r, idx });
}
setResults(out);
scored.sort((a, b) => {
if (a.idx !== b.idx) return a.idx - b.idx; // earlier index first
if (a.s.system.length !== b.s.system.length) return a.s.system.length - b.s.system.length; // shorter name next
return a.s.system.localeCompare(b.s.system);
});
setResults(scored.slice(0, 10).map(x => x.s));
}, [query, all, automaton]);
const onSelect = (r: SearchResult) => {