From 4891b78d89462453d2f39460b18e36dcf9bf4076 Mon Sep 17 00:00:00 2001 From: Istvan Soos Date: Tue, 18 Feb 2025 14:08:11 +0100 Subject: [PATCH 1/2] Use square root of points for normalized score. --- app/lib/search/mem_index.dart | 7 ++++--- app/test/search/mem_index_test.dart | 12 ++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/lib/search/mem_index.dart b/app/lib/search/mem_index.dart index bce0de1099..9c928f9e2b 100644 --- a/app/lib/search/mem_index.dart +++ b/app/lib/search/mem_index.dart @@ -313,9 +313,10 @@ class InMemoryPackageIndex { _adjustedOverallScores = _documents.map((doc) { final downloadScore = doc.downloadScore ?? 0.0; final likeScore = doc.likeScore ?? 0.0; - final combinedScore = (downloadScore + likeScore) / 2; - final points = doc.grantedPoints / math.max(1, doc.maxPoints); - final overall = combinedScore * 0.5 + points * 0.5; + final popularityScore = (downloadScore + likeScore) / 2; + final pointScore = + math.sqrt(doc.grantedPoints / math.max(1, doc.maxPoints)); + final overall = popularityScore * 0.5 + pointScore * 0.5; doc.overallScore = overall; // adding a base score prevents later multiplication with zero return 0.4 + 0.6 * overall; diff --git a/app/test/search/mem_index_test.dart b/app/test/search/mem_index_test.dart index 5fdcb04e63..a7e322ae42 100644 --- a/app/test/search/mem_index_test.dart +++ b/app/test/search/mem_index_test.dart @@ -98,7 +98,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'nameMatches': ['async'], 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'async', 'score': closeTo(0.65, 0.01)}, + {'package': 'async', 'score': closeTo(0.71, 0.01)}, ], }); }); @@ -143,8 +143,8 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'totalCount': 2, 'sdkLibraryHits': [], 'packageHits': [ + {'package': 'async', 'score': closeTo(0.71, 0.01)}, {'package': 'http', 'score': closeTo(0.69, 0.01)}, - {'package': 'async', 'score': closeTo(0.65, 0.01)}, ], }); }); @@ -157,7 +157,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'totalCount': 1, 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'async', 'score': closeTo(0.34, 0.01)}, + {'package': 'async', 'score': closeTo(0.37, 0.01)}, ], }); }); @@ -183,7 +183,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'totalCount': 1, 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'async', 'score': closeTo(0.24, 0.01)}, + {'package': 'async', 'score': closeTo(0.26, 0.01)}, ], }); }); @@ -335,7 +335,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'sdkLibraryHits': [], 'packageHits': [ {'package': 'http', 'score': closeTo(0.92, 0.01)}, - {'package': 'async', 'score': closeTo(0.41, 0.01)}, + {'package': 'async', 'score': closeTo(0.52, 0.01)}, ], }); @@ -422,7 +422,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'totalCount': 1, 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'async', 'score': closeTo(0.41, 0.01)}, + {'package': 'async', 'score': closeTo(0.52, 0.01)}, ], }); From 2ea5731a09cf4376e79874be4956676bb639e8e6 Mon Sep 17 00:00:00 2001 From: Istvan Soos Date: Tue, 18 Feb 2025 15:16:09 +0100 Subject: [PATCH 2/2] different normalization + comments --- app/lib/search/mem_index.dart | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/lib/search/mem_index.dart b/app/lib/search/mem_index.dart index 9c928f9e2b..687b474242 100644 --- a/app/lib/search/mem_index.dart +++ b/app/lib/search/mem_index.dart @@ -314,8 +314,13 @@ class InMemoryPackageIndex { final downloadScore = doc.downloadScore ?? 0.0; final likeScore = doc.likeScore ?? 0.0; final popularityScore = (downloadScore + likeScore) / 2; - final pointScore = - math.sqrt(doc.grantedPoints / math.max(1, doc.maxPoints)); + + // prevent division by zero in case maxPoints is zero + final pointRatio = doc.grantedPoints / math.max(1, doc.maxPoints); + // force value between 0.0 and 1.0 in case we have bad points + // using square root to lower the differences between higher values + final pointScore = math.sqrt(math.max(0.0, math.min(1.0, pointRatio))); + final overall = popularityScore * 0.5 + pointScore * 0.5; doc.overallScore = overall; // adding a base score prevents later multiplication with zero