Skip to content

Commit 9940bdb

Browse files
authored
Use IndexedScores in package and SDK doc page search + fixing weight. (#8241)
1 parent 55d4963 commit 9940bdb

File tree

2 files changed

+13
-50
lines changed

2 files changed

+13
-50
lines changed

app/lib/search/token_index.dart

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -206,38 +206,6 @@ class TokenIndex {
206206
return tokenMatch;
207207
}
208208

209-
/// Returns an {id: score} map of the documents stored in the [TokenIndex].
210-
/// The tokens in [tokenMatch] will be used to calculate a weighted sum of scores.
211-
///
212-
/// When [limitToIds] is specified, the result will contain only the set of
213-
/// identifiers in it.
214-
Map<String, double> _scoreDocs(TokenMatch tokenMatch,
215-
{double weight = 1.0, Set<String>? limitToIds}) {
216-
// Summarize the scores for the documents.
217-
final scores = IndexedScore(_ids);
218-
for (final token in tokenMatch.tokens) {
219-
final docWeights = _inverseIds[token]!;
220-
for (final e in docWeights.entries) {
221-
scores.setValueMaxOf(e.key, tokenMatch[token]! * e.value);
222-
}
223-
}
224-
225-
if (limitToIds != null) {
226-
scores.retainWhere((_, id) => limitToIds.contains(id));
227-
}
228-
final result = <String, double>{};
229-
// post-process match weights
230-
for (var i = 0; i < _length; i++) {
231-
final w = scores._values[i];
232-
if (w <= 0.0) {
233-
continue;
234-
}
235-
final id = _ids[i];
236-
result[id] = scores._values[i] * weight;
237-
}
238-
return result;
239-
}
240-
241209
/// Search the index for [text], with a (term-match / document coverage percent)
242210
/// scoring.
243211
@visibleForTesting
@@ -247,25 +215,20 @@ class TokenIndex {
247215

248216
/// Search the index for [words], with a (term-match / document coverage percent)
249217
/// scoring.
250-
Score searchWords(List<String> words,
251-
{double weight = 1.0, Set<String>? limitToIds}) {
252-
if (limitToIds != null && limitToIds.isEmpty) {
253-
return Score.empty;
254-
}
255-
final scores = <Score>[];
218+
Score searchWords(List<String> words, {double weight = 1.0}) {
219+
if (words.isEmpty) return Score.empty;
220+
IndexedScore? score;
221+
weight = math.pow(weight, 1 / words.length).toDouble();
256222
for (final w in words) {
257-
final tokens = lookupTokens(w);
258-
final values = _scoreDocs(
259-
tokens,
260-
weight: weight,
261-
limitToIds: limitToIds,
262-
);
263-
if (values.isEmpty) {
264-
return Score.empty;
223+
final s = IndexedScore(_ids);
224+
searchAndAccumulate(w, score: s, weight: weight);
225+
if (score == null) {
226+
score = s;
227+
} else {
228+
score.multiplyAllFrom(s);
265229
}
266-
scores.add(Score(values));
267230
}
268-
return Score.multiply(scores);
231+
return score?.toScore() ?? Score.empty;
269232
}
270233

271234
/// Searches the index with [word] and stores the results in [score], using

app/test/search/api_doc_page_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void main() {
9999
'packageHits': [
100100
{
101101
'package': 'foo',
102-
'score': closeTo(0.18, 0.01), // find WebPageGenerator
102+
'score': closeTo(0.26, 0.01), // find WebPageGenerator
103103
'apiPages': [
104104
{'path': 'generator.html'},
105105
],
@@ -119,7 +119,7 @@ void main() {
119119
'packageHits': [
120120
{
121121
'package': 'foo',
122-
'score': closeTo(0.11, 0.01), // find WebPageGenerator
122+
'score': closeTo(0.16, 0.01), // find WebPageGenerator
123123
'apiPages': [
124124
{'path': 'generator.html'},
125125
],

0 commit comments

Comments
 (0)