diff --git a/app/lib/search/mem_index.dart b/app/lib/search/mem_index.dart index 157dda0f66..5f8ae3dc7e 100644 --- a/app/lib/search/mem_index.dart +++ b/app/lib/search/mem_index.dart @@ -479,7 +479,7 @@ class PackageNameIndex { /// Search [text] and return the matching packages with scores. @visibleForTesting - Score search(String text) { + Map search(String text) { IndexedScore? score; for (final w in splitForQuery(text)) { final s = searchWord(w, filterOnNonZeros: score); @@ -489,7 +489,7 @@ class PackageNameIndex { score.multiplyAllFrom(s); } } - return score?.toScore() ?? Score.empty; + return score?.toMap() ?? {}; } /// Search using the parsed [word] and return the matching packages with scores diff --git a/app/lib/search/sdk_mem_index.dart b/app/lib/search/sdk_mem_index.dart index ca41d7c921..fc80ed7a64 100644 --- a/app/lib/search/sdk_mem_index.dart +++ b/app/lib/search/sdk_mem_index.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:_pub_shared/utils/http.dart'; // ignore: implementation_imports import 'package:pana/src/dartdoc/dartdoc_index.dart'; @@ -135,18 +137,17 @@ class SdkMemIndex { final isQualifiedQuery = query.contains(library.split(':').last); final tokens = _tokensPerLibrary[library]!; - final plainResults = - Score(tokens.searchWords(words).top(3, minValue: 0.05)); + final plainResults = tokens.searchWords(words).top(3, minValue: 0.05); if (plainResults.isEmpty) continue; final libraryWeight = _libraryWeights[library] ?? 1.0; final weightedResults = isQualifiedQuery ? plainResults - : plainResults.mapValues( + : plainResults.map( (key, value) { final dir = p.dirname(key); final w = (_apiPageDirWeights[dir] ?? 1.0) * libraryWeight; - return w * value; + return MapEntry(key, w * value); }, ); @@ -185,9 +186,9 @@ class SdkMemIndex { class _Hit { final String library; - final Score top; + final Map top; _Hit(this.library, this.top); - late final score = top.maxValue; + late final score = top.values.fold(0.0, (a, b) => max(a, b)); } diff --git a/app/lib/search/token_index.dart b/app/lib/search/token_index.dart index 478facf0bc..84604dcd74 100644 --- a/app/lib/search/token_index.dart +++ b/app/lib/search/token_index.dart @@ -8,24 +8,6 @@ import 'package:meta/meta.dart'; import 'text_utils.dart'; -/// Represents an evaluated score as an {id: score} map. -extension type const Score._(Map _values) - implements Map { - static const Score empty = Score._({}); - - Score(this._values); - - factory Score.fromEntries(Iterable> entries) => - Score(Map.fromEntries(entries)); - - double get maxValue => _values.values.fold(0.0, math.max); - - /// Transfer the score values with [f]. - Score mapValues(double Function(String key, double value) f) => - Score.fromEntries( - _values.entries.map((e) => MapEntry(e.key, f(e.key, e.value)))); -} - /// The weighted tokens used for the final search. class TokenMatch { final Map _tokenWeights = {}; @@ -151,7 +133,7 @@ extension StringTokenIndexExt on TokenIndex { /// scoring. @visibleForTesting Map search(String text) { - return searchWords(splitForQuery(text)).toScore(); + return searchWords(splitForQuery(text)).toMap(); } } @@ -250,17 +232,15 @@ class IndexedScore { } return Map.fromEntries(list.map((i) => MapEntry(_keys[i], _values[i]))); } -} -extension StringIndexedScoreExt on IndexedScore { - Score toScore() { - final map = {}; + Map toMap() { + final map = {}; for (var i = 0; i < _values.length; i++) { final v = _values[i]; if (v > 0.0) { map[_keys[i]] = v; } } - return Score._(map); + return map; } }