Skip to content

Commit c22a9e0

Browse files
committed
sort serch results by framework/sdk popularity
1 parent f940420 commit c22a9e0

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

scripts/algolia.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,39 @@ async function generateAlogliaRecords(pageFrontMatters: FrontMatter[]) {
129129
return records.flat();
130130
}
131131

132+
/**
133+
* Framework popularity ranking map - frameworks listed in order of priority
134+
*/
135+
const frameworkPopularity: Record<string, number> = {
136+
nextjs: 1,
137+
react: 2,
138+
'react-native': 3,
139+
python: 4,
140+
laravel: 5,
141+
node: 6,
142+
vue: 7,
143+
ios: 8,
144+
angular: 9,
145+
nestjs: 10,
146+
django: 11,
147+
spring: 12,
148+
go: 13,
149+
ruby: 14,
150+
kotlin: 15,
151+
dart: 16,
152+
unity: 17,
153+
};
154+
155+
const getPopularity = (sdk: string | undefined, framework: string | undefined) => {
156+
if (sdk && frameworkPopularity[sdk]) {
157+
return frameworkPopularity[sdk];
158+
}
159+
if (framework && frameworkPopularity[framework]) {
160+
return frameworkPopularity[framework];
161+
}
162+
return Number.MAX_SAFE_INTEGER;
163+
};
164+
132165
async function getRecords(pageFm: FrontMatter) {
133166
console.log('processing:', pageFm.slug);
134167

@@ -155,6 +188,8 @@ async function getRecords(pageFm: FrontMatter) {
155188
keywords: pageFm.keywords,
156189
sdk,
157190
framework,
191+
// @ts-ignore
192+
popularity: getPopularity(sdk, framework),
158193
},
159194
'#main'
160195
);

src/components/search/index.tsx

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,80 @@ export function Search({path, autoFocus, searchPlatforms = [], showChatBot}: Pro
320320
</div>
321321
);
322322
}
323+
324+
// const exampleDocsHit = {
325+
// id: 'f2274542a8fefa5e95bae958c36b97bd',
326+
// site: 'docs',
327+
// url: 'https://docs.sentry.io/platforms/javascript/guides/koa/troubleshooting/',
328+
// index: 'sentry-docs-v2',
329+
// context: {
330+
// context1: 'Platforms > JavaScript > Guides > Koa > Troubleshooting',
331+
// },
332+
// title: 'Troubleshooting',
333+
// text: 'If you need additional help, you can ask on GitHub . Customers on a paid plan …',
334+
// queryID: '021f95330896bc64cb93b22741badb26',
335+
// };
336+
337+
// const platformsByTraffic = [
338+
// 'nextjs',
339+
// 'react',
340+
// 'react-native',
341+
// 'python',
342+
// 'laravel',
343+
// 'node',
344+
// 'vue',
345+
// 'ios',
346+
// 'angular',
347+
// 'nestjs',
348+
// 'django',
349+
// 'spring',
350+
// 'go',
351+
// 'ruby',
352+
// 'kotlin',
353+
// 'dart',
354+
// 'unity',
355+
// ];
356+
357+
// function sortPlatforms(hits: Hit[]) {
358+
// console.log('👉 unsorted hits', hits);
359+
360+
// const getPlatform = (hit: Hit) => {
361+
// const url = new URL(hit.url);
362+
// const pathname = url.pathname.slice(1);
363+
// let sdkOrFramework: string | undefined;
364+
// if (pathname.includes('/guides/')) {
365+
// sdkOrFramework = pathname.split('/')[3];
366+
// } else if (pathname.includes('platforms/')) {
367+
// sdkOrFramework = pathname.split('/')[1];
368+
// }
369+
// return sdkOrFramework;
370+
// };
371+
372+
// const hitsWithPlatform = hits.map(h => ({...h, platform: getPlatform(h)}));
373+
374+
// const nonPlatformHits = hitsWithPlatform.filter(hit => !hit.platform);
375+
// const platformHits = hitsWithPlatform.filter(hit => !!hit.platform);
376+
377+
// const sortedPlatformHits = platformHits.slice().sort((a, b) => {
378+
// const aPlatform = a.platform ?? '';
379+
// const bPlatform = b.platform ?? '';
380+
// // some platforms are missing, we should prioritize the ones in the platformsByTraffic array
381+
// const aIndex = platformsByTraffic.indexOf(aPlatform);
382+
// const bIndex = platformsByTraffic.indexOf(bPlatform);
383+
// debugger;
384+
// let tie = 0;
385+
// if (aIndex === -1 && bIndex !== -1) {
386+
// tie = -1;
387+
// }
388+
// if (bIndex === -1 && aIndex !== -1) {
389+
// tie = 1;
390+
// }
391+
// // smaller index has more weight
392+
// const diff = bIndex - aIndex;
393+
// tie = diff < 0 ? -1 : diff > 0 ? 1 : 0;
394+
// console.log('👉 comparing', a.platform, b.platform, 'diff', diff, 'tie', tie);
395+
// return tie;
396+
// });
397+
// console.log('👉 sorted hits', sortedPlatformHits.concat(nonPlatformHits));
398+
// return sortedPlatformHits;
399+
// }

0 commit comments

Comments
 (0)