Skip to content

Commit f6c1319

Browse files
committed
Use IndexedScores in package and SDK doc page search.
1 parent 55d4963 commit f6c1319

File tree

1 file changed

+10
-48
lines changed

1 file changed

+10
-48
lines changed

app/lib/search/token_index.dart

Lines changed: 10 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,19 @@ 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+
IndexedScore? score;
256220
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;
221+
final s = IndexedScore(_ids);
222+
searchAndAccumulate(w, score: s, weight: weight);
223+
if (score == null) {
224+
score = s;
225+
// NOTE: in the subsequent round(s), weight will be re-applied on the next word(s) too.
226+
} else {
227+
score.multiplyAllFrom(s);
265228
}
266-
scores.add(Score(values));
267229
}
268-
return Score.multiply(scores);
230+
return score?.toScore() ?? Score.empty;
269231
}
270232

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

0 commit comments

Comments
 (0)