Skip to content

Commit 5348995

Browse files
committed
Don't promote name match when other filters are present.
1 parent c1e34d9 commit 5348995

File tree

11 files changed

+34
-27
lines changed

11 files changed

+34
-27
lines changed

app/lib/search/mem_index.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ class InMemoryPackageIndex {
234234
);
235235

236236
String? bestNameMatch;
237-
if (parsedQueryText != null) {
237+
if (parsedQueryText != null &&
238+
query.parsedQuery.hasOnlyFreeText &&
239+
query.isNaturalOrder) {
238240
// exact package name
239241
if (_documentsByName.containsKey(parsedQueryText)) {
240242
bestNameMatch = parsedQueryText;
@@ -277,8 +279,11 @@ class InMemoryPackageIndex {
277279
);
278280
break;
279281
case SearchOrder.text:
280-
indexedHits = _rankWithValues(packageScores,
281-
requiredLengthThreshold: query.offset);
282+
indexedHits = _rankWithValues(
283+
packageScores,
284+
requiredLengthThreshold: query.offset,
285+
bestNameMatch: bestNameMatch,
286+
);
282287
break;
283288
case SearchOrder.created:
284289
indexedHits = _createdOrderedHits.whereInScores(packageScores);
@@ -322,10 +327,14 @@ class InMemoryPackageIndex {
322327
packageHits = indexedHits.map((h) => h.hit).toList();
323328
}
324329

330+
// Only indicate name match when the first item's score is lower than the second's score.
331+
final indicateNameMatch = bestNameMatch != null &&
332+
packageHits.length > 1 &&
333+
((packageHits[0].score ?? 0) <= (packageHits[1].score ?? 0));
325334
return PackageSearchResult(
326335
timestamp: clock.now().toUtc(),
327336
totalCount: totalCount,
328-
nameMatches: bestNameMatch == null ? null : [bestNameMatch],
337+
nameMatches: indicateNameMatch ? [bestNameMatch] : null,
329338
packageHits: packageHits,
330339
errorMessage: textResults?.errorMessage,
331340
);

app/lib/search/search_service.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ class ServiceSearchQuery {
283283
late final effectiveOrder = parsedQuery.order ?? order;
284284
bool get _hasQuery => query != null && query!.isNotEmpty;
285285
bool get _hasOnlyFreeText => _hasQuery && parsedQuery.hasOnlyFreeText;
286-
bool get _isNaturalOrder =>
286+
bool get isNaturalOrder =>
287287
effectiveOrder == null ||
288288
effectiveOrder == SearchOrder.top ||
289289
effectiveOrder == SearchOrder.text;
@@ -294,7 +294,7 @@ class ServiceSearchQuery {
294294
bool get includeSdkResults =>
295295
offset == 0 &&
296296
_hasOnlyFreeText &&
297-
_isNaturalOrder &&
297+
isNaturalOrder &&
298298
_hasNoOwnershipScope &&
299299
!_isFlutterFavorite &&
300300
(textMatchExtent ?? TextMatchExtent.api).shouldMatchApi();

app/test/search/api_doc_page_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ void main() {
5353
expect(json.decode(json.encode(result)), {
5454
'timestamp': isNotNull,
5555
'totalCount': 2,
56-
'nameMatches': ['foo'],
5756
'sdkLibraryHits': [],
5857
'packageHits': [
5958
{'package': 'foo', 'score': 1.0}, // finds package name

app/test/search/flutter_iap_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ void main() {
6969
expect(json.decode(json.encode(result)), {
7070
'timestamp': isNotNull,
7171
'totalCount': 1,
72-
'nameMatches': ['flutter_iap'],
7372
'sdkLibraryHits': [],
7473
'packageHits': [
7574
{'package': 'flutter_iap', 'score': 1.0},

app/test/search/handlers_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ void main() {
3333
await expectJsonResponse(await issueGet('/search?q=oxygen'), body: {
3434
'timestamp': isNotNull,
3535
'totalCount': 1,
36-
'nameMatches': ['oxygen'],
3736
'sdkLibraryHits': [],
3837
'packageHits': [
3938
{'package': 'oxygen', 'score': isPositive},

app/test/search/haversine_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ MIT'''),
380380
expect(json.decode(json.encode(result)), {
381381
'timestamp': isNotNull,
382382
'totalCount': 3,
383-
'nameMatches': ['haversine'],
384383
'sdkLibraryHits': [],
385384
'packageHits': [
386385
{'package': 'haversine', 'score': 1.0},

app/test/search/json_tool_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ void main() {
2727
expect(json.decode(json.encode(result)), {
2828
'timestamp': isNotNull,
2929
'totalCount': 1,
30-
'nameMatches': ['jsontool'],
3130
'sdkLibraryHits': [],
3231
'packageHits': [
3332
{'package': 'jsontool', 'score': 1.0},
@@ -70,8 +69,8 @@ void main() {
7069
'nameMatches': ['jsontool'],
7170
'sdkLibraryHits': [],
7271
'packageHits': [
73-
{'package': 'json2entity', 'score': 1.0},
7472
{'package': 'jsontool', 'score': 1.0},
73+
{'package': 'json2entity', 'score': 1.0},
7574
{'package': 'json_to_model', 'score': closeTo(0.73, 0.01)},
7675
],
7776
});

app/test/search/maps_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ void main() {
2626
expect(json.decode(json.encode(result)), {
2727
'timestamp': isNotNull,
2828
'totalCount': 2,
29-
'nameMatches': ['maps'],
3029
'sdkLibraryHits': [],
3130
'packageHits': [
3231
{'package': 'maps', 'score': 1.0},
@@ -44,8 +43,8 @@ void main() {
4443
'nameMatches': ['map'],
4544
'sdkLibraryHits': [],
4645
'packageHits': [
47-
{'package': 'maps', 'score': 1.0},
4846
{'package': 'map', 'score': 1.0},
47+
{'package': 'maps', 'score': 1.0},
4948
],
5049
});
5150
});

app/test/search/mem_index_test.dart

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
9595
expect(json.decode(json.encode(result)), {
9696
'timestamp': isNotNull,
9797
'totalCount': 1,
98-
'nameMatches': ['async'],
9998
'sdkLibraryHits': [],
10099
'packageHits': [
101100
{'package': 'async', 'score': closeTo(0.71, 0.01)},
@@ -254,7 +253,6 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
254253
expect(json.decode(json.encode(result)), {
255254
'timestamp': isNotNull,
256255
'totalCount': 2,
257-
'nameMatches': ['http'],
258256
'sdkLibraryHits': [],
259257
'packageHits': [
260258
{'package': 'http'},
@@ -628,7 +626,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
628626
]
629627
},
630628
);
631-
// exact name match without tags
629+
// exact name match without tags - promoted
632630
expect(
633631
(index.search(ServiceSearchQuery.parse(query: 'abc'))).toJson(), {
634632
'timestamp': isNotEmpty,
@@ -641,19 +639,29 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
641639
{'package': 'def', 'score': closeTo(0.85, 0.01)},
642640
]
643641
});
644-
// exact name match with tags
642+
// exact name match without tags - first anyway
643+
expect(
644+
(index.search(ServiceSearchQuery.parse(query: 'def'))).toJson(), {
645+
'timestamp': isNotEmpty,
646+
'totalCount': 2,
647+
'sdkLibraryHits': [],
648+
'packageHits': [
649+
{'package': 'def', 'score': closeTo(0.85, 0.01)},
650+
{'package': 'abc', 'score': closeTo(0.70, 0.01)},
651+
]
652+
});
653+
// absent exact name match with tags
645654
expect(
646655
(index.search(ServiceSearchQuery.parse(query: 'abc sdk:dart')))
647656
.toJson(),
648657
{
649658
'timestamp': isNotEmpty,
650659
'totalCount': 2,
651-
'nameMatches': ['abc'],
652660
'sdkLibraryHits': [],
653661
'packageHits': [
654-
// `abc` is at the first position, score is kept
655-
{'package': 'abc', 'score': closeTo(0.70, 0.01)},
662+
// `abc` is at the original position
656663
{'package': 'def', 'score': closeTo(0.85, 0.01)},
664+
{'package': 'abc', 'score': closeTo(0.70, 0.01)},
657665
]
658666
});
659667
// absent exact name match with tags
@@ -662,12 +670,10 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
662670
.toJson(),
663671
{
664672
'timestamp': isNotEmpty,
665-
'totalCount': 2,
666-
'nameMatches': ['abc'],
673+
'totalCount': 1,
667674
'sdkLibraryHits': [],
668675
'packageHits': [
669-
// `abc` is at the first position, score is zero
670-
{'package': 'abc', 'score': 0.0},
676+
// `abc` is absent
671677
{'package': 'def', 'score': closeTo(0.85, 0.01)},
672678
]
673679
});

app/test/search/stack_trace_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ void main() {
3232
expect(json.decode(json.encode(result)), {
3333
'timestamp': isNotNull,
3434
'totalCount': 1,
35-
'nameMatches': ['stack_trace'],
3635
'sdkLibraryHits': [],
3736
'packageHits': [
3837
{

0 commit comments

Comments
 (0)