From ce02c42effc11fa1873698439ec8ba0964c1b5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kol=C3=A1rik?= Date: Fri, 14 Sep 2018 13:04:33 +0200 Subject: [PATCH] feat(relevance): use jsDelivr hits for ranking --- README.md | 3 ++- src/__tests__/__snapshots__/config.test.js.snap | 1 + src/config.js | 1 + src/jsDelivr.js | 13 ++++++++++++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 58340a111..99dade73b 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,8 @@ For every single NPM package, we create a record in the Algolia index. The resul "objectID": "babel-core", "_searchInternal": { "popularName": "babel-core", - "downloadsMagnitude": 8 + "downloadsMagnitude": 8, + "jsDelivrPopularity": 5 } } ``` diff --git a/src/__tests__/__snapshots__/config.test.js.snap b/src/__tests__/__snapshots__/config.test.js.snap index 5d17d9967..7b28bc9c7 100644 --- a/src/__tests__/__snapshots__/config.test.js.snap +++ b/src/__tests__/__snapshots__/config.test.js.snap @@ -34,6 +34,7 @@ Object { ], "customRanking": Array [ "desc(_searchInternal.downloadsMagnitude)", + "desc(_searchInternal.jsDelivrPopularity)", "desc(dependents)", "desc(downloadsLast30Days)", ], diff --git a/src/config.js b/src/config.js index 6d3f2c695..c1613706b 100644 --- a/src/config.js +++ b/src/config.js @@ -33,6 +33,7 @@ const defaultConfig = { ], customRanking: [ 'desc(_searchInternal.downloadsMagnitude)', + 'desc(_searchInternal.jsDelivrPopularity)', 'desc(dependents)', 'desc(downloadsLast30Days)', ], diff --git a/src/jsDelivr.js b/src/jsDelivr.js index 6016fc92e..46c54348b 100644 --- a/src/jsDelivr.js +++ b/src/jsDelivr.js @@ -19,5 +19,16 @@ export async function loadHits() { } export function getHits(pkgs) { - return pkgs.map(({ name }) => ({ jsDelivrHits: hits.get(name) || 0 })); + return pkgs.map(({ name }) => { + const jsDelivrHits = hits.get(name) || 0; + + return { + jsDelivrHits, + _searchInternal: { + // anything below 1000 hits/month is likely to mean that + // someone just made a few random requests so we count that as 0 + jsDelivrPopularity: Math.max(jsDelivrHits.toString().length - 3, 0), + }, + }; + }); }