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 @@
1<script lang="ts"> 1<script lang="ts">
2 import { onMount } from 'svelte'; 2 import { onMount } from 'svelte';
3 import Fuse from 'fuse.js'; 3 import Fuse from 'fuse.js';
4 import { fetchCategories, slugify } from '@utils/fetch-data'; 4 import { fetchCategories } from '@utils/fetch-data';
5 import { formatLink } from '@utils/parse-markdown'; 5 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';
7 import type { SearchItem } from '@utils/do-searchy-searchy'; 11 import type { SearchItem } from '@utils/do-searchy-searchy';
8 12
9 interface Props { 13 interface Props {
@@ -20,13 +24,6 @@
20 fuse = new Fuse(items, searchOptions); 24 fuse = new Fuse(items, searchOptions);
21 }); 25 });
22 26
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
30 const makeResultText = (cat?: string, sec?: string, itm?: string) => { 27 const makeResultText = (cat?: string, sec?: string, itm?: string) => {
31 if (itm) return itm; 28 if (itm) return itm;
32 if (sec) return sec; 29 if (sec) return sec;
@@ -52,6 +49,7 @@
52 searchQuery && fuse 49 searchQuery && fuse
53 ? fuse 50 ? fuse
54 .search(searchQuery) 51 .search(searchQuery)
52 .filter(isRelevant)
55 .map((r) => r.item) 53 .map((r) => r.item)
56 .slice(0, 25) 54 .slice(0, 25)
57 : [], 55 : [],
@@ -68,7 +66,7 @@
68 event.preventDefault(); 66 event.preventDefault();
69 const active = results[activeIndex]; 67 const active = results[activeIndex];
70 window.location.href = active 68 window.location.href = active
71 ? makeResultLink(active.category, active.sectionName, active.name) 69 ? active.path
72 : `/search/${encodeURIComponent(searchQuery)}/`; 70 : `/search/${encodeURIComponent(searchQuery)}/`;
73 } 71 }
74 if (event.key === 'Escape') { 72 if (event.key === 'Escape') {
@@ -117,11 +115,7 @@
117 aria-selected={i === activeIndex} 115 aria-selected={i === activeIndex}
118 > 116 >
119 <a 117 <a
120 href={makeResultLink( 118 href={result.path}
121 result.category,
122 result.sectionName,
123 result.name,
124 )}
125 title={makeTitle(result.type, result.description)} 119 title={makeTitle(result.type, result.description)}
126 > 120 >
127 <span class="name"> 121 <span class="name">
web/src/pages/search/[...searchTerm].astro +7 −2
@@ -3,7 +3,11 @@ import Fuse from 'fuse.js';
3 3
4import Layout from '@layouts/Layout.astro'; 4import Layout from '@layouts/Layout.astro';
5import { fetchData, slugify } from '@utils/fetch-data'; 5import { 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';
7import type { SearchItem } from '@utils/do-searchy-searchy'; 11import type { SearchItem } from '@utils/do-searchy-searchy';
8import Search from '@components/things/Search.svelte'; 12import Search from '@components/things/Search.svelte';
9import SmartSuggestions from '@components/things/SmartSuggestions.svelte'; 13import SmartSuggestions from '@components/things/SmartSuggestions.svelte';
@@ -23,6 +27,7 @@ const searchTerm = Astro.params.searchTerm;
23 27
24const searchResults = fuse 28const searchResults = fuse
25 .search(searchTerm || '') 29 .search(searchTerm || '')
30 .filter(isRelevant)
26 .map((result) => result.item); 31 .map((result) => result.item);
27 32
28const services = searchResults.filter((result) => result.type === 'Service'); 33const services = searchResults.filter((result) => result.type === 'Service');
@@ -96,7 +101,7 @@ const beer = putResultsIntoGroups();
96 <ul class="service-list"> 101 <ul class="service-list">
97 {section.items.map((item) => ( 102 {section.items.map((item) => (
98 <li> 103 <li>
99 <a href={item.url}>{item.name}</a> 104 <a href={item.path}>{item.name}</a>
100 </li> 105 </li>
101 ))} 106 ))}
102 </ul> 107 </ul>
web/src/utils/do-searchy-searchy.ts +34 −5
@@ -1,8 +1,10 @@
1import type { Category } from '../types/Service'; 1import type { Category } from '../types/Service';
2import { slugify } from './fetch-data';
2 3
3export interface SearchItem { 4export interface SearchItem {
4 type: 'Category' | 'Section' | 'Service'; 5 type: 'Category' | 'Section' | 'Service';
5 category: string; 6 category: string;
7 path: string;
6 itemCount?: number; 8 itemCount?: number;
7 sectionName?: string; 9 sectionName?: string;
8 description?: string; 10 description?: string;
@@ -12,15 +14,20 @@ export interface SearchItem {
12 codeberg?: string; 14 codeberg?: string;
13 git?: string; 15 git?: string;
14 logo?: string; 16 logo?: string;
17 platforms?: string[];
18 traits?: string[];
19 alternativeTo?: string[];
15} 20}
16 21
17export const prepareSearchItems = (categories: Category[]): SearchItem[] => { 22export const prepareSearchItems = (categories: Category[]): SearchItem[] => {
18 const items: SearchItem[] = []; 23 const items: SearchItem[] = [];
19 // Add each category 24 // Add each category
20 categories.forEach((category) => { 25 categories.forEach((category) => {
26 const categoryPath = `/${slugify(category.name)}/`;
21 items.push({ 27 items.push({
22 type: 'Category', 28 type: 'Category',
23 category: category.name, 29 category: category.name,
30 path: categoryPath,
24 itemCount: (category.sections || []).reduce((acc, section) => { 31 itemCount: (category.sections || []).reduce((acc, section) => {
25 return acc + (section.services || []).length; 32 return acc + (section.services || []).length;
26 }, 0), 33 }, 0),
@@ -28,11 +35,13 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => {
28 35
29 // Add section with category context 36 // Add section with category context
30 category.sections.forEach((section) => { 37 category.sections.forEach((section) => {
38 const sectionPath = `${categoryPath}${slugify(section.name)}/`;
31 items.push({ 39 items.push({
32 type: 'Section', 40 type: 'Section',
33 sectionName: section.name, 41 sectionName: section.name,
34 description: section.intro || '', 42 description: section.intro || '',
35 category: category.name, 43 category: category.name,
44 path: sectionPath,
36 itemCount: (section.services || []).length, 45 itemCount: (section.services || []).length,
37 }); 46 });
38 47
@@ -48,7 +57,21 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => {
48 git: service.git || '', 57 git: service.git || '',
49 category: category.name, 58 category: category.name,
50 sectionName: section.name, 59 sectionName: section.name,
60 path: `${sectionPath}${slugify(service.name)}/`,
51 logo: service.icon || '', 61 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 || [],
52 }); 75 });
53 }); 76 });
54 }); 77 });
@@ -56,21 +79,27 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => {
56 return items; 79 return items;
57}; 80};
58 81
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
59export const searchOptions = { 88export const searchOptions = {
60 includeScore: true, 89 includeScore: true,
90 ignoreLocation: true,
91 threshold: 0.3,
61 keys: [ 92 keys: [
62 { name: 'name', weight: 0.9 }, 93 { name: 'name', weight: 0.9 },
63 { name: 'sectionName', weight: 0.8 }, 94 { name: 'sectionName', weight: 0.8 },
95 { name: 'alternativeTo', weight: 0.8 },
64 { name: 'category', weight: 0.7 }, 96 { name: 'category', weight: 0.7 },
65 { name: 'notableMentions', weight: 0.5 }, 97 { name: 'platforms', weight: 0.6 },
66 { name: 'alternativeTo', weight: 0.5 }, 98 { name: 'traits', weight: 0.6 },
67 { name: 'github', weight: 0.4 }, 99 { name: 'github', weight: 0.4 },
68 { name: 'codeberg', weight: 0.4 }, 100 { name: 'codeberg', weight: 0.4 },
69 { name: 'git', weight: 0.3 }, 101 { name: 'git', weight: 0.3 },
70 { name: 'url', weight: 0.2 }, 102 { name: 'url', weight: 0.2 },
71 { name: 'description', weight: 0.1 }, 103 { name: 'description', weight: 0.1 },
72 { name: 'intro', weight: 0.1 },
73 { name: 'furtherInfo', weight: 0.1 },
74 { name: 'wordOfWarning', weight: 0.1 },
75 ], 104 ],
76}; 105};