Skip to content

Commit 1004d37

Browse files
committed
Select only a single name match in search.
1 parent 344745a commit 1004d37

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

app/lib/search/mem_index.dart

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,27 @@ class InMemoryPackageIndex {
225225
final textResults = _searchText(
226226
packageScores,
227227
parsedQueryText,
228-
includeNameMatches: (query.offset ?? 0) == 0,
229228
textMatchExtent: query.textMatchExtent ?? TextMatchExtent.api,
230229
);
231230

232-
final nameMatches = textResults?.nameMatches;
231+
String? bestNameMatch;
232+
if (parsedQueryText != null) {
233+
final matches = _packageNameIndex.lookupMatchingNames(parsedQueryText);
234+
if (matches != null && matches.isNotEmpty) {
235+
bestNameMatch = matches.length == 1
236+
? matches.single
237+
:
238+
// Note: to keep it simple, we select the most downloaded one from competing matches.
239+
matches.reduce((a, b) {
240+
if (_documentsByName[a]!.downloadCount >
241+
_documentsByName[b]!.downloadCount) {
242+
return a;
243+
} else {
244+
return b;
245+
}
246+
});
247+
}
248+
}
233249

234250
List<IndexedPackageHit> indexedHits;
235251
switch (query.effectiveOrder ?? SearchOrder.top) {
@@ -292,7 +308,7 @@ class InMemoryPackageIndex {
292308
return PackageSearchResult(
293309
timestamp: clock.now().toUtc(),
294310
totalCount: totalCount,
295-
nameMatches: nameMatches,
311+
nameMatches: bestNameMatch == null ? null : [bestNameMatch],
296312
packageHits: packageHits,
297313
errorMessage: textResults?.errorMessage,
298314
);
@@ -321,7 +337,6 @@ class InMemoryPackageIndex {
321337
_TextResults? _searchText(
322338
IndexedScore<String> packageScores,
323339
String? text, {
324-
required bool includeNameMatches,
325340
required TextMatchExtent textMatchExtent,
326341
}) {
327342
if (text == null || text.isEmpty) {
@@ -353,15 +368,6 @@ class InMemoryPackageIndex {
353368
return aborted;
354369
}
355370

356-
Set<String>? nameMatches;
357-
if (includeNameMatches) {
358-
final matches = _packageNameIndex.lookupMatchingNames(text);
359-
if (matches != null) {
360-
nameMatches ??= <String>{};
361-
nameMatches.addAll(matches);
362-
}
363-
}
364-
365371
// Multiple words are scored separately, and then the individual scores
366372
// are multiplied. We can use a package filter that is applied after each
367373
// word to reduce the scope of the later words based on the previous results.
@@ -373,14 +379,6 @@ class InMemoryPackageIndex {
373379
final matchApi = textMatchExtent.shouldMatchApi();
374380

375381
for (final word in words) {
376-
if (includeNameMatches) {
377-
final matches = _packageNameIndex.lookupMatchingNames(word);
378-
if (matches != null) {
379-
nameMatches ??= <String>{};
380-
nameMatches.addAll(matches);
381-
}
382-
}
383-
384382
_scorePool.withScore(
385383
value: 0.0,
386384
fn: (wordScore) {
@@ -454,10 +452,7 @@ class InMemoryPackageIndex {
454452
}
455453
}
456454

457-
return _TextResults(
458-
topApiPages,
459-
nameMatches: nameMatches?.toList(),
460-
);
455+
return _TextResults(topApiPages);
461456
}
462457

463458
List<IndexedPackageHit> _rankWithValues(
@@ -535,20 +530,17 @@ class InMemoryPackageIndex {
535530

536531
class _TextResults {
537532
final List<List<MapEntry<String, double>>?>? topApiPages;
538-
final List<String>? nameMatches;
539533
final String? errorMessage;
540534

541535
factory _TextResults.empty({String? errorMessage}) {
542536
return _TextResults(
543537
null,
544-
nameMatches: null,
545538
errorMessage: errorMessage,
546539
);
547540
}
548541

549542
_TextResults(
550543
this.topApiPages, {
551-
required this.nameMatches,
552544
this.errorMessage,
553545
});
554546
}

0 commit comments

Comments
 (0)