Commit 8ef016c318

8ef016c318bf93d80e862d58cc0760d53d77ea04

parent: 0997f021ed

Unsigned

Alicia Sykes <alicia@omg.lol> · 2026-09-06 10:37 UTC

Fixes the search weights for fuzzy search

Layout: unified · split

web/src/components/things/Search.svelte +9 −15
@@ -1,9 +1,13 @@
11<script lang="ts">
22 import { onMount } from 'svelte';
33 import Fuse from 'fuse.js';
4 import { fetchCategories, slugify } from '@utils/fetch-data';
4 import { fetchCategories } from '@utils/fetch-data';
55 import { formatLink } from '@utils/parse-markdown';
6 import { prepareSearchItems, searchOptions } from '@utils/do-searchy-searchy';
6 import {
7 prepareSearchItems,
8 searchOptions,
9 isRelevant,
10 } from '@utils/do-searchy-searchy';
711 import type { SearchItem } from '@utils/do-searchy-searchy';
812
913 interface Props {
@@ -20,13 +24,6 @@
2024 fuse = new Fuse(items, searchOptions);
2125 });
2226
23 const makeResultLink = (cat?: string, sec?: string, itm?: string) => {
24 if (!cat) return '/';
25 if (!sec) return `/${slugify(cat)}/`;
26 if (!itm) return `/${slugify(cat)}/${slugify(sec)}/`;
27 return `/${slugify(cat)}/${slugify(sec)}/${slugify(itm)}/`;
28 };
29
3027 const makeResultText = (cat?: string, sec?: string, itm?: string) => {
3128 if (itm) return itm;
3229 if (sec) return sec;
@@ -52,6 +49,7 @@
5249 searchQuery && fuse
5350 ? fuse
5451 .search(searchQuery)
52 .filter(isRelevant)
5553 .map((r) => r.item)
5654 .slice(0, 25)
5755 : [],
@@ -68,7 +66,7 @@
6866 event.preventDefault();
6967 const active = results[activeIndex];
7068 window.location.href = active
71 ? makeResultLink(active.category, active.sectionName, active.name)
69 ? active.path
7270 : `/search/${encodeURIComponent(searchQuery)}/`;
7371 }
7472 if (event.key === 'Escape') {
@@ -117,11 +115,7 @@
117115 aria-selected={i === activeIndex}
118116 >
119117 <a
120 href={makeResultLink(
121 result.category,
122 result.sectionName,
123 result.name,
124 )}
118 href={result.path}
125119 title={makeTitle(result.type, result.description)}
126120 >
127121 <span class="name">
web/src/pages/search/[...searchTerm].astro +7 −2
@@ -3,7 +3,11 @@ import Fuse from 'fuse.js';
33
44import Layout from '@layouts/Layout.astro';
55import { fetchData, slugify } from '@utils/fetch-data';
6import { prepareSearchItems, searchOptions } from '@utils/do-searchy-searchy';
6import {
7 prepareSearchItems,
8 searchOptions,
9 isRelevant,
10} from '@utils/do-searchy-searchy';
711import type { SearchItem } from '@utils/do-searchy-searchy';
812import Search from '@components/things/Search.svelte';
913import SmartSuggestions from '@components/things/SmartSuggestions.svelte';
@@ -23,6 +27,7 @@ const searchTerm = Astro.params.searchTerm;
2327
2428const searchResults = fuse
2529 .search(searchTerm || '')
30 .filter(isRelevant)
2631 .map((result) => result.item);
2732
2833const services = searchResults.filter((result) => result.type === 'Service');
@@ -96,7 +101,7 @@ const beer = putResultsIntoGroups();
96101 <ul class="service-list">
97102 {section.items.map((item) => (
98103 <li>
99 <a href={item.url}>{item.name}</a>
104 <a href={item.path}>{item.name}</a>
100105 </li>
101106 ))}
102107 </ul>
web/src/utils/do-searchy-searchy.ts +34 −5
@@ -1,8 +1,10 @@
11import type { Category } from '../types/Service';
2import { slugify } from './fetch-data';
23
34export interface SearchItem {
45 type: 'Category' | 'Section' | 'Service';
56 category: string;
7 path: string;
68 itemCount?: number;
79 sectionName?: string;
810 description?: string;
@@ -12,15 +14,20 @@ export interface SearchItem {
1214 codeberg?: string;
1315 git?: string;
1416 logo?: string;
17 platforms?: string[];
18 traits?: string[];
19 alternativeTo?: string[];
1520}
1621
1722export const prepareSearchItems = (categories: Category[]): SearchItem[] => {
1823 const items: SearchItem[] = [];
1924 // Add each category
2025 categories.forEach((category) => {
26 const categoryPath = `/${slugify(category.name)}/`;
2127 items.push({
2228 type: 'Category',
2329 category: category.name,
30 path: categoryPath,
2431 itemCount: (category.sections || []).reduce((acc, section) => {
2532 return acc + (section.services || []).length;
2633 }, 0),
@@ -28,11 +35,13 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => {
2835
2936 // Add section with category context
3037 category.sections.forEach((section) => {
38 const sectionPath = `${categoryPath}${slugify(section.name)}/`;
3139 items.push({
3240 type: 'Section',
3341 sectionName: section.name,
3442 description: section.intro || '',
3543 category: category.name,
44 path: sectionPath,
3645 itemCount: (section.services || []).length,
3746 });
3847
@@ -48,7 +57,21 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => {
4857 git: service.git || '',
4958 category: category.name,
5059 sectionName: section.name,
60 path: `${sectionPath}${slugify(service.name)}/`,
5161 logo: service.icon || '',
62 // Get the plafrorm from followWith field or android/ios app
63 platforms: Object.keys(service)
64 .filter((key) => key.endsWith('App'))
65 .map((key) => key.replace(/App$/, ''))
66 .concat(service.followWith || []),
67 // Boolean fields, as words: `openSource` -> "open source"
68 traits: Object.entries(service)
69 .filter(([, value]) => value === true)
70 .map(([key]) =>
71 key.replace(/[A-Z]/g, (c) => ` ${c.toLowerCase()}`),
72 ),
73 // The proprietary tools this section replaces, eg "whatsapp"
74 alternativeTo: section.alternativeTo || [],
5275 });
5376 });
5477 });
@@ -56,21 +79,27 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => {
5679 return items;
5780};
5881
82// Set the fuzziness power. 0 = perfect match, and 1 = barley matched
83export const SEARCH_SCORE_CUTOFF = 0.85;
84
85export const isRelevant = (result: { score?: number }): boolean =>
86 (result.score ?? 0) <= SEARCH_SCORE_CUTOFF;
87
5988export const searchOptions = {
6089 includeScore: true,
90 ignoreLocation: true,
91 threshold: 0.3,
6192 keys: [
6293 { name: 'name', weight: 0.9 },
6394 { name: 'sectionName', weight: 0.8 },
95 { name: 'alternativeTo', weight: 0.8 },
6496 { name: 'category', weight: 0.7 },
65 { name: 'notableMentions', weight: 0.5 },
66 { name: 'alternativeTo', weight: 0.5 },
97 { name: 'platforms', weight: 0.6 },
98 { name: 'traits', weight: 0.6 },
6799 { name: 'github', weight: 0.4 },
68100 { name: 'codeberg', weight: 0.4 },
69101 { name: 'git', weight: 0.3 },
70102 { name: 'url', weight: 0.2 },
71103 { name: 'description', weight: 0.1 },
72 { name: 'intro', weight: 0.1 },
73 { name: 'furtherInfo', weight: 0.1 },
74 { name: 'wordOfWarning', weight: 0.1 },
75104 ],
76105};