diff --git a/app/lib/search/models.dart b/app/lib/search/models.dart index 47beb3f222..cd21d75554 100644 --- a/app/lib/search/models.dart +++ b/app/lib/search/models.dart @@ -2,6 +2,8 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'dart:math'; + import 'package:clock/clock.dart'; import 'package:collection/collection.dart'; import 'package:json_annotation/json_annotation.dart'; @@ -68,7 +70,7 @@ extension UpdateLikesExt on Iterable { if (i > 0 && list[i - 1].downloadCount == list[i].downloadCount) { list[i].downloadScore = list[i - 1].downloadScore; } else { - list[i].downloadScore = (i + 1) / list.length; + list[i].downloadScore = pow((i + 1) / list.length, 2).toDouble(); } } } @@ -77,13 +79,12 @@ extension UpdateLikesExt on Iterable { /// The score is normalized into the range of [0.0 - 1.0] using the /// ordered list of packages by like counts (same like count gets the same score). void updateLikeScores() { - final sortedByLikes = sorted((a, b) => a.likeCount.compareTo(b.likeCount)); - for (var i = 0; i < sortedByLikes.length; i++) { - if (i > 0 && - sortedByLikes[i - 1].likeCount == sortedByLikes[i].likeCount) { - sortedByLikes[i].likeScore = sortedByLikes[i - 1].likeScore; + final list = sorted((a, b) => a.likeCount.compareTo(b.likeCount)); + for (var i = 0; i < list.length; i++) { + if (i > 0 && list[i - 1].likeCount == list[i].likeCount) { + list[i].likeScore = list[i - 1].likeScore; } else { - sortedByLikes[i].likeScore = (i + 1) / sortedByLikes.length; + list[i].likeScore = pow((i + 1) / list.length, 2).toDouble(); } } } diff --git a/app/test/search/mem_index_test.dart b/app/test/search/mem_index_test.dart index f9b6a73119..b92e8ff799 100644 --- a/app/test/search/mem_index_test.dart +++ b/app/test/search/mem_index_test.dart @@ -106,7 +106,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'totalCount': 1, 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'async', 'score': closeTo(0.71, 0.01)}, + {'package': 'async', 'score': closeTo(0.68, 0.01)}, ], }); }); @@ -134,7 +134,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'totalCount': 1, 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'chrome_net', 'score': closeTo(0.33, 0.01)}, + {'package': 'chrome_net', 'score': closeTo(0.30, 0.01)}, ], }); }); @@ -148,8 +148,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.67, 0.01)}, ], }); }); @@ -163,7 +163,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'totalCount': 1, 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'async', 'score': closeTo(0.37, 0.01)}, + {'package': 'async', 'score': closeTo(0.35, 0.01)}, ], }); }); @@ -348,7 +348,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.52, 0.01)}, + {'package': 'async', 'score': closeTo(0.46, 0.01)}, ], }); @@ -368,7 +368,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'totalCount': 1, 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'chrome_net', 'score': closeTo(0.08, 0.01)}, + {'package': 'chrome_net', 'score': closeTo(0.03, 0.01)}, ], }); }); @@ -383,7 +383,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'sdkLibraryHits': [], 'packageHits': [ {'package': 'http', 'score': closeTo(0.92, 0.01)}, - {'package': 'chrome_net', 'score': closeTo(0.08, 0.01)}, + {'package': 'chrome_net', 'score': closeTo(0.03, 0.01)}, ], }); @@ -443,7 +443,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'totalCount': 1, 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'async', 'score': closeTo(0.52, 0.01)}, + {'package': 'async', 'score': closeTo(0.46, 0.01)}, ], }); @@ -677,7 +677,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'totalCount': 2, 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'def', 'score': closeTo(0.85, 0.01)}, + {'package': 'def', 'score': closeTo(0.78, 0.01)}, {'package': 'abc', 'score': closeTo(0.70, 0.01)}, ], }, @@ -693,7 +693,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'packageHits': [ // `abc` is at the first position, score is kept {'package': 'abc', 'score': closeTo(0.70, 0.01)}, - {'package': 'def', 'score': closeTo(0.85, 0.01)}, + {'package': 'def', 'score': closeTo(0.78, 0.01)}, ], }, ); @@ -705,7 +705,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'totalCount': 2, 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'def', 'score': closeTo(0.85, 0.01)}, + {'package': 'def', 'score': closeTo(0.78, 0.01)}, {'package': 'abc', 'score': closeTo(0.70, 0.01)}, ], }, @@ -721,7 +721,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'sdkLibraryHits': [], 'packageHits': [ // `abc` is at the original position - {'package': 'def', 'score': closeTo(0.85, 0.01)}, + {'package': 'def', 'score': closeTo(0.78, 0.01)}, {'package': 'abc', 'score': closeTo(0.70, 0.01)}, ], }, @@ -737,7 +737,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'sdkLibraryHits': [], 'packageHits': [ // `abc` is absent - {'package': 'def', 'score': closeTo(0.85, 0.01)}, + {'package': 'def', 'score': closeTo(0.78, 0.01)}, ], }, ); @@ -775,7 +775,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure 'nameMatches': ['abc_def'], 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'abc_def', 'score': closeTo(0.55, 0.01)}, + {'package': 'abc_def', 'score': closeTo(0.48, 0.01)}, {'package': 'abc_def_xyz', 'score': closeTo(1.0, 0.01)}, ], }); diff --git a/app/test/service/entrypoint/search_index_test.dart b/app/test/service/entrypoint/search_index_test.dart index e3a4cadd7d..8ced0b4eaa 100644 --- a/app/test/service/entrypoint/search_index_test.dart +++ b/app/test/service/entrypoint/search_index_test.dart @@ -49,7 +49,7 @@ void main() { 'totalCount': 1, 'sdkLibraryHits': [], 'packageHits': [ - {'package': 'json_annotation', 'score': greaterThan(0.5)}, + {'package': 'json_annotation', 'score': greaterThan(0.47)}, ], });