Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 11 additions & 48 deletions app/lib/search/token_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,38 +206,6 @@ class TokenIndex {
return tokenMatch;
}

/// Returns an {id: score} map of the documents stored in the [TokenIndex].
/// The tokens in [tokenMatch] will be used to calculate a weighted sum of scores.
///
/// When [limitToIds] is specified, the result will contain only the set of
/// identifiers in it.
Map<String, double> _scoreDocs(TokenMatch tokenMatch,
{double weight = 1.0, Set<String>? limitToIds}) {
// Summarize the scores for the documents.
final scores = IndexedScore(_ids);
for (final token in tokenMatch.tokens) {
final docWeights = _inverseIds[token]!;
for (final e in docWeights.entries) {
scores.setValueMaxOf(e.key, tokenMatch[token]! * e.value);
}
}

if (limitToIds != null) {
scores.retainWhere((_, id) => limitToIds.contains(id));
}
final result = <String, double>{};
// post-process match weights
for (var i = 0; i < _length; i++) {
final w = scores._values[i];
if (w <= 0.0) {
continue;
}
final id = _ids[i];
result[id] = scores._values[i] * weight;
}
return result;
}

/// Search the index for [text], with a (term-match / document coverage percent)
/// scoring.
@visibleForTesting
Expand All @@ -247,25 +215,20 @@ class TokenIndex {

/// Search the index for [words], with a (term-match / document coverage percent)
/// scoring.
Score searchWords(List<String> words,
{double weight = 1.0, Set<String>? limitToIds}) {
if (limitToIds != null && limitToIds.isEmpty) {
return Score.empty;
}
final scores = <Score>[];
Score searchWords(List<String> words, {double weight = 1.0}) {
if (words.isEmpty) return Score.empty;
IndexedScore? score;
weight = math.pow(weight, 1 / words.length).toDouble();
for (final w in words) {
final tokens = lookupTokens(w);
final values = _scoreDocs(
tokens,
weight: weight,
limitToIds: limitToIds,
);
if (values.isEmpty) {
return Score.empty;
final s = IndexedScore(_ids);
searchAndAccumulate(w, score: s, weight: weight);
if (score == null) {
score = s;
} else {
score.multiplyAllFrom(s);
}
scores.add(Score(values));
}
return Score.multiply(scores);
return score?.toScore() ?? Score.empty;
}

/// Searches the index with [word] and stores the results in [score], using
Expand Down
4 changes: 2 additions & 2 deletions app/test/search/api_doc_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void main() {
'packageHits': [
{
'package': 'foo',
'score': closeTo(0.18, 0.01), // find WebPageGenerator
'score': closeTo(0.26, 0.01), // find WebPageGenerator
'apiPages': [
{'path': 'generator.html'},
],
Expand All @@ -119,7 +119,7 @@ void main() {
'packageHits': [
{
'package': 'foo',
'score': closeTo(0.11, 0.01), // find WebPageGenerator
'score': closeTo(0.16, 0.01), // find WebPageGenerator
'apiPages': [
{'path': 'generator.html'},
],
Expand Down
Loading