@@ -225,11 +225,33 @@ class InMemoryPackageIndex {
225
225
final textResults = _searchText (
226
226
packageScores,
227
227
parsedQueryText,
228
- includeNameMatches: (query.offset ?? 0 ) == 0 ,
229
228
textMatchExtent: query.textMatchExtent ?? TextMatchExtent .api,
230
229
);
231
230
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
+ }
233
255
234
256
List <IndexedPackageHit > indexedHits;
235
257
switch (query.effectiveOrder ?? SearchOrder .top) {
@@ -292,7 +314,7 @@ class InMemoryPackageIndex {
292
314
return PackageSearchResult (
293
315
timestamp: clock.now ().toUtc (),
294
316
totalCount: totalCount,
295
- nameMatches: nameMatches ,
317
+ nameMatches: bestNameMatch == null ? null : [bestNameMatch] ,
296
318
packageHits: packageHits,
297
319
errorMessage: textResults? .errorMessage,
298
320
);
@@ -321,7 +343,6 @@ class InMemoryPackageIndex {
321
343
_TextResults ? _searchText (
322
344
IndexedScore <String > packageScores,
323
345
String ? text, {
324
- required bool includeNameMatches,
325
346
required TextMatchExtent textMatchExtent,
326
347
}) {
327
348
if (text == null || text.isEmpty) {
@@ -353,15 +374,6 @@ class InMemoryPackageIndex {
353
374
return aborted;
354
375
}
355
376
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
-
365
377
// Multiple words are scored separately, and then the individual scores
366
378
// are multiplied. We can use a package filter that is applied after each
367
379
// word to reduce the scope of the later words based on the previous results.
@@ -373,14 +385,6 @@ class InMemoryPackageIndex {
373
385
final matchApi = textMatchExtent.shouldMatchApi ();
374
386
375
387
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
-
384
388
_scorePool.withScore (
385
389
value: 0.0 ,
386
390
fn: (wordScore) {
@@ -454,10 +458,7 @@ class InMemoryPackageIndex {
454
458
}
455
459
}
456
460
457
- return _TextResults (
458
- topApiPages,
459
- nameMatches: nameMatches? .toList (),
460
- );
461
+ return _TextResults (topApiPages);
461
462
}
462
463
463
464
List <IndexedPackageHit > _rankWithValues (
@@ -535,20 +536,17 @@ class InMemoryPackageIndex {
535
536
536
537
class _TextResults {
537
538
final List <List <MapEntry <String , double >>?>? topApiPages;
538
- final List <String >? nameMatches;
539
539
final String ? errorMessage;
540
540
541
541
factory _TextResults .empty ({String ? errorMessage}) {
542
542
return _TextResults (
543
543
null ,
544
- nameMatches: null ,
545
544
errorMessage: errorMessage,
546
545
);
547
546
}
548
547
549
548
_TextResults (
550
549
this .topApiPages, {
551
- required this .nameMatches,
552
550
this .errorMessage,
553
551
});
554
552
}
0 commit comments