Skip to content

Promote name matches to top position + add name match badge after the package name. #8745

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 3 commits into from
May 15, 2025
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
1 change: 1 addition & 0 deletions app/lib/frontend/templates/admin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ String renderAccountPackagesPage({
searchForm: null,
sdkLibraryHits: [],
packageHits: packageHits,
nameMatches: null,
),
if (nextPackage != null)
d.div(
Expand Down
28 changes: 1 addition & 27 deletions app/lib/frontend/templates/listing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import 'dart:math';

import 'package:_pub_shared/search/search_form.dart';
import 'package:collection/collection.dart';

import '../../package/search_adapter.dart';
import '../../search/search_service.dart';
import '../../shared/urls.dart' as urls;
import '../dom/dom.dart' as d;

import '_consts.dart';
Expand All @@ -29,6 +27,7 @@ d.Node packageList(SearchResultPage searchResultPage) {
searchForm: searchResultPage.form,
sdkLibraryHits: searchResultPage.sdkLibraryHits,
packageHits: searchResultPage.packageHits,
nameMatches: searchResultPage.nameMatches,
);
}

Expand All @@ -50,7 +49,6 @@ String renderPkgIndexPage(
title: topPackages,
messageFromBackend: searchResultPage.errorMessage,
),
nameMatches: _nameMatches(searchForm, searchResultPage.nameMatches),
packageList: packageList(searchResultPage),
pagination: searchResultPage.hasHit ? paginationNode(links) : null,
openSections: openSections,
Expand Down Expand Up @@ -123,27 +121,3 @@ class PageLinks {
return min(fromSymmetry, max(currentPage!, fromCount));
}
}

d.Node? _nameMatches(SearchForm form, List<String>? matches) {
if (matches == null || matches.isEmpty) {
return null;
}
final singular = matches.length == 1;
final isExactNameMatch = singular && form.parsedQuery.text == matches.single;
final nameMatchLabel = isExactNameMatch
? 'Exact package name match: '
: 'Matching package ${singular ? 'name' : 'names'}: ';

return d.p(children: [
d.text(nameMatchLabel),
...matches.expandIndexed((i, name) {
return [
if (i > 0) d.text(', '),
d.a(
href: urls.pkgPageUrl(name),
child: d.b(text: name),
),
];
}),
]);
}
6 changes: 6 additions & 0 deletions app/lib/frontend/templates/package_misc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ final flutterFavoriteBadgeNode = packageBadgeNode(
),
);

/// Renders the name match badge node, used for exact package name hits.
final nameMatchBadgeNode = packageBadgeNode(
label: 'Name match',
color: 'name-match',
);

/// Renders the null-safe badge used by package listing and package page.
d.Node nullSafeBadgeNode({String? title}) {
return packageBadgeNode(
Expand Down
6 changes: 0 additions & 6 deletions app/lib/frontend/templates/views/pkg/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@ import '../../../static_files.dart';
d.Node packageListingNode({
required SearchForm searchForm,
required d.Node listingInfo,
required d.Node? nameMatches,
required d.Node packageList,
required d.Node? pagination,
required Set<String>? openSections,
}) {
final innerContent = d.fragment([
listingInfo,
if (nameMatches != null)
d.div(
classes: ['listing-highlight-block'],
child: nameMatches,
),
packageList,
if (pagination != null) pagination,
d.markdown('Check our help page for details on '
Expand Down
15 changes: 14 additions & 1 deletion app/lib/frontend/templates/views/pkg/package_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ d.Node listOfPackagesNode({
required SearchForm? searchForm,
required List<SdkLibraryHit> sdkLibraryHits,
required List<PackageView> packageHits,
required List<String>? nameMatches,
}) {
final bestNameMatch =
(nameMatches == null || nameMatches.isEmpty) ? null : nameMatches.first;
return d.div(
classes: ['packages'],
children: [
...sdkLibraryHits.map(_sdkLibraryItem),
...packageHits.map((hit) => _packageItem(hit, searchForm: searchForm)),
...packageHits.map((hit) => _packageItem(
hit,
searchForm: searchForm,
isNameMatch: hit.name == bestNameMatch,
)),
imageCarousel(),
],
);
Expand All @@ -48,6 +55,7 @@ d.Node _sdkLibraryItem(SdkLibraryHit hit) {
return _item(
url: hit.url!,
name: hit.library!,
isNameMatch: false,
newTimestamp: null,
labeledScoresNode: null,
description: hit.description ?? '',
Expand All @@ -71,6 +79,7 @@ d.Node _sdkLibraryItem(SdkLibraryHit hit) {
d.Node _packageItem(
PackageView view, {
required SearchForm? searchForm,
required bool isNameMatch,
}) {
final isFlutterFavorite = view.tags.contains(PackageTags.isFlutterFavorite);
final isNullSafe = view.tags.contains(PackageVersionTags.isNullSafe);
Expand Down Expand Up @@ -177,6 +186,7 @@ d.Node _packageItem(
screenshotDescriptions: screenshotDescriptions,
url: urls.pkgPageUrl(view.name),
name: view.name,
isNameMatch: isNameMatch,
newTimestamp: view.created,
labeledScoresNode: labeledScoresNodeFromPackageView(view),
description: view.ellipsizedDescription ?? '',
Expand Down Expand Up @@ -207,6 +217,7 @@ d.Node _item({
List<d.Node> topics = const [],
required String url,
required String name,
required bool isNameMatch,
required DateTime? newTimestamp,
required d.Node? labeledScoresNode,
required String description,
Expand All @@ -230,6 +241,8 @@ d.Node _item({
], children: [
d.a(href: url, text: name),
if (copyIcon != null) copyIcon,
d.text(' '),
if (isNameMatch) nameMatchBadgeNode,
]),
if (age != null && age.inDays <= 30)
d.div(
Expand Down
33 changes: 26 additions & 7 deletions app/lib/search/mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final _textSearchTimeout = Duration(milliseconds: 500);
class InMemoryPackageIndex {
final List<PackageDocument> _documents;
final _documentsByName = <String, PackageDocument>{};
final _nameToIndex = <String, int>{};
late final PackageNameIndex _packageNameIndex;
late final TokenIndex<String> _descrIndex;
late final TokenIndex<String> _readmeIndex;
Expand Down Expand Up @@ -62,6 +63,7 @@ class InMemoryPackageIndex {
for (var i = 0; i < _documents.length; i++) {
final doc = _documents[i];
_documentsByName[doc.package] = doc;
_nameToIndex[doc.package] = i;

// transform tags into numberical IDs
final tagIds = <int>[];
Expand Down Expand Up @@ -232,7 +234,9 @@ class InMemoryPackageIndex {
);

String? bestNameMatch;
if (parsedQueryText != null) {
if (parsedQueryText != null &&
query.parsedQuery.hasOnlyFreeText &&
query.isNaturalOrder) {
// exact package name
if (_documentsByName.containsKey(parsedQueryText)) {
bestNameMatch = parsedQueryText;
Expand Down Expand Up @@ -268,12 +272,18 @@ class InMemoryPackageIndex {
/// it linearly into the [0.4-1.0] range, to allow better
/// multiplication outcomes.
packageScores.multiplyAllFromValues(_adjustedOverallScores);
indexedHits = _rankWithValues(packageScores,
requiredLengthThreshold: query.offset);
indexedHits = _rankWithValues(
packageScores,
requiredLengthThreshold: query.offset,
bestNameMatch: bestNameMatch,
);
break;
case SearchOrder.text:
indexedHits = _rankWithValues(packageScores,
requiredLengthThreshold: query.offset);
indexedHits = _rankWithValues(
packageScores,
requiredLengthThreshold: query.offset,
bestNameMatch: bestNameMatch,
);
break;
case SearchOrder.created:
indexedHits = _createdOrderedHits.whereInScores(packageScores);
Expand Down Expand Up @@ -317,10 +327,14 @@ class InMemoryPackageIndex {
packageHits = indexedHits.map((h) => h.hit).toList();
}

// Only indicate name match when the first item's score is lower than the second's score.
final indicateNameMatch = bestNameMatch != null &&
packageHits.length > 1 &&
((packageHits[0].score ?? 0) <= (packageHits[1].score ?? 0));
return PackageSearchResult(
timestamp: clock.now().toUtc(),
totalCount: totalCount,
nameMatches: bestNameMatch == null ? null : [bestNameMatch],
nameMatches: indicateNameMatch ? [bestNameMatch] : null,
packageHits: packageHits,
errorMessage: textResults?.errorMessage,
);
Expand Down Expand Up @@ -471,11 +485,14 @@ class InMemoryPackageIndex {
IndexedScore<String> score, {
// if the item count is fewer than this threshold, an empty list will be returned
int? requiredLengthThreshold,
String? bestNameMatch,
}) {
final list = <IndexedPackageHit>[];
final bestNameIndex =
bestNameMatch == null ? null : _nameToIndex[bestNameMatch];
for (var i = 0; i < score.length; i++) {
final value = score.getValue(i);
if (value <= 0.0) continue;
if (value <= 0.0 && i != bestNameIndex) continue;
list.add(IndexedPackageHit(
i, PackageHit(package: score.keys[i], score: value)));
}
Expand All @@ -484,6 +501,8 @@ class InMemoryPackageIndex {
return [];
}
list.sort((a, b) {
if (a.index == bestNameIndex) return -1;
if (b.index == bestNameIndex) return 1;
final scoreCompare = -a.hit.score!.compareTo(b.hit.score!);
if (scoreCompare != 0) return scoreCompare;
// if two packages got the same score, order by last updated
Expand Down
4 changes: 2 additions & 2 deletions app/lib/search/search_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class ServiceSearchQuery {
late final effectiveOrder = parsedQuery.order ?? order;
bool get _hasQuery => query != null && query!.isNotEmpty;
bool get _hasOnlyFreeText => _hasQuery && parsedQuery.hasOnlyFreeText;
bool get _isNaturalOrder =>
bool get isNaturalOrder =>
effectiveOrder == null ||
effectiveOrder == SearchOrder.top ||
effectiveOrder == SearchOrder.text;
Expand All @@ -294,7 +294,7 @@ class ServiceSearchQuery {
bool get includeSdkResults =>
offset == 0 &&
_hasOnlyFreeText &&
_isNaturalOrder &&
isNaturalOrder &&
_hasNoOwnershipScope &&
!_isFlutterFavorite &&
(textMatchExtent ?? TextMatchExtent.api).shouldMatchApi();
Expand Down
1 change: 0 additions & 1 deletion app/test/search/api_doc_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ void main() {
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 2,
'nameMatches': ['foo'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'foo', 'score': 1.0}, // finds package name
Expand Down
1 change: 0 additions & 1 deletion app/test/search/flutter_iap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ void main() {
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 1,
'nameMatches': ['flutter_iap'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'flutter_iap', 'score': 1.0},
Expand Down
1 change: 0 additions & 1 deletion app/test/search/handlers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ void main() {
await expectJsonResponse(await issueGet('/search?q=oxygen'), body: {
'timestamp': isNotNull,
'totalCount': 1,
'nameMatches': ['oxygen'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'oxygen', 'score': isPositive},
Expand Down
1 change: 0 additions & 1 deletion app/test/search/haversine_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ MIT'''),
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 3,
'nameMatches': ['haversine'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'haversine', 'score': 1.0},
Expand Down
3 changes: 1 addition & 2 deletions app/test/search/json_tool_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ void main() {
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 1,
'nameMatches': ['jsontool'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'jsontool', 'score': 1.0},
Expand Down Expand Up @@ -70,8 +69,8 @@ void main() {
'nameMatches': ['jsontool'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'json2entity', 'score': 1.0},
{'package': 'jsontool', 'score': 1.0},
{'package': 'json2entity', 'score': 1.0},
{'package': 'json_to_model', 'score': closeTo(0.73, 0.01)},
],
});
Expand Down
3 changes: 1 addition & 2 deletions app/test/search/maps_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ void main() {
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 2,
'nameMatches': ['maps'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'maps', 'score': 1.0},
Expand All @@ -44,8 +43,8 @@ void main() {
'nameMatches': ['map'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'maps', 'score': 1.0},
{'package': 'map', 'score': 1.0},
{'package': 'maps', 'score': 1.0},
],
});
});
Expand Down
Loading