Skip to content

Commit c198620

Browse files
authored
Select only a single name match in search. (#8743)
1 parent f6720dc commit c198620

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

app/lib/search/mem_index.dart

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,33 @@ 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+
// exact package name
234+
if (_documentsByName.containsKey(parsedQueryText)) {
235+
bestNameMatch = parsedQueryText;
236+
} else {
237+
// reduced package name match
238+
final matches = _packageNameIndex.lookupMatchingNames(parsedQueryText);
239+
if (matches != null && matches.isNotEmpty) {
240+
bestNameMatch = matches.length == 1
241+
? matches.single
242+
:
243+
// Note: to keep it simple, we select the most downloaded one from competing matches.
244+
matches.reduce((a, b) {
245+
if (_documentsByName[a]!.downloadCount >
246+
_documentsByName[b]!.downloadCount) {
247+
return a;
248+
} else {
249+
return b;
250+
}
251+
});
252+
}
253+
}
254+
}
233255

234256
List<IndexedPackageHit> indexedHits;
235257
switch (query.effectiveOrder ?? SearchOrder.top) {
@@ -292,7 +314,7 @@ class InMemoryPackageIndex {
292314
return PackageSearchResult(
293315
timestamp: clock.now().toUtc(),
294316
totalCount: totalCount,
295-
nameMatches: nameMatches,
317+
nameMatches: bestNameMatch == null ? null : [bestNameMatch],
296318
packageHits: packageHits,
297319
errorMessage: textResults?.errorMessage,
298320
);
@@ -321,7 +343,6 @@ class InMemoryPackageIndex {
321343
_TextResults? _searchText(
322344
IndexedScore<String> packageScores,
323345
String? text, {
324-
required bool includeNameMatches,
325346
required TextMatchExtent textMatchExtent,
326347
}) {
327348
if (text == null || text.isEmpty) {
@@ -353,15 +374,6 @@ class InMemoryPackageIndex {
353374
return aborted;
354375
}
355376

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-
365377
// Multiple words are scored separately, and then the individual scores
366378
// are multiplied. We can use a package filter that is applied after each
367379
// word to reduce the scope of the later words based on the previous results.
@@ -373,14 +385,6 @@ class InMemoryPackageIndex {
373385
final matchApi = textMatchExtent.shouldMatchApi();
374386

375387
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-
384388
_scorePool.withScore(
385389
value: 0.0,
386390
fn: (wordScore) {
@@ -454,10 +458,7 @@ class InMemoryPackageIndex {
454458
}
455459
}
456460

457-
return _TextResults(
458-
topApiPages,
459-
nameMatches: nameMatches?.toList(),
460-
);
461+
return _TextResults(topApiPages);
461462
}
462463

463464
List<IndexedPackageHit> _rankWithValues(
@@ -535,20 +536,17 @@ class InMemoryPackageIndex {
535536

536537
class _TextResults {
537538
final List<List<MapEntry<String, double>>?>? topApiPages;
538-
final List<String>? nameMatches;
539539
final String? errorMessage;
540540

541541
factory _TextResults.empty({String? errorMessage}) {
542542
return _TextResults(
543543
null,
544-
nameMatches: null,
545544
errorMessage: errorMessage,
546545
);
547546
}
548547

549548
_TextResults(
550549
this.topApiPages, {
551-
required this.nameMatches,
552550
this.errorMessage,
553551
});
554552
}

0 commit comments

Comments
 (0)