Skip to content

Select only a single name match in search. #8743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2025
Merged
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
54 changes: 26 additions & 28 deletions app/lib/search/mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,33 @@ class InMemoryPackageIndex {
final textResults = _searchText(
packageScores,
parsedQueryText,
includeNameMatches: (query.offset ?? 0) == 0,
textMatchExtent: query.textMatchExtent ?? TextMatchExtent.api,
);

final nameMatches = textResults?.nameMatches;
String? bestNameMatch;
if (parsedQueryText != null) {
// exact package name
if (_documentsByName.containsKey(parsedQueryText)) {
bestNameMatch = parsedQueryText;
} else {
// reduced package name match
final matches = _packageNameIndex.lookupMatchingNames(parsedQueryText);
if (matches != null && matches.isNotEmpty) {
bestNameMatch = matches.length == 1
? matches.single
:
// Note: to keep it simple, we select the most downloaded one from competing matches.
matches.reduce((a, b) {
if (_documentsByName[a]!.downloadCount >
_documentsByName[b]!.downloadCount) {
return a;
} else {
return b;
}
});
}
}
}

List<IndexedPackageHit> indexedHits;
switch (query.effectiveOrder ?? SearchOrder.top) {
Expand Down Expand Up @@ -292,7 +314,7 @@ class InMemoryPackageIndex {
return PackageSearchResult(
timestamp: clock.now().toUtc(),
totalCount: totalCount,
nameMatches: nameMatches,
nameMatches: bestNameMatch == null ? null : [bestNameMatch],
packageHits: packageHits,
errorMessage: textResults?.errorMessage,
);
Expand Down Expand Up @@ -321,7 +343,6 @@ class InMemoryPackageIndex {
_TextResults? _searchText(
IndexedScore<String> packageScores,
String? text, {
required bool includeNameMatches,
required TextMatchExtent textMatchExtent,
}) {
if (text == null || text.isEmpty) {
Expand Down Expand Up @@ -353,15 +374,6 @@ class InMemoryPackageIndex {
return aborted;
}

Set<String>? nameMatches;
if (includeNameMatches) {
final matches = _packageNameIndex.lookupMatchingNames(text);
if (matches != null) {
nameMatches ??= <String>{};
nameMatches.addAll(matches);
}
}

// Multiple words are scored separately, and then the individual scores
// are multiplied. We can use a package filter that is applied after each
// word to reduce the scope of the later words based on the previous results.
Expand All @@ -373,14 +385,6 @@ class InMemoryPackageIndex {
final matchApi = textMatchExtent.shouldMatchApi();

for (final word in words) {
if (includeNameMatches) {
final matches = _packageNameIndex.lookupMatchingNames(word);
if (matches != null) {
nameMatches ??= <String>{};
nameMatches.addAll(matches);
}
}

_scorePool.withScore(
value: 0.0,
fn: (wordScore) {
Expand Down Expand Up @@ -454,10 +458,7 @@ class InMemoryPackageIndex {
}
}

return _TextResults(
topApiPages,
nameMatches: nameMatches?.toList(),
);
return _TextResults(topApiPages);
}

List<IndexedPackageHit> _rankWithValues(
Expand Down Expand Up @@ -535,20 +536,17 @@ class InMemoryPackageIndex {

class _TextResults {
final List<List<MapEntry<String, double>>?>? topApiPages;
final List<String>? nameMatches;
final String? errorMessage;

factory _TextResults.empty({String? errorMessage}) {
return _TextResults(
null,
nameMatches: null,
errorMessage: errorMessage,
);
}

_TextResults(
this.topApiPages, {
required this.nameMatches,
this.errorMessage,
});
}
Expand Down